+ 5

Machine Learning - A Forest of Trees

Build a Random Forest model. Task You will be given a feature matrix X and target array y. Your task is to split the data into training and test sets, build a Random Forest model with the training set, and make predictions for the test set. Give the random forest 5 trees. You will be given an integer to be used as the random state. Make sure to use it in both the train test split and the Random Forest model. Input Format First line: integer (random state to use) Second line: integer (number of datapoints) Next n lines: Values of the row in the feature matrix, separated by spaces Last line: Target values separated by spaces Output Format Numpy array of 1's and 0's Sample Input 1 10 -1.53 -2.86 -4.42 0.71 -1.55 1.04 -0.6 -2.01 -3.43 1.5 1.45 -1.15 -1.6 -1.52 0.79 0.55 1.37 -0.23 1.23 1.72 0 1 1 0 1 0 0 1 0 1 Sample Output [1 0 0]

4th Feb 2022, 6:53 AM
Avrian Shandy
Avrian Shandy - avatar
17 Réponses
+ 4
this is enough to answer all of them 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) model=RandomForestClassifier(n_estimators=5,random_state=random_state) model.fit(x_train,y_train) print(model.predict(x_test))
4th Feb 2022, 3:04 PM
Avrian Shandy
Avrian Shandy - avatar
+ 3
but the error starts from here from sklearn.ensemble import
4th Feb 2022, 12:20 PM
Avrian Shandy
Avrian Shandy - avatar
+ 2
It's my code 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=randomState) rf = RandomForestClassifier(n_estimators=5,random_state=randomState) rf.fit(X_train,y_train) print(rf.predict(X_test))
4th Feb 2022, 9:40 AM
Avrian Shandy
Avrian Shandy - avatar
+ 2
i still find error in other part, i have tried to fix it, but the result is still the same, can you write your full code, so i can understand it better
4th Feb 2022, 1:10 PM
Avrian Shandy
Avrian Shandy - avatar
+ 2
the result is still wrong, I've replaced it with a string with sign (" ") and (' ')
4th Feb 2022, 1:35 PM
Avrian Shandy
Avrian Shandy - avatar
+ 1
I've found the problem, and it's worked, I'm very happy to be able to discuss with you, thank you for your time
4th Feb 2022, 3:02 PM
Avrian Shandy
Avrian Shandy - avatar
+ 1
Problem are not sure
5th Feb 2022, 10:06 AM
Ranjan Yadav
+ 1
Thank you for the highlight.
5th Feb 2022, 3:17 PM
Random GBA Player
Random GBA Player - avatar
0
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=random_state) rf = RandomForestClassifier(n_estimators=5, random_state=random_state) rf.fit(X_train, y_train) print(rf.predict(X_test))
31st Jul 2022, 3:06 PM
Ernesto Avila Domenech
Ernesto Avila Domenech - avatar
0
Here is my answer: 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()]) # setting the random_state from input() X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=random_state) # setting the number of trees we want from input() rf = RandomForestClassifier(n_estimators=n) rf.fit(X_train, y_train) # printing the result print(rf.predict(X_test))
19th Jan 2023, 7:07 PM
Plamen Petkov
Plamen Petkov - avatar