-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassification_Code.py
More file actions
97 lines (80 loc) · 2.6 KB
/
Classification_Code.py
File metadata and controls
97 lines (80 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.ensemble import RandomForestClassifier
import seaborn as sns
from google.colab import files
import io
#To upload dataset from local drive to colab
#uploaded = files.upload()
raw_data = pd.read_csv(io.BytesIO(uploaded["sonar.csv"]), header = None)
x = raw_data[raw_data.columns[0:60]].values
y = list(raw_data[[60]].values)
#Converting 'R' & 'M' to 0 & 1
y = [0 if x == 'R' else 1 for x in y]
def Get_Accuracy(model, x_,y_, num_com):
pca = PCA(n_components=num_com)
#pca.fit(x)
x_reduced = pca.fit_transform(x_)
pca_var = sum(pca.explained_variance_ratio_)
##Splitting training and test data
x_train, x_test, y_train, y_test = train_test_split(x_reduced, y_, test_size = 0.3, shuffle = True)
model.fit(x_train, y_train)
test_acc = model.score(x_test, y_test)
#print(test_accuracy)
return [pca_var, test_acc]
#RandomForestClassifier Model
model = RandomForestClassifier()
pca_var_ = []
test_acc_ = []
for i in range(1,61):
pca_var,test_acc = Get_Accuracy(model,x,y,i)
pca_var_.append(pca_var)
test_acc_.append(test_acc)
p = [i for i in range(1,61)]
plt.plot(p,pca_var_)
plt.xlabel("No. of features considered in PCA")
plt.ylabel("Explained Variance by PCA")
plt.show()
plt.plot(p,test_acc_)
plt.xlabel("No. of features considered in PCA")
plt.ylabel("Accuracy of reduced features in RandomForrest Model")
plt.show()
#LinearRegression Model
model = LinearRegression()
pca_var_ = []
test_acc_ = []
for i in range(1,61):
pca_var,test_acc = Get_Accuracy(model,x,y,i)
pca_var_.append(pca_var)
test_acc_.append(test_acc)
p = [i for i in range(1,61)]
plt.plot(p,pca_var_)
plt.xlabel("No. of features considered in PCA")
plt.ylabel("Explained Variance by PCA")
plt.show()
plt.plot(p,test_acc_)
plt.xlabel("No. of features considered in PCA")
plt.ylabel("Accuracy of reduced features in LinearRegression Model")
plt.show()
#LogisticRegression Model
model = LogisticRegression()
pca_var_ = []
test_acc_ = []
for i in range(1,61):
pca_var,test_acc = Get_Accuracy(model,x,y,i)
pca_var_.append(pca_var)
test_acc_.append(test_acc)
p = [i for i in range(1,61)]
plt.plot(p,pca_var_)
plt.xlabel("No. of features considered in PCA")
plt.ylabel("Explained Variance by PCA")
plt.show()
plt.plot(p,test_acc_)
plt.xlabel("No. of features considered in PCA")
plt.ylabel("Accuracy of reduced features in LogisticRegression Model")
plt.show()