Question
python with random forest,svm,etc I'm trying to run this code from this link http://machinelearner.net/nb.html but the problem it gives me this type of error: TypeError:
python with random forest,svm,etc I'm trying to run this code from this link http://machinelearner.net/nb.html but the problem it gives me this type of error:
TypeError: __init__() got an unexpected keyword argument 'indices'
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)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started