1from sklearn.ensemble import RandomForestRegressor
2
3
4clf = RandomForestRegressor(max_depth=2, random_state=0)
5
6clf.fit(X, y)
7
8print(clf.predict([[0, 0, 0, 0]]))
1from sklearn.ensemble import RandomForestClassifier
2
3
4clf = RandomForestClassifier(max_depth=2, random_state=0)
5
6clf.fit(X, y)
7
8print(clf.predict([[0, 0, 0, 0]]))
1from sklearn.ensemble import RandomForestRegressor
2
3regressor = RandomForestRegressor(n_estimators=20, random_state=0)
4regressor.fit(X_train, y_train)
5y_pred = regressor.predict(X_test)
6
1from sklearn.ensemble import RandomForestClassifier
2
3y = train_data["Survived"]
4
5features = ["Pclass", "Sex", "SibSp", "Parch","Fare","Age"]
6X = pd.get_dummies(train_data[features])
7X_test = pd.get_dummies(test_data[features])
8
9model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
10model.fit(X, y)
11predictions = model.predict(X_test)
1from sklearn.model_selection import train_test_split
2
3X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
4