-
Notifications
You must be signed in to change notification settings - Fork 0
/
LDA-Logestic regression using iris data set.py
66 lines (38 loc) · 1.47 KB
/
LDA-Logestic regression using iris data set.py
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
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('iris.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
from sklearn.linear_model import LogisticRegression
classifier1 = LogisticRegression(random_state = 0)
classifier1.fit(X_train, y_train)
y_pred1 = classifier1.predict(X_test)
score1 = classifier1.score(X_test, y_test)
print('score 1 = ', score1 )
from sklearn.metrics import confusion_matrix
cm1 = confusion_matrix(y_test, y_pred1)
print('cm1 \n' , cm1 )
import seaborn as sns
sns.heatmap(cm1, center=True)
plt.show()
#####################################################################
# apply LDA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
lda = LDA(n_components = 2)
X_train = lda.fit_transform(X_train, y_train)
X_test = lda.transform(X_test)
classifier2 = LogisticRegression(random_state = 0)
classifier2.fit(X_train, y_train)
y_pred2 = classifier2.predict(X_test)
score2 = classifier2.score(X_test, y_test)
print('score 2 = ', score2 )
cm2 = confusion_matrix(y_test, y_pred2)
print('cm2 \n' , cm2 )
sns.heatmap(cm2, center=True)
plt.show()