-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassificationModelsComparison.py
More file actions
87 lines (70 loc) · 2.83 KB
/
ClassificationModelsComparison.py
File metadata and controls
87 lines (70 loc) · 2.83 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
from sklearn.datasets import make_classification ,make_moons,make_circles
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
from matplotlib.colors import ListedColormap
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.inspection import DecisionBoundaryDisplay
import matplotlib.pyplot as plt
import numpy as np
X,y= make_classification(n_features=2,n_redundant=0,n_informative=2,n_clusters_per_class=1 ,random_state=42)
X +=1.2*np.random.uniform(size=X.shape)
Xy=(X,y)
# plt.scatter(X[:,0],X[:,1], c= y)
# plt.show()
# X,y= make_moons(noise=0.2,random_state=42)
# X,y = make_circles(noise=0.1,factor=0.3,random_state=42)
datasets = [Xy,
make_moons(noise=0.2,random_state=42),
make_circles(noise=0.1,factor=0.3,random_state=42)
]
fig= plt.figure(figsize=(6,9))
i=1
for ds_cnt , ds in enumerate(datasets):
X,y = ds
if ds_cnt ==0:
colors="darkred"
elif ds_cnt==1:
colors="darkblue"
else:
colors="darkgreen"
ax=plt.subplot(len(datasets),1,i)
ax.scatter(X[:,0],X[:,1], c= y,cmap=plt.cm.coolwarm,edgecolors="black")
i+=1
names =["Nearest Neighbors" , "Linear SVM","Decision Tree", "Random Forest", "Naive Bayes"]
classifiers =[KNeighborsClassifier(),
SVC(),
DecisionTreeClassifier(),
RandomForestClassifier(),
GaussianNB()]
fig= plt.figure(figsize=(6,9))
i=1
for ds_cnt , ds in enumerate(datasets):
X,y=ds
X_train , X_test , y_train , y_test =train_test_split(X,y,test_size=0.2,random_state=42)
cm_bright= ListedColormap(["darkred","darkblue"])
ax=plt.subplot(len(datasets),len(classifiers)+1,i)
if ds_cnt==0:
ax.set_title("Input Data")
ax.scatter(X_train[:,0],X_train[:,1],c=y_train,cmap= cm_bright,edgecolors="black")
ax.scatter(X_train[:,0],X_train[:,1],c=y_train,cmap= cm_bright,edgecolors="black",alpha=0.6)
i+=1
for name, clf in zip(names,classifiers):
ax=plt.subplot(len(datasets),len(classifiers)+1,i)
clf=make_pipeline(StandardScaler(),clf)
clf.fit(X_train,y_train)
score=clf.score(X_test,y_test)
DecisionBoundaryDisplay.from_estimator(clf,X,cmap=plt.cm.RdBu,alpha=0.7,ax=ax,eps=0.5)
ax.scatter(X_train[:,0],X_train[:,1],c=y_train,cmap= cm_bright,edgecolors="black")
ax.scatter(X_train[:,0],X_train[:,1],c=y_train,cmap= cm_bright,edgecolors="black",alpha=0.6)
if ds_cnt ==0:
ax.set_title(name)
ax.text(X[:,0].max()-0.15,
X[:,1].min()-0.35,
str(score))
i+=1
plt.show()