2014-02-23 30 views
1

我试图运行从科幻套件中的随机森林分类学习和获取可疑不良输出 - 预测的不到1%是正确的。该模型的表现比机会差得多。我对Python,ML和sci-kit学习(一种三重奏)相对来说比较陌生,我担心的是我错过了一些基本的东西,而不是需要微调参数。我希望看到的是更多的老眼,通过代码来查看设置是否有问题。故障排除随机森林分类学

我试图根据字出现来预测在电子表格中的行类 - 所以每行的输入是表示数组出现多少次的每个单词,例如[1 0 0 2 0 ... 1]。我正在使用sci-kit learn的CountVectorizer来做这个处理 - 我给它提供包含每行中的单词的字符串,并且它输出单词出现数组。如果这个输入不适合某种原因,那可能是事情发生错误的地方,但我没有在网上找到任何东西,或者在文档中发现这种情况。

眼下,森林正确回答的时间约0.5%。在SGD分类器中使用完全相同的输入会产生接近80%的值,这表明我所做的预处理和向量化很好 - 这是RF分类器特有的。我的第一反应是寻找过度配合,但即使我在培训数据上运行模型,它仍然几乎一切都错了。

我打得四处训练数据的树木数量和金额,但是这似乎并没有太大改变我。我试图只显示相关的代码,但可以发布更多,如果这是有帮助的。首先SO贴,所有的想法和反馈表示赞赏。

#pull in package to create word occurence vectors for each line 
from sklearn.feature_extraction.text import CountVectorizer 
vectorizer = CountVectorizer(min_df=1,charset_error='ignore') 
X_train = vectorizer.fit_transform(train_file) 
#convert to dense array, the required input type for random forest classifier 
X_train = X_train.todense() 

#pull in random forest classifier and train on data 
from sklearn.ensemble import RandomForestClassifier 
clf = RandomForestClassifier(n_estimators = 100, compute_importances=True) 
clf = clf.fit(X_train, train_targets) 

#transform the test data into the vector format 
testdata = vectorizer.transform(test_file) 
testdata = testdata.todense() 


#export 
with open('output.csv', 'wb') as csvfile: 
    spamwriter = csv.writer(csvfile) 
    for item in clf.predict(testdata): 
     spamwriter.writerow([item]) 
+0

你喂养你的电子表格train_file在这一行矢量:X_train = vectorizer.fit_transform(train_file)?如果是这种情况,那么你的矢量化对待你的文件作为一个单一的文本文件,并计算该文件中的单词。如果你有你的输入作为电子表格,那么你不应该使用CountVectorizer,因为你已经有你的字数,你应该阅读该电子表格到矩阵 – Shahram

+0

@Shahram,X_train本质上是一个字符串列表,其中每个字符串包含所有电子表格中特定行中的单词。字符串1包含第1行中的单词,依此类推。所以我认为矢量化是创造单词出现的必要条件。我查看了CountVectorizer的输出结果,我认为这部分是正确的。不过我会澄清的。 – DanT

回答

1

如果与随机森林(RF),你变得如此糟糕设定X_train训练,然后东西肯定是不对的,因为你应该得到一个巨大的比例,在90%以上。 (第一个代码段) 尝试以下操作:

print "K-means" 
clf = KMeans(n_clusters=len(train_targets), n_init=1000, n_jobs=2) 

print "Gaussian Mixtures: full covariance" 
covar_type = 'full' # 'spherical', 'diag', 'tied', 'full'  
clf = GMM(n_components=len(train_targets), covariance_type=covar_type, init_params='wc', n_iter=10000) 

print "VBGMM: full covariance" 
covar_type = 'full' # 'spherical', 'diag', 'tied', 'full'  
clf = VBGMM(n_components=len(train_targets), covariance_type=covar_type, alpha=1.0, random_state=None, thresh=0.01, verbose=False, min_covar=None, n_iter=1000000, params='wc', init_params='wc') 

print "Random Forest" 
clf = RandomForestClassifier(n_estimators=400, criterion='entropy', n_jobs=2) 

print "MultiNomial Logistic Regression" 
clf = LogisticRegression(penalty='l2', dual=False, C=1.0, fit_intercept=True, intercept_scaling=1, tol=0.0001) 

print "SVM: Gaussian Kernel, infty iterations" 
clf = SVC(C=1.0, kernel='rbf', degree=3, gamma=3.0, coef0=1.0, shrinking=True, 
probability=False, tol=0.001, cache_size=200, class_weight=None, 
verbose=False, max_iter=-1, random_state=None) 
  1. 不同的分类,在SCI-KIT接口学习基本上是始终不变的,看看他们的行为(也许RF是不是真的是最好的)。见上面
  2. 代码尝试创建一些随机生成的数据集,送给RF分类,我强烈怀疑不顺心的事在生成vectorizer对象的映射过程。因此,开始创建您的X_train并参阅。

希望帮助