在 python 的随机森林、非线性 SVC 和多项式 NB 的每个 运行 上获得不同的准确度以进行文本分类

Getting different accuracy on each run of Random Forest, Non-Linear SVC and Multinomial NB in python for text classification

我正在 python 中处理二进制文本分类问题,并开发了随机森林、非线性 SVC 和多项式 NB 中的模型。

但是在这些各自模型的每个 运行 上,我在测试集上得到不同的精度和混淆矩阵参数。我在 train_test_split 中使用了 random_state 参数,并在初始化这些模型中的每一个时。 Random.Seed也在代码中加入

还有什么我想念的吗?

谢谢。

代码示例:

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.15, stratify= Y, random_state = 42) 

tfidf_vectorizer = TfidfVectorizer(analyzer='word', stop_words = 'english', max_df = 0.8, min_df = 0.05, ngram_range=(1,3)) 
tfidf_train = tfidf_vectorizer.fit_transform(X_train) 
tfidf_test = tfidf_vectorizer.transform(X_test) #Default Hyperparameters 

rfc = RandomForestClassifier(random_state = 42) 

rfc.fit(tfidf_train,Y_train) 
predictions = rfc.predict(tfidf_test) 

score = metrics.accuracy_score(Y_test, predictions) # get scores

print("accuracy: %0.3f" % score) #printing score

您使用的某些实用程序可能包含一些隐藏的随机操作、不确定性。

由于某些库使用 numpy.random() instead of random.random(),您应该使用 numpy.random.seed()