Setting the n_estimators argument using **kwargs (Scikit Learn)
<p>I am trying to follow <a href="http://blog.yhat.com/posts/predicting-customer-churn-with-sklearn.html" rel="nofollow">this</a> tutorial to learn the machine learning based prediction but I have got two questions on it?</p>

<p>Ques1. How to set the <code>n_estimators</code> in the below piece of code, otherwise it will always assume the default value.</p>

<pre><code>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
</code></pre>

<p>It is being called as:</p>

<p><code>from sklearn.svm import SVC
 print "%.3f" % accuracy(y, run_cv(X,y,SVC))</code></p>

<p>Ques2: How to use the already trained model file (e.g. obtained from SVM) so that I can use it to predict more (test) data which I didn't used for training?</p>

<p>For your first question, in the above code you would call <code>run_cv(X,y,SVC,n_classifiers=100)</code>, the <code>**kwargs</code> will pass this to the classifier initializer with the step <code>clf = clf_class(**kwargs)</code>.</p>

<p>For your second question, the cross validation in the code you've linked is just for model evaluation, i.e. comparing different types of models and hyperparameters, and determining the likely effectiveness of your model in production. Once you've decided on your model, you need to refit the model on the whole dataset: </p>

<pre><code>clf.fit(X,y)
</code></pre>

<p>Then you can get predictions with <code>clf.predict</code> or <code>clf.predict_proba</code>.</p>

python machine-learning scikit-learn