Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
debakarr
GitHub Repository: debakarr/machinelearning
Path: blob/master/Part 9 - Dimension Reduction/Principal Component Analysis/pca.py
1336 views
1
# PCA
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('Wine.csv')
10
X = dataset.iloc[:, 0:13].values
11
y = dataset.iloc[:, 13].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.2, 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
# Applying PCA
24
from sklearn.decomposition import PCA
25
pca = PCA(n_components = 2)
26
X_train = pca.fit_transform(X_train)
27
X_test = pca.transform(X_test)
28
explained_variance = pca.explained_variance_ratio_
29
30
# Fitting Logistic Regression to the Training set
31
from sklearn.linear_model import LogisticRegression
32
classifier = LogisticRegression(random_state = 0)
33
classifier.fit(X_train, y_train)
34
35
# Predicting the Test set results
36
y_pred = classifier.predict(X_test)
37
38
# Making the Confusion Matrix
39
from sklearn.metrics import confusion_matrix
40
cm = confusion_matrix(y_test, y_pred)
41
42
# Visualising the Training set results
43
from matplotlib.colors import ListedColormap
44
X_set, y_set = X_train, y_train
45
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
46
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
47
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
48
alpha = 0.75, cmap = ListedColormap(('red', 'green', 'blue')))
49
plt.xlim(X1.min(), X1.max())
50
plt.ylim(X2.min(), X2.max())
51
for i, j in enumerate(np.unique(y_set)):
52
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
53
c = ListedColormap(('red', 'green', 'blue'))(i), label = j)
54
plt.title('Logistic Regression (Training set)')
55
plt.xlabel('PC1')
56
plt.ylabel('PC2')
57
plt.legend()
58
plt.show()
59
60
# Visualising the Test set results
61
from matplotlib.colors import ListedColormap
62
X_set, y_set = X_test, y_test
63
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
64
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
65
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
66
alpha = 0.75, cmap = ListedColormap(('red', 'green', 'blue')))
67
plt.xlim(X1.min(), X1.max())
68
plt.ylim(X2.min(), X2.max())
69
for i, j in enumerate(np.unique(y_set)):
70
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
71
c = ListedColormap(('red', 'green', 'blue'))(i), label = j)
72
plt.title('Logistic Regression (Test set)')
73
plt.xlabel('PC1')
74
plt.ylabel('PC2')
75
plt.legend()
76
plt.show()
77