【问题标题】:Random Forest Classifier随机森林分类器
【发布时间】:2018-02-18 15:32:07
【问题描述】:

我正在尝试为二项式分类构建一个随机森林分类器。有人可以解释为什么每次运行此程序时我的准确度分数都会有所不同吗?分数在 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]))

【问题讨论】:

标签: machine-learning random-forest data-analysis grid-search


【解决方案1】:

每次运行程序时,您的准确度都会发生变化,因为创建的模型不同。并且模型不同,因为创建它时您没有修复随机状态。查看scikit-learn documentation 中的random_state 参数。

对于第二个问题,您可以尝试很多方法来提高模型的准确性。按重要性排序:

  • 获取更多训练数据
  • 改进您的训练数据(即去除低质量特征或样本,创建新特征......)
  • 调整您的学习算法的参数(RandomForest 有一些可玩)
  • 尝试另一种学习模式。
  • 尝试组合不同的模型

【讨论】:

  • 我试图调整参数,结果是:n_jobs = -1,n_estimators = 75,min_samples_leaf = 25,random_state = 42,oob_score = True。但我仍然得到不同的精度值:Out[472]: 0.75221238938053092, Out[474]: 0.68965517241379315
  • 如果我正确阅读了您的代码,您的训练/测试拆分器也是随机的(您可以使用 from sklearn.model_selection import train_test_split 接受 random_state 参数)
猜你喜欢
  • 2018-05-20
  • 2018-03-05
  • 1970-01-01
  • 2019-09-05
  • 2013-09-22
  • 2019-08-11
  • 2020-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多