python - 如何找到给出最佳分数的 train_test_split 的最佳 random_state 值?
问题描述
我对我的数据进行了训练测试拆分,并为其拟合了支持向量机。
xtrain, xtest, ytrain, ytest = train_test_split(df, target)
svc = svm.SVC(C=30, gamma='auto')
svc.fit(xtrain,ytrain)
svc.score(xtest,ytest)
我正在将SVC
模型拟合到 iris 数据集,并且每次运行都会得到不同的结果train_test_split
(这很明显)。
是否有 train test_test_split 的任何属性或函数或任何其他方式,以便在获得结果后(执行上述代码后)我可以找出random_state
我得到结果的值是什么?
解决方案
您可以运行自制的网格搜索来找到random_state
.
但是,您永远不应该针对随机性进行优化。通过这样做,您将找到最适合某些随机事件的模型,根据定义,这些随机事件与您的目标变量没有因果关系。
如果您真的想继续,那么您必须记录每个随机状态拆分的分数。
import numpy as np
import pandas as pd
# Array of random_state values from -100 to 100
random_states = np.arange(start=-100, stop=101)
# Initialize a list where we'll store the score of each random_state
scores = []
# Initialize search
for state in random_states:
xtrain, ytrain, xtest, ytest = train_test_split(df, target, random_state=state)
svc = svm.SVC(C=30, gamma='auto')
svc.fit(xtrain, ytrain)
scores.append(svc.score(xtest, ytest))
现在将这两个数组放入一个 pandas 数据框中。
results = pd.DataFrame({'random_state':random_states, 'score':scores})
results[results['score'] == results['score'].max()]