Supervised Machine Learning With Python: Classification. Random Forest

Supervised Machine Learning With Python: Classification. Random Forest

The techniques known as ensemble methods are those that integrate machine learning models to create a more effective machine learning model. One of them is the decision tree collection known as Random Forest. Because it may reduce over-fitting by averaging the outcomes while maintaining the predictive abilities, it is superior to a single decision tree. The scikit learn cancer dataset will be used to create the random forest model in this case.

We begin by importing all necessary packages into our script instance:

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
import matplotlib.pyplot as plt
import numpy as np        

Next, we will load the dataset into system memory, and split the dataset into the training and testing sets:

cancer = load_breast_cancer()

X_train, X_test, y_train, y_test = train_test_split(cancer.data,
                                                    cancer.target,
                                                    random_state=0)        

We may thereafter proceed to instantiate an object of the RandomForestClassifier class:

algorithm = RandomForestClassifier(n_estimators=50, random_state=0)        

Next, we train the algorithm on the data to create a model:

model = algorithm.fit(X_train,y_train)        

Finally, we will evaluate the performance of our model by checking the accuracy it obtained against the training and testing dataset, respectively:

print('Training Accuracy:', (model.score(X_train,y_train)))
print('Testing Accuracy:', round((model.score(X_test,y_test)), 10))        

The output to the above block of code will show as follows:

Random Forests objects provide us with the feature importance sub-class, which will give users a better understanding of feature weights and importance than decision tree. The following can be plotted and visualized:

The image in the plots pane of my Spyder window displays as follows:


Rubab Bibi

Affiliate Marketing | Affiliate Marketing Manager | Digital Marketing | Product Marketing | Open To New Connections

1 年

I'll keep this in mind

要查看或添加评论,请登录

Shivek Maharaj的更多文章

社区洞察