Skip to content

Commit e4115dc

Browse files
added till may 9th
all deep learning algorithms until 9th may
1 parent 252d945 commit e4115dc

File tree

134 files changed

+195793
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+195793
-0
lines changed

1_learning/10_model_selection/Social_Network_Ads.csv

Lines changed: 401 additions & 0 deletions
Large diffs are not rendered by default.

1_learning/10_model_selection/grid_search.ipynb

Lines changed: 440 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Grid Search
2+
3+
# Importing the libraries
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
import pandas as pd
7+
8+
# Importing the dataset
9+
dataset = pd.read_csv('Social_Network_Ads.csv')
10+
X = dataset.iloc[:, :-1].values
11+
y = dataset.iloc[:, -1].values
12+
13+
# Splitting the dataset into the Training set and Test set
14+
from sklearn.model_selection import train_test_split
15+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
16+
17+
# Feature Scaling
18+
from sklearn.preprocessing import StandardScaler
19+
sc = StandardScaler()
20+
X_train = sc.fit_transform(X_train)
21+
X_test = sc.transform(X_test)
22+
23+
# Training the Kernel SVM model on the Training set
24+
from sklearn.svm import SVC
25+
classifier = SVC(kernel = 'rbf', random_state = 0)
26+
classifier.fit(X_train, y_train)
27+
28+
# Making the Confusion Matrix
29+
from sklearn.metrics import confusion_matrix, accuracy_score
30+
y_pred = classifier.predict(X_test)
31+
cm = confusion_matrix(y_test, y_pred)
32+
print(cm)
33+
accuracy_score(y_test, y_pred)
34+
35+
# Applying k-Fold Cross Validation
36+
from sklearn.model_selection import cross_val_score
37+
accuracies = cross_val_score(estimator = classifier, X = X_train, y = y_train, cv = 10)
38+
print("Accuracy: {:.2f} %".format(accuracies.mean()*100))
39+
print("Standard Deviation: {:.2f} %".format(accuracies.std()*100))
40+
41+
# Applying Grid Search to find the best model and the best parameters
42+
from sklearn.model_selection import GridSearchCV
43+
parameters = [{'C': [0.25, 0.5, 0.75, 1], 'kernel': ['linear']},
44+
{'C': [0.25, 0.5, 0.75, 1], 'kernel': ['rbf'], 'gamma': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]}]
45+
grid_search = GridSearchCV(estimator = classifier,
46+
param_grid = parameters,
47+
scoring = 'accuracy',
48+
cv = 10,
49+
n_jobs = -1)
50+
grid_search.fit(X_train, y_train)
51+
best_accuracy = grid_search.best_score_
52+
best_parameters = grid_search.best_params_
53+
print("Best Accuracy: {:.2f} %".format(best_accuracy*100))
54+
print("Best Parameters:", best_parameters)
55+
56+
# Visualising the Training set results
57+
from matplotlib.colors import ListedColormap
58+
X_set, y_set = X_train, y_train
59+
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
60+
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
61+
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
62+
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
63+
plt.xlim(X1.min(), X1.max())
64+
plt.ylim(X2.min(), X2.max())
65+
for i, j in enumerate(np.unique(y_set)):
66+
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
67+
c = ListedColormap(('red', 'green'))(i), label = j)
68+
plt.title('Kernel SVM (Training set)')
69+
plt.xlabel('Age')
70+
plt.ylabel('Estimated Salary')
71+
plt.legend()
72+
plt.show()
73+
74+
# Visualising the Test set results
75+
from matplotlib.colors import ListedColormap
76+
X_set, y_set = X_test, y_test
77+
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
78+
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
79+
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
80+
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
81+
plt.xlim(X1.min(), X1.max())
82+
plt.ylim(X2.min(), X2.max())
83+
for i, j in enumerate(np.unique(y_set)):
84+
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
85+
c = ListedColormap(('red', 'green'))(i), label = j)
86+
plt.title('Kernel SVM (Test set)')
87+
plt.xlabel('Age')
88+
plt.ylabel('Estimated Salary')
89+
plt.legend()
90+
plt.show()

0 commit comments

Comments
 (0)