我正在使用Python处理一个二元文本分类问题,并已经开发了基于随机森林、非线性SVC和多项式朴素贝叶斯的模型。
但是,每次运行这些模型时,在测试集上得到的准确率和混淆矩阵参数都不同。我在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 scoresprint("accuracy: %0.3f" % score) #printing score
回答:
您使用的某些工具可能包含一些隐藏的随机操作,导致不确定性。
由于一些库使用的是numpy.random()而不是random.random(),您应该使用numpy.random.seed()
。