-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinalLogesticRegresion.py
225 lines (114 loc) · 3.79 KB
/
FinalLogesticRegresion.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# coding: utf-8
# In[1]:
#Logetic regression
# In[2]:
#import Libraries
# In[3]:
import numpy as np
# In[4]:
import matplotlib.pyplot as plt
# In[5]:
import pandas as pd
# In[6]:
#Importing data set
# In[9]:
dataset=pd.read_csv('/home/raniladkat/Downloads/Logistic_Regression/Logistic_Regression/Social_Network_Ads.csv')
# In[10]:
dataset
# In[11]:
X = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, 4].values
# In[12]:
X
# In[13]:
y
# In[14]:
# Splitting the dataset into the Training set and Test set
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
# In[15]:
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# In[16]:
# Fitting Logistic Regression to the Training set
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state = 0)
classifier.fit(X_train, y_train)
# In[43]:
# Predicting the Test set results
#newtestdata=pd.read_csv('/home/raniladkat/Downloads/Logistic_Regression/Logistic_Regression/Social_Network_Ads.csv')
#y_pred=classifier.predict_proba(newtestedata)
y_pred = classifier.predict_proba(X_test)
data1=pd.DataFrame(y_pred)
data1.columns=["Rejected","Approved"]
data1
# In[37]:
data1["BoardedProbability"]=data1.Approved.apply(groupbyprob)
data1
# In[23]:
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
cm
# In[29]:
##Converting test data into Probability
def groupbyprob(x):
if x > 0.9:
return "high"
elif x > 0.7 and x < 0.09:
return "medium"
elif x > 0.5 and x <0.7:
return "lowmedium"
else:
return "Rejected"
# In[19]:
# Visualising the Training set results
from matplotlib.colors import ListedColormap
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('Logistic Regression (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()
# In[20]:
# Visualising the Test set results
from matplotlib.colors import ListedColormap
X_set, y_set = X_test, y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('Logistic Regression (Test set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()
# In[39]:
data1.groupby("BoardedProbability").size()
# In[40]:
data1[data1["BoardedProbability"]=="high"]
# In[41]:
from sklearn.metrics import accuracy_score
# In[44]:
y_pred = classifier.predict(X_test)
# In[45]:
y_pred
# In[46]:
accuracy_score(y_test,y_pred)