9951 explained code solutions for 126 technologies


python-scipyHow can I use Python and SciPy to create a random forest model?


To create a random forest model using Python and SciPy, you can use the RandomForestClassifier class from the Scikit-Learn library. This class provides a simple API for creating and training a random forest model.

Example code

# import RandomForestClassifier from Scikit-Learn
from sklearn.ensemble import RandomForestClassifier

# create a RandomForestClassifier object
clf = RandomForestClassifier()

# fit the model to the training data
clf.fit(X_train, y_train)

# make predictions on the test data
predictions = clf.predict(X_test)

# print the accuracy of the model
print("Accuracy:", clf.score(X_test, y_test))

Output example

Accuracy: 0.964

Code explanation

  1. Import the RandomForestClassifier class from Scikit-Learn: from sklearn.ensemble import RandomForestClassifier
  2. Create a RandomForestClassifier object: clf = RandomForestClassifier()
  3. Fit the model to the training data: clf.fit(X_train, y_train)
  4. Make predictions on the test data: predictions = clf.predict(X_test)
  5. Print the accuracy of the model: print("Accuracy:", clf.score(X_test, y_test))

Helpful links

Edit this code on GitHub