2017-09-10 206 views
2

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

链接数据集:https://archive.ics.uci.edu/ml/datasets/ILPD+(Indian+Liver+Patient+Dataset) – TheBeginner

+0

为了提高你的模型,我建议你使用合奏,也尝试XGBoost。 –

回答

1

由于创建的模型不同,每次运行程序时精度都会发生变化。和模型是不同的,因为你创建它时没有修复随机状态。查看scikit-learn documentationrandom_state参数。

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

  • 获取更多的训练数据
  • 提高你的训练数据(即摆脱低质量的功能或样品,创造新的功能...)
  • 调整你的学习算法的参数(RandomForest有几个玩)
  • 尝试另一种学习模式。
  • 尝试结合不同型号
+0

我试图调整参数,最后在:n_jobs = -1,n_estimators = 75,min_samples_leaf = 25,random_state = 42,oob_score = True。但是我仍然得到不同的准确度值:Out [472]:0.75221238938053092,Out [474]:0.68965517241379315 – TheBeginner