-
Notifications
You must be signed in to change notification settings - Fork 19
/
b-30-differences_between_ML_and_DL.py
73 lines (58 loc) · 2.13 KB
/
b-30-differences_between_ML_and_DL.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
67
68
69
70
71
72
73
from sklearn import datasets
iris = datasets.load_iris()
x = iris.data
y = iris.target
from sklearn.model_selection import train_test_split
x_train, x_test,y_train,y_test = train_test_split(x,y,test_size=0.33, random_state=0)
#verilerin olceklenmesi
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(x_train)
X_test = sc.fit_transform(x_test)
X=sc.fit_transform(x)
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis #LDA
lda = LinearDiscriminantAnalysis(n_components=1) #bir boyuta indirdik
X_train2 = lda.fit_transform(X_train, y_train)
X_test2 = lda.fit_transform(X_test, y_test)
X=lda.fit_transform(x,y)
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()
lr.fit(X_train,y_train)
#LDA uygulanmamış veri
lr2 = LogisticRegression()
lr2.fit(X_train2,y_train)
#Predictions
y_pred = lr.predict(X_test)
y_pred2 = lr2.predict(X_test2)
print (X.shape)
from keras.utils import to_categorical
y = to_categorical(y, num_classes=3)
y_test2 = to_categorical(y_test, num_classes=3)
#PREDİCTİON WİTH KERAS
from keras.layers import Dense
from keras.models import Sequential
model = Sequential()
model.add(Dense(10, kernel_initializer='glorot_uniform', activation='relu', input_dim=1))
model.add(Dense(8,activation='tanh'))
model.add(Dense(6,activation='tanh'))
model.add(Dense(3,activation='softmax'))
model.compile(optimizer="SGD",loss="categorical_crossentropy",metrics=['accuracy'])
model.fit(X,y,batch_size=5,epochs=200)
y_pred3 = model.predict(X_test2) #keras prediction
from sklearn.metrics import confusion_matrix
from sklearn.metrics import r2_score
#BOYUT İNDİRGEME İŞLEMİNİN BAŞARISI:(LDA)
print ("\nOriginal Prediction Matrix:")
cm1 = confusion_matrix(y_test, y_pred)
print (cm1)
print ("\nLDA Applied Prediction:")
cm2 = confusion_matrix(y_test, y_pred2)
print (cm2)
cm1 = confusion_matrix(y_test, y_pred)
#MODELLERİN R KARE DEĞERLERİNE GÖRE BAŞARISI:
r2_1 = r2_score(y_test,y_pred)
print("\nOrjinal R2 score:", r2_1)
r2_2 = r2_score(y_test,y_pred2)
print("LDA Applied R2 score:", r2_2)
r2_3 = r2_score(y_test2,y_pred3)
print ("Keras R2 score:",r2_3)