Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

this is site code: #computer 1 from sklearn.cross_validation import KFold # R: U

ID: 3785502 • Letter: T

Question

this is site code:

#computer 1
from sklearn.cross_validation import KFold
# R: Use library(cvFold)

# A very similar routine can be written in R using cvFold
def cross_validate(model):
    cv = KFold(len(y), n_folds=10, indices=True, shuffle=False, random_state=None, k=None)

    sum_accuracy = 0
    fold = 0
    for traincv, testcv in cv:
        result = model.fit(X[traincv], y[traincv])
        p = model.predict(X[testcv])
        accuracy = sum(p==y[testcv])/float(len(y[testcv]))
        fold = fold + 1
        print "Fold accuracy ",fold,":",accuracy
        sum_accuracy = sum_accuracy + accuracy


    print 'Overall CV Accuracy:',sum_accuracy/fold

#computer 2
from sklearn.ensemble import RandomForestClassifier
# R: This is the equivalent of library(randomForest) in R

# Evaluate Random Forest
rfc = RandomForestClassifier()
cross_validate(rfc)

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

I tried to fix it used new library but the problem it did not show the accuracy for 10 fold cross valdiation for random forst and SVM.

the code has no error but the result did not show.

How can I show the result in this case?

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

from sklearn import cross_validation
#from sklearn.cross_validation import KFold
#from sklearn import cross_validation
# R: Use library(cvFold)

# A very similar routine can be written in R using cvFold
def cross_validate(model):
    num_folds = 10
    num_instances = len(y)
    seed = None
    #cv = KFold(len(y), n_folds=10, indices=False, shuffle=False, random_state=None, k=None)
    cv = cross_validation.KFold(n=num_instances, n_folds=num_folds, random_state=seed)

    sum_accuracy = 0
    fold = 0
    for traincv, testcv in cv:
        result = model.fit(X[traincv], y[traincv])
        p = model.predict(X[testcv])
        accuracy = sum(p==y[testcv])/float(len(y[testcv]))
        fold = fold + 1
        print "Fold accuracy ",fold,":",accuracy
        sum_accuracy = sum_accuracy + accuracy


    print 'Overall CV Accuracy:',sum_accuracy/fold

# the problem in this code the result did not show . it should show the acuraccy for 10 fold cross validation

from sklearn.svm import SVC
# R: This is the equivalent of library(e1071) in R

# Evaluate SVM
svmc = SVC()
cross_validate(svmc)

from sklearn.ensemble import RandomForestClassifier
# R: This is the equivalent of library(randomForest) in R

# Evaluate Random Forest
rfc = RandomForestClassifier()
cross_validate(rfc)

Explanation / Answer

You need to use the api call to use the CV for best model selection.: Find below one such code snippet:

In this code, by default 3 Fold CV was done. You could also pass