Python Forum

Full Version: How to find the accuracy for Random Forest
You're currently viewing a stripped down version of our content. View the full version with proper formatting.



May I know how to modify my Python programming so that can obtain the accuracy vs number of features as refer to the attached image file -







from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# load the data
iris = datasets.load_iris()
# get the features and labels from the data
x = iris.data
y = iris.target
# split the data into training and test data
X_train, X_test, y_train, y_test = train_test_split(x, y,test_size=0.7, random_state=0) 
# standardise the data
sc = StandardScaler()
X_train_std = sc.fit_transform(X_train)
X_test_std = sc.fit_transform(X_test)
# choose algorithm and set the hyperparameters
forest = RandomForestClassifier(criterion='entropy', n_estimators=10, random_state=1)
# train the model
forest.fit(X_train_std, y_train)
# make the prediction using the model
y_pred = forest.predict(X_test_std)

A = []
C1 = [forest]
for i in range(len(C)):
    forest = RandomForestClassifier(C=C1[i], random_state=0)
    forest.fit(X_train_std,y_train)
    y_pred = forest.predict(X_test_std)
    A.append(accuracy_score(y_test,y_pred))

import matplotlib.pyplot as plt
plt.plot(C1, A)
plt.yticks(np.arange(0.90, 0.95, 0.01))
plt.xlabel('Number of features')
plt.ylabel('Accuracy')
plt.title('RansomForest')
plt.show()


The error message is -





runfile('C:/Users/HSIPL/Desktop/Homework 7 Solution draft.py', wdir='C:/Users/HSIPL/Desktop')
Traceback (most recent call last):

  File "<ipython-input-10-f06d5471b604>", line 1, in <module>
    runfile('C:/Users/HSIPL/Desktop/Homework 7 Solution draft.py', wdir='C:/Users/HSIPL/Desktop')

  File "C:\Users\HSIPL\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "C:\Users\HSIPL\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/HSIPL/Desktop/Homework 7 Solution draft.py", line 28, in <module>
    forest = RandomForestClassifier(C=C1[i], random_state=0)

TypeError: __init__() got an unexpected keyword argument 'C'





Please see the attached image file -



[Image: XdDw3.jpg]




Please help me on this case






You are getting an error because RandomForestClassifier has no C parameter. It is unclear why you are trying to pass one to it. It appears the value you are trying to pass is another RandomForestClassifier. But RandomForestClassifier does not take other RandomForestClassifer's as a value for any of it's parameters. See the documentation.



How to write the correct code for the number of features