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
- Import the RandomForestClassifier class 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))
Helpful links
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How do I use Scipy zeros in Python?
- How to use Python, XML-RPC, and NumPy together?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How do I create a zero matrix using Python and Numpy?
- How can I use Scipy with Python?
- How do I create a numpy array of zeros using Python?
- How do I check the version of Python Numpy I am using?
- How do I use the NumPy transpose function in Python?
- How can I use Python Numpy to select elements from an array based on multiple conditions?
See more codes...