Random Forest Classifier

Kategori: Machine Learning , 09 Aralık 2019 , JanFranco


Random forest algoritmasını görmüştük. Regression bölümünde kullanmıştık. Classify problemlerinde de kullanabiliriz:


import pandas as pd
from sklearn.impute import SimpleImputer
from sklearn.metrics import confusion_matrix
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, OneHotEncoder

df = pd.read_csv('veriler.csv')

missing = SimpleImputer()
age = df.iloc[:, [3]].values
age = missing.fit_transform(age)
age = pd.DataFrame(data=age, columns=['age'])

labelEncoder = LabelEncoder()
gender = df.iloc[:, [4]].values
gender = labelEncoder.fit_transform(gender.ravel())
gender = pd.DataFrame(data=gender, columns=['gender'])

oneHotEncoder = OneHotEncoder()
country = df.iloc[:, [0]].values
country = oneHotEncoder.fit_transform(country).toarray()
country = pd.DataFrame(data=country, columns=['fr', 'tr', 'us'])

hw = df.iloc[:, [1, 2]].values
hw = pd.DataFrame(data=hw, columns=['height', 'weight'])

df = pd.concat([country, hw, age, gender], axis=1)
print(df)

x = df.iloc[:, [3, 4, 5]]
y = df.iloc[:, [6]]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.33, random_state=0)

randomForest = RandomForestClassifier(n_estimators=5)
randomForest.fit(x_train, y_train.values.ravel())
decisionTreeRes = randomForest.predict(x_test)

confusionMatrix = confusion_matrix(y_true=y_test, y_pred=decisionTreeRes)
print("***** Confusion Matrix - 5 Tree *****")
print("     E   K")
print("E   ", confusionMatrix[0][0], " ", confusionMatrix[0][1])
print("K   ", confusionMatrix[1][0], " ", confusionMatrix[1][1])

randomForest = RandomForestClassifier(n_estimators=10)
randomForest.fit(x_train, y_train.values.ravel())
decisionTreeRes = randomForest.predict(x_test)

confusionMatrix = confusion_matrix(y_true=y_test, y_pred=decisionTreeRes)
print("***** Confusion Matrix - 10 Tree *****")
print("     E   K")
print("E   ", confusionMatrix[0][0], " ", confusionMatrix[0][1])
print("K   ", confusionMatrix[1][0], " ", confusionMatrix[1][1])

randomForest = RandomForestClassifier(n_estimators=15)
randomForest.fit(x_train, y_train.values.ravel())
decisionTreeRes = randomForest.predict(x_test)

confusionMatrix = confusion_matrix(y_true=y_test, y_pred=decisionTreeRes)
print("***** Confusion Matrix - 15 Tree *****")
print("     E   K")
print("E   ", confusionMatrix[0][0], " ", confusionMatrix[0][1])
print("K   ", confusionMatrix[1][0], " ", confusionMatrix[1][1])

>>

     fr   tr   us  height  weight    age  gender
0   0.0  1.0  0.0     130      30  10.00       0
1   0.0  1.0  0.0     125      36  11.00       0
2   0.0  1.0  0.0     135      34  10.00       1
3   0.0  1.0  0.0     133      30   9.00       1
4   0.0  1.0  0.0     129      38  12.00       0
5   0.0  1.0  0.0     180      90  30.00       0
6   0.0  1.0  0.0     190      80  25.00       0
7   0.0  1.0  0.0     175      90  35.00       0
8   0.0  1.0  0.0     177      60  22.00       1
9   0.0  0.0  1.0     185     105  33.00       0
10  0.0  0.0  1.0     165      55  27.00       1
11  0.0  0.0  1.0     155      50  44.00       1
12  0.0  0.0  1.0     160      58  28.45       1
13  0.0  0.0  1.0     162      59  41.00       1
14  0.0  0.0  1.0     167      62  55.00       1
15  1.0  0.0  0.0     174      70  47.00       0
16  1.0  0.0  0.0     193      90  28.45       0
17  1.0  0.0  0.0     187      80  27.00       0
18  1.0  0.0  0.0     183      88  28.00       0
19  1.0  0.0  0.0     159      40  29.00       1
20  1.0  0.0  0.0     164      66  32.00       1
21  1.0  0.0  0.0     166      56  42.00       1
***** Confusion Matrix - 5 Tree *****
     E   K
E    1   0
K    0   7
***** Confusion Matrix - 10 Tree *****
     E   K
E    1   0
K    0   7
***** Confusion Matrix - 15 Tree *****
     E   K
E    1   0
K    1   6


Sonraki Yazı: Visualization - 3D Visualization
Yorumlar

Henüz bir yorum bulunmuyor.