-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom Forest.py
More file actions
85 lines (83 loc) · 3.61 KB
/
Copy pathRandom Forest.py
File metadata and controls
85 lines (83 loc) · 3.61 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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.decomposition import PCA
from sklearn.model_selection import GridSearchCV,train_test_split,cross_val_score
from sklearn.metrics import classification_report,confusion_matrix
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_curve, auc
data=pd.read_csv('heart.csv')
dataX=data.drop('target',axis=1)
dataY=data['target']
X_train,X_test,y_train,y_test=train_test_split(dataX,dataY,test_size=0.2,random_state=42)
X_train=(X_train-np.min(X_train))/(np.max(X_train)-np.min(X_train)).values
X_test=(X_test-np.min(X_test))/(np.max(X_test)-np.min(X_test)).values
pca=PCA().fit(X_train)
pca = PCA(n_components=8)
pca.fit(X_train)
pca = PCA(n_components=8)
pca.fit(X_test)
reduced_data_train = pca.transform(X_train)
reduced_data_test = pca.transform(X_test)
reduced_data_train = pd.DataFrame(reduced_data_train,
columns=['Dim1','Dim2','Dim3','Dim4','Dim5','Dim6','Dim7','Dim8'])
reduced_data_test = pd.DataFrame(reduced_data_test,
columns=['Dim1','Dim2','Dim3','Dim4','Dim5','Dim6','Dim7','Dim8'])
X_train=reduced_data_train
X_test=reduced_data_test
def plot_roc_(false_positive_rate,true_positive_rate,roc_auc):
plt.figure(figsize=(5,5))
plt.title('Receiver Operating Characteristic')
plt.plot(false_positive_rate,true_positive_rate, color='red',label = 'AUC = %0.2f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],linestyle='--')
plt.axis('tight')
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()
def plot_feature_importances(gbm):
n_features = X_train.shape[1]
plt.barh(range(n_features), gbm.feature_importances_, align='center')
plt.yticks(np.arange(n_features), X_train.columns)
plt.xlabel("Feature importance")
plt.ylabel("Feature")
plt.ylim(-1, n_features)
combine_features_list=[
('Dim1','Dim2','Dim3'),
('Dim4','Dim5','Dim5','Dim6'),
('Dim7','Dim8','Dim1'),
('Dim4','Dim8','Dim5')
]
#RANDOM FOREST
parameters = [
{
'max_depth': np.arange(1, 10),
'min_samples_split': np.arange(2, 5),
'random_state': [3],
'n_estimators': np.arange(10, 20)
},
]
for features in combine_features_list:
X_train_set=X_train.loc[:,features]
X_test1_set=X_test.loc[:,features]
tree=GridSearchCV(RandomForestClassifier(),parameters,scoring='accuracy')
tree.fit(X_train_set, y_train)
print('Best parameters set:')
print(tree.best_params_)
predictions = [
(tree.predict(X_train_set), y_train, 'Train'),
(tree.predict(X_test1_set), y_test, 'Test1')
]
rfc=RandomForestClassifier(max_depth=7,min_samples_split=4,n_estimators=19,random_state=3)
rfc.fit(X_train,y_train)
y_pred=rfc.predict(X_test)
y_proba=rfc.predict_proba(X_test)
false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test,y_proba[:,1])
roc_auc = auc(false_positive_rate, true_positive_rate)
plot_roc_(false_positive_rate,true_positive_rate,roc_auc)
from sklearn.metrics import accuracy_score
print('Accurancy Oranı :',accuracy_score(y_test, y_pred))
print("RandomForestClassifier TRAIN score with ",format(rfc.score(X_train, y_train)))
print("RandomForestClassifier TEST score with ",format(rfc.score(X_test, y_test)))
print()