Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用**kwargs(Scikit学习)设置n_估计器参数_Python_Machine Learning_Scikit Learn - Fatal编程技术网

Python 使用**kwargs(Scikit学习)设置n_估计器参数

Python 使用**kwargs(Scikit学习)设置n_估计器参数,python,machine-learning,scikit-learn,Python,Machine Learning,Scikit Learn,我试图按照教程学习基于机器学习的预测,但我有两个问题 问题1。如何在下面的代码段中设置n_估计器,否则它将始终采用默认值 from sklearn.cross_validation import KFold def run_cv(X,y,clf_class,**kwargs): # Construct a kfolds object kf = KFold(len(y),n_folds=5,shuffle=True) y_pred = y.copy() # Iterate through fo

我试图按照教程学习基于机器学习的预测,但我有两个问题

问题1。如何在下面的代码段中设置
n_估计器
,否则它将始终采用默认值

from sklearn.cross_validation import KFold

def run_cv(X,y,clf_class,**kwargs):
# Construct a kfolds object
kf = KFold(len(y),n_folds=5,shuffle=True)
y_pred = y.copy()

# Iterate through folds
for train_index, test_index in kf:
    X_train, X_test = X[train_index], X[test_index]
    y_train = y[train_index]
    # Initialize a classifier with key word arguments
    clf = clf_class(**kwargs)
    clf.fit(X_train,y_train)
    y_pred[test_index] = clf.predict(X_test)
return y_pred
它被称为:

从sklearn.svm导入SVC
打印“%.3f%”精度(y,运行曲线(X,y,SVC))


问题2:如何使用已经训练过的模型文件(例如从SVM获得的),以便我可以使用它来预测更多我没有用于训练的(测试)数据?

对于第一个问题,在上面的代码中,您可以调用
run\u cv(X,y,SVC,n\u classifiers=100)
**kwargs
将通过步骤
clf=clf_class(**kwargs)
将其传递给分类器初始值设定项

对于第二个问题,您链接的代码中的交叉验证仅用于模型评估,即比较不同类型的模型和超参数,并确定模型在生产中的可能有效性。确定模型后,需要在整个数据集上重新安装模型:

clf.fit(X,y)
然后您可以使用
clf.predict
clf.predict\u proba
进行预测