ML - Lab-6.ipynb - Colab
ML - Lab-6.ipynb - Colab
Write a program to implement the Logistic Regression for the given dataset and compute the accuracy of the classifier
###LOGISTIC REG
#Data Pre-procesing Step
# importing libraries
import numpy as nm
import matplotlib.pyplot as mtp
import pandas as pd
import seaborn as sns
#importing datasets
data_set= pd.read_csv('/content/Social_Network_Ads.csv')
42 16
39 15
40 15
48 14
47 14
27 13
38 13
28 12
36 12
46 12
31 11
30 11
49 10
29 10
33 9
32 9
24 9
34 6
23 6
52 6
25 6
58 6
53 5
57 5
22 5
18 5
21 4
50 4
54 4
55 3
56 3
51 3
43 3
44 2
Name: count, dtype: int64
EstimatedSalary
72000 12
80000 11
79000 10
75000 9
71000 9
..
123000 1
37000 1
115000 1
148000 1
139000 1
Name: count, Length: 117, dtype: int64
[[74 5]
[11 30]]
Accuracy score 0.8666666666666667
Recall score 0.7317073170731707
Precision score 0.8571428571428571
<ipython-input-50-e33159ed9acd>:11: UserWarning: *c* argument looks like a single numeric RGB or RGBA sequence, which should be avoi
mtp.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1],
##test
#Visulaizing the test set result
from matplotlib.colors import ListedColormap
x_set, y_set = x_test, y_test
x1, x2 = nm.meshgrid(nm.arange(start = x_set[:, 0].min() - 1, stop = x_set[:, 0].max() + 1, step =0.01),
nm.arange(start = x_set[:, 1].min() - 1, stop = x_set[:, 1].max() + 1, step = 0.01))
mtp.contourf(x1, x2, classifier.predict(nm.array([x1.ravel(), x2.ravel()]).T).reshape(x1.shape),
alpha = 0.75, cmap = ListedColormap(('blue','green' )))
mtp.xlim(x1.min(), x1.max())
mtp.ylim(x2.min(), x2.max())
for i, j in enumerate(nm.unique(y_set)):
mtp.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1],
c = ListedColormap(('blue', 'green'))(i), label = j)
mtp.title('Logistic Regression (Test set)')
mtp.xlabel('Age')
mtp.ylabel('Estimated Salary')
mtp.legend()
mtp.show()
<ipython-input-56-57183aa04161>:12: UserWarning: *c* argument looks like a single numeric RGB or RGBA sequence, which should be avoi
mtp.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1],
import statsmodels.api as sm
x1=sm.add_constant(x)
print(x1)
logit_model=sm.Logit(y,x1)
result=logit_model.fit()
print(result.summary2())
print(result.summary())