forked from laviii123/Btecky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ML_program_for_AD_sale_predection
61 lines (54 loc) · 2.53 KB
/
ML_program_for_AD_sale_predection
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# **Project 1- Ad Sale Prediction from existiong customer- Logistic Regression**
**Importing Libraries**
import pandas as pd # useful for loading the dataset
import numpy as np #to perform array
**Choose Dataset file from Local Directory**
data = pd.read_csv(R"C:\Users\csc\OneDrive\Desktop\Python Program\Project 1- Sale prediction from an existing customer using LOGISTIC REGRESSION\ad_dataseto.csv")
**Load Dataset using pandas library**
dataset = pd.read_csv(R'C:\Users\csc\OneDrive\Desktop\Python Program\Project 1- Sale prediction from an existing customer using LOGISTIC REGRESSION\ad_dataseto.csv')
print(dataset)
**Summarize Dataset**
print(dataset.shape) # no. of rows and columns
print(dataset.head(5)) #top values of data
**Segrate Dataset into X(Input/Independent Variable) and Y(Output/Dependent Variable)**
X = dataset.iloc[:, :-1].values
X
Y = dataset.iloc[:,-1].values
Y
**Splitting Dataset into Train and Test**
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.25, random_state=0)
***Feature Scaling***
we scale our data to make all the features contribute equally to the result
Fit_Transform- fit method is calculating the mean and variance of each of the features present in our data.
**Transform** - Transform method is transforming alll the features using the respective mean and variance,
We want our test data to be completely new and a surprise set for our model
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print(X_train)
**Training/Algorithm**
from sklearn.linear_model import LogisticRegression
model = LogisticRegression() #loading the algorithm
model.fit(X_train, Y_train) #train
**Prediction for all Test Data**
Y_pred = model.predict(X_test)
#print(np.concatenate((Y_pred.reshape(len(Y_pred),1), Y_test.reshape(len(Y_test),1)),1))
**Evaluating Model- CONFUSION MATRIX**
#from sklearn.matrics import confusion_matrix, accuracy_score
from sklearn.metrics import accuracy_score
# cm = confusion_matrix(Y_test, Y_pred)
#print(" Confusion Matrix: ")
#print(cm)
print(" Accuracy of the Model: {0}% ".format(accuracy_score(Y_test, Y_pred)*100))
**Predicting, wheather new customer with Age and Salary will Buy or Not**
age = int(input(" Enter New Customer's Age: "))
salary = int(input(" Enter New Customer's Salary: "))
newCust = [[age,salary]]
result = model.predict(sc.transform(newCust))
print(result)
if result == 1:
print("Customer will Buy")
else:
print("Customer won't Buy")