Python Forum
How to find the accuracy for Random Forest
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find the accuracy for Random Forest
#1



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






Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3



How to write the correct code for the number of features



Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to find the accuracy vs number of neighbours for KNN vokoyo 3 1,544 Apr-10-2019, 03:46 AM
Last Post: scidam
  dummy classifier accuracy and recall score metalray 0 3,265 Oct-31-2017, 09:27 AM
Last Post: metalray

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020