MACHINE LEARNING - CLASSIFICATION (LOGISTIC REGRESSION)
THE MAIN CODING CONCEPT IS SAME AS THE REGRESSION
CODE--
#logistic regression
#Created_By@THE AI DATA SCIENCE
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
#import_datset
dataset = pd.read_csv('Social_Network_Ads.csv')
x = dataset.iloc[:, 2:4].values
y= dataset.iloc[:, 4].values
#spliting_deataset_into_test_and_training_data
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)
#feature_scaling
from sklearn.preprocessing import StandardScaler
sc_x = StandardScaler()
x_train = sc_x.fit_transform(x_train)
x_test = sc_x.transform(x_test)
#fitting logistic regression
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state = 0)
classifier.fit(x_train , y_train)
#predict test data
y_pred = classifier.predict(x_test)
#making the confusion matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test , y_pred)
#visualizing
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('est salary')
plt.legend()
plt.show()
#visualizing
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('est salary')
plt.legend()
plt.show()
STAY TUNED FOR THE NEXT ML ALGORITHM CODE SNIPPET

Post a Comment