Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
debakarr
GitHub Repository: debakarr/machinelearning
Path: blob/master/Part 3 - Classification/Random Forest/random_forest_classification.py
1009 views
1
# Random Forest Classification
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[:, [2, 3]].values
11
y = dataset.iloc[:, 4].values
12
13
# Splitting the dataset into the Training set and Test set
14
from sklearn.cross_validation 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
# Fitting Random Forest Classification to the Training set
24
from sklearn.ensemble import RandomForestClassifier
25
classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0)
26
classifier.fit(X_train, y_train)
27
28
# Predicting the Test set results
29
y_pred = classifier.predict(X_test)
30
31
# Making the Confusion Matrix
32
from sklearn.metrics import confusion_matrix
33
cm = confusion_matrix(y_test, y_pred)
34
35
# Visualising the Training set results
36
from matplotlib.colors import ListedColormap
37
X_set, y_set = X_train, y_train
38
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
39
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
40
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
41
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
42
plt.xlim(X1.min(), X1.max())
43
plt.ylim(X2.min(), X2.max())
44
for i, j in enumerate(np.unique(y_set)):
45
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
46
c = ListedColormap(('red', 'green'))(i), label = j)
47
plt.title('Random Forest Classification (Training set)')
48
plt.xlabel('Age')
49
plt.ylabel('Estimated Salary')
50
plt.legend()
51
plt.show()
52
53
# Visualising the Test set results
54
from matplotlib.colors import ListedColormap
55
X_set, y_set = X_test, y_test
56
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
57
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
58
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
59
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
60
plt.xlim(X1.min(), X1.max())
61
plt.ylim(X2.min(), X2.max())
62
for i, j in enumerate(np.unique(y_set)):
63
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
64
c = ListedColormap(('red', 'green'))(i), label = j)
65
plt.title('Random Forest Classification (Test set)')
66
plt.xlabel('Age')
67
plt.ylabel('Estimated Salary')
68
plt.legend()
69
plt.show()
70