Path: blob/master/Part 3 - Classification/Naive Bayes/classification_template.py
1009 views
# Classification template12# Importing the libraries3import numpy as np4import matplotlib.pyplot as plt5import pandas as pd67# Importing the dataset8dataset = pd.read_csv('Social_Network_Ads.csv')9X = dataset.iloc[:, [2, 3]].values10y = dataset.iloc[:, 4].values1112# Splitting the dataset into the Training set and Test set13from sklearn.cross_validation import train_test_split14X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)1516# Feature Scaling17from sklearn.preprocessing import StandardScaler18sc = StandardScaler()19X_train = sc.fit_transform(X_train)20X_test = sc.transform(X_test)2122# Fitting classifier to the Training set23# Create your classifier here2425# Predicting the Test set results26y_pred = classifier.predict(X_test)2728# Making the Confusion Matrix29from sklearn.metrics import confusion_matrix30cm = confusion_matrix(y_test, y_pred)3132# Visualising the Training set results33from matplotlib.colors import ListedColormap34X_set, y_set = X_train, y_train35X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),36np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))37plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),38alpha = 0.75, cmap = ListedColormap(('red', 'green')))39plt.xlim(X1.min(), X1.max())40plt.ylim(X2.min(), X2.max())41for i, j in enumerate(np.unique(y_set)):42plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],43c = ListedColormap(('red', 'green'))(i), label = j)44plt.title('Classifier (Training set)')45plt.xlabel('Age')46plt.ylabel('Estimated Salary')47plt.legend()48plt.show()4950# Visualising the Test set results51from matplotlib.colors import ListedColormap52X_set, y_set = X_test, y_test53X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),54np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))55plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),56alpha = 0.75, cmap = ListedColormap(('red', 'green')))57plt.xlim(X1.min(), X1.max())58plt.ylim(X2.min(), X2.max())59for i, j in enumerate(np.unique(y_set)):60plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],61c = ListedColormap(('red', 'green'))(i), label = j)62plt.title('Classifier (Test set)')63plt.xlabel('Age')64plt.ylabel('Estimated Salary')65plt.legend()66plt.show()6768