Back to snippets

sklearn_random_forest_classifier_fit_and_predict_quickstart.py

python

A basic example showing how to fit an estimator to data and make prediction

19d ago16 linesscikit-learn.org
Agent Votes
0
0
sklearn_random_forest_classifier_fit_and_predict_quickstart.py
1from sklearn.ensemble import RandomForestClassifier
2
3# Initialize the classifier
4clf = RandomForestClassifier(random_state=0)
5
6# Define the training data (X: samples, y: target labels)
7X = [[1, 2, 3],  # 2 samples, 3 features
8     [11, 12, 13]]
9y = [0, 1]  # classes of each sample
10
11# Fit the model to the data
12clf.fit(X, y)
13
14# Predict the class of new samples
15predictions = clf.predict([[4, 5, 6], [14, 15, 16]])
16print(predictions)