+ 1

A Forest of Trees Run

I was solving sololearn machine learning random forest project. But I am stuck and I cannot figure out the reason my code is failing. I have passe both the test cases, but I cannot access the hidden test cases. Here is the code I tried... import numpy as np import pandas as pd from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split random_state = int(input()) n = int(input()) rows = [] for i in range(n): rows.append([float(a) for a in input().split()]) X = np.array(rows) y = np.array([int(a) for a in input().split()]) X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=n) rf = RandomForestClassifier(n_estimators=5, random_state=n) rf.fit(X_train, y_train) result = rf.predict(X_test) print(result)

25th Feb 2021, 3:01 PM
Nehul Patel
Nehul Patel - avatar
4 odpowiedzi
+ 5
in your model you use random_state=n, while random_state were supposed to be random_state
1st Mar 2021, 1:52 AM
Satrio Bayu Pradhipta
Satrio Bayu Pradhipta - avatar
+ 3
ANS : import numpy as np from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split random_state = int(input()) n = int(input()) rows = [] for i in range(n): rows.append([float(a) for a in input().split()]) X = np.array(rows) y = np.array([int(a) for a in input().split()]) x_train, x_test, y_train, y_test = train_test_split(X,y,random_state=random_state) #print(x_train,x_test,y_train,y_test,sep='\n \n') model = RandomForestClassifier(n_estimators=5,random_state=random_state) model.fit(x_train,y_train) pred =model.predict(x_test) print(pred)
25th Jul 2022, 11:41 AM
Reza Zeraat Kar
Reza Zeraat Kar - avatar
+ 2
# Author: Abdullah Abdelhakeem Amer# # Date : 1/3/2021 # # version : v01 # import numpy as np import pandas as pd from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split randomState = int(input()) n = int(input()) rows = [] for i in range(n): rows.append([float(a) for a in input().split()]) X = np.array(rows) y = np.array([int(a) for a in input().split()]) X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=randomState) rf = RandomForestClassifier(n_estimators=5,random_state=randomState) rf.fit(X_train,y_train) print(rf.predict(X_test))
1st Mar 2021, 11:08 AM
Abdullah Abdelhakeem
Abdullah Abdelhakeem - avatar
0
USE THIS CODE TO GET ALL TEST CASES RIGHT: SOLUTION!! import numpy as np from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier random_state = int(input()) n = int(input()) rows = [] for i in range(n): rows.append([float(a) for a in input().split()]) X = np.array(rows) y = np.array([int(a) for a in input().split()]) X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=random_state) rf = RandomForestClassifier(n_estimators=5,random_state=random_state) rf.fit(X_train,y_train) print(rf.predict(X_test))
2nd Nov 2022, 9:49 PM
Adanya William Eyram Kofi