SagarManchekar commited on
Commit
8e7f7c1
·
1 Parent(s): 8e268f1

Upload logistic_regression.py

Browse files
Files changed (1) hide show
  1. logistic_regression.py +104 -0
logistic_regression.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """logistic_regression.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1-Slk6y5-E3eUnmM4vjtoRrGMoIKvD0hU
8
+
9
+ # Logistic Regression
10
+
11
+ ## Importing the libraries
12
+ """
13
+
14
+ import numpy as np
15
+ import matplotlib.pyplot as plt
16
+ import pandas as pd
17
+
18
+ """## Importing the dataset"""
19
+
20
+ dataset = pd.read_csv('Social_Network_Ads.csv')
21
+ X = dataset.iloc[:, :-1].values
22
+ y = dataset.iloc[:, -1].values
23
+
24
+ """## Splitting the dataset into the Training set and Test set"""
25
+
26
+ from sklearn.model_selection import train_test_split
27
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
28
+
29
+ print(X_train)
30
+
31
+ print(y_train)
32
+
33
+ print(X_test)
34
+
35
+ print(y_test)
36
+
37
+ """## Feature Scaling"""
38
+
39
+ from sklearn.preprocessing import StandardScaler
40
+ sc = StandardScaler()
41
+ X_train = sc.fit_transform(X_train)
42
+ X_test = sc.transform(X_test)
43
+
44
+ print(X_train)
45
+
46
+ print(X_test)
47
+
48
+ """## Training the Logistic Regression model on the Training set"""
49
+
50
+ from sklearn.linear_model import LogisticRegression
51
+ classifier = LogisticRegression(random_state = 0)
52
+ classifier.fit(X_train, y_train)
53
+
54
+ """## Predicting a new result"""
55
+
56
+ print(classifier.predict(sc.transform([[30,87000]])))
57
+
58
+ """## Predicting the Test set results"""
59
+
60
+ y_pred = classifier.predict(X_test)
61
+ print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))
62
+
63
+ """## Making the Confusion Matrix"""
64
+
65
+ from sklearn.metrics import confusion_matrix, accuracy_score
66
+ cm = confusion_matrix(y_test, y_pred)
67
+ print(cm)
68
+ accuracy_score(y_test, y_pred)
69
+
70
+ """## Visualising the Training set results"""
71
+
72
+ from matplotlib.colors import ListedColormap
73
+ X_set, y_set = sc.inverse_transform(X_train), y_train
74
+ X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 10, stop = X_set[:, 0].max() + 10, step = 0.25),
75
+ np.arange(start = X_set[:, 1].min() - 1000, stop = X_set[:, 1].max() + 1000, step = 0.25))
76
+ plt.contourf(X1, X2, classifier.predict(sc.transform(np.array([X1.ravel(), X2.ravel()]).T)).reshape(X1.shape),
77
+ alpha = 0.75, cmap = ListedColormap(('salmon', 'dodgerblue')))
78
+ plt.xlim(X1.min(), X1.max())
79
+ plt.ylim(X2.min(), X2.max())
80
+ for i, j in enumerate(np.unique(y_set)):
81
+ plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('salmon', 'dodgerblue'))(i), label = j)
82
+ plt.title('Logistic Regression (Training set)')
83
+ plt.xlabel('Age')
84
+ plt.ylabel('Estimated Salary')
85
+ plt.legend()
86
+ plt.show()
87
+
88
+ """## Visualising the Test set results"""
89
+
90
+ from matplotlib.colors import ListedColormap
91
+ X_set, y_set = sc.inverse_transform(X_test), y_test
92
+ X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 10, stop = X_set[:, 0].max() + 10, step = 0.25),
93
+ np.arange(start = X_set[:, 1].min() - 1000, stop = X_set[:, 1].max() + 1000, step = 0.25))
94
+ plt.contourf(X1, X2, classifier.predict(sc.transform(np.array([X1.ravel(), X2.ravel()]).T)).reshape(X1.shape),
95
+ alpha = 0.75, cmap = ListedColormap(('salmon', 'dodgerblue')))
96
+ plt.xlim(X1.min(), X1.max())
97
+ plt.ylim(X2.min(), X2.max())
98
+ for i, j in enumerate(np.unique(y_set)):
99
+ plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('salmon', 'dodgerblue'))(i), label = j)
100
+ plt.title('Logistic Regression (Test set)')
101
+ plt.xlabel('Age')
102
+ plt.ylabel('Estimated Salary')
103
+ plt.legend()
104
+ plt.show()