0% found this document useful (0 votes)
113 views2 pages

5 Random Forest - Jupyter Notebook

This document loads and prepares iris data from a CSV file. It then splits the data into training and test sets, divides the inputs and outputs, and fits a random forest classifier to the training set. It uses the trained model to predict the test set and calculates performance metrics like the confusion matrix and accuracy score. Key steps include: 1) Loading iris data 2) Splitting into train and test 3) Dividing inputs and outputs for each set 4) Training a random forest classifier on the training set 5) Predicting the test set and calculating performance.

Uploaded by

venkatesh m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views2 pages

5 Random Forest - Jupyter Notebook

This document loads and prepares iris data from a CSV file. It then splits the data into training and test sets, divides the inputs and outputs, and fits a random forest classifier to the training set. It uses the trained model to predict the test set and calculates performance metrics like the confusion matrix and accuracy score. Key steps include: 1) Loading iris data 2) Splitting into train and test 3) Dividing inputs and outputs for each set 4) Training a random forest classifier on the training set 5) Predicting the test set and calculating performance.

Uploaded by

venkatesh m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

In 

[1]: import numpy as np


import matplotlib.pyplot as plt
import pandas as pd

In [2]: iris = pd.read_csv("D:\\Course\\Python\\Datasets\\iris.csv")


iris

...

In [3]: iris_setosa = iris.iloc[0:50,0:5]


iris_versicolor = iris.iloc[50:100,0:5]
iris_vergenica = iris.iloc[100:150,0:5]

In [4]: df1 = pd.DataFrame(iris_setosa.iloc[0:25,0:5])


df2 = pd.DataFrame(iris_versicolor.iloc[0:25,0:5])
df3 = pd.DataFrame(iris_vergenica.iloc[0:25,0:5])

In [5]: iris_train = pd.concat([df1,df2,df3])


iris_train

...

In [7]: # Dividing the input and output variable for Training Dataset

X_train = iris_train.iloc[:,0:4]
X_train
y_train = iris_train.iloc[:,4]
y_train

...

In [8]: df1 = pd.DataFrame(iris_setosa.iloc[25:50,0:5])


df2 = pd.DataFrame(iris_versicolor.iloc[25:50,0:5])
df3 = pd.DataFrame(iris_vergenica.iloc[25:50,0:5])

In [9]: iris_test= pd.concat([df1,df2,df3])

In [10]: X_test = iris_test.iloc[:,0:4]


X_test
y_test = iris_test.iloc[:,4]
y_test

...

In [10]: # X_train and X_test are the dataset for training and Testing
#y_train ,y_test are target predictor for Training and Testing Dataset

In [ ]: X_train,y_train,X_test,y_test
In [11]: # Fitting Random Forest Classification to the Training set
# 1 : Import required libraries and Method for RF

from sklearn.ensemble import RandomForestClassifier

# 2 : create an alogrithm using imported method name

classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy')

# Train the model - Training Data (xtrain, ytrain)

classifier.fit(X_train, y_train)
...

In [12]: # Predicting the Test set results - only on input varabiles of dataset

y_pred = classifier.predict(X_test)

In [13]: # Making the Confusion Matrix


from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)

In [14]: print(classifier.feature_importances_)

[0.06800539 0.02570351 0.45203587 0.45425523]

In [15]: feat_importances = pd.Series(classifier.feature_importances_)


feat_importances.nlargest(10).plot(kind='barh')
plt.show()

...

In [16]: cm

...

In [17]: from sklearn.metrics import accuracy_score


Accuracy_Score = accuracy_score(y_test, y_pred)

In [18]: Accuracy_Score
...

In [ ]: ​

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy