我正在尝试为二项分类构建随机森林分类器。有人能解释为什么我每次运行这个程序时我的准确度得分都不同吗?分数在68%到74%之间变化。此外,我尝试调整参数,但我不能达到74以上的准确性。任何建议也将不胜感激。我尝试使用GridSearchCV,但我只增加了3分。
#import libraries
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn import preprocessing
#read data into pandas dataframe
df = pd.read_csv("data.csv")
#handle missing values
df = df.dropna(axis = 0, how = 'any')
#handle string-type data
le = preprocessing.LabelEncoder()
le.fit(['Male','Female'])
df.loc[:,'Sex'] = le.transform(df['Sex'])
#split into train and test data
df['is_train'] = np.random.uniform(0, 1, len(df)) <= 0.8
train, test = df[df['is_train'] == True], df[df['is_train'] == False]
#make an array of columns
features = df.columns[:10]
#build the classifier
clf = RandomForestClassifier()
#train the classifier
y = train['Selector']
clf.fit(train[features], train['Selector'])
#test the classifier
clf.predict(test[features])
#calculate accuracy
accuracy_score(test['Selector'], clf.predict(test[features]))
accuracy_score(train['Selector'], clf.predict(train[features]))
答案 0 :(得分:3)
每次运行程序时,您的准确性都会更改,因为创建的模型不同。并且模型是不同的,因为您在创建它时没有修复随机状态。请查看scikit-learn documentation中的random_state
参数。
对于第二个问题,您可以尝试许多方法来提高模型的准确性。按重要性排序: