0% found this document useful (0 votes)
23 views

RF - Classifier - Ipynb - Colaboratory

This document loads training and test CSV data, splits the data into features and labels, trains a random forest classifier on the training data, predicts labels for the test data, calculates the accuracy, and generates partial dependence plots for specific features in the training and test data to visualize the relationships learned by the random forest model.

Uploaded by

GARGI SHARMA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

RF - Classifier - Ipynb - Colaboratory

This document loads training and test CSV data, splits the data into features and labels, trains a random forest classifier on the training data, predicts labels for the test data, calculates the accuracy, and generates partial dependence plots for specific features in the training and test data to visualize the relationships learned by the random forest model.

Uploaded by

GARGI SHARMA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1/27/22, 11:54 PM RF_Classifier.

ipynb - Colaboratory

import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
from sklearn.inspection import PartialDependenceDisplay
import matplotlib.pyplot as plt
# from sklearn.ensemble import partial_dependence
# from sklearn.ensemble.partial_dependence import partial_dependence, plot_partial_depende

df_train = pd.read_csv("/content/Big Faults nos_25.0-75.0_train.csv")
df_test = pd.read_csv("/content/Big Faults nos_25.0-75.0_test.csv")

X_train = df_train.iloc[:, 1:]
Y_train = df_train.iloc[:, :1]
X_test = df_test.iloc[:, 1:]
Y_test = df_test.iloc[:, :1]

clf=RandomForestClassifier(n_estimators=30)

clf.fit(X_train,Y_train)

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:1: DataConversionWarning
"""Entry point for launching an IPython kernel.

RandomForestClassifier(n_estimators=30)

y_pred=clf.predict(X_test)

print("Accuracy:",metrics.accuracy_score(Y_test, y_pred))

Accuracy: 0.46788990825688076

features = [10, 11, 12]

Partial Dependence Plots: Train set

fig, ax = plt.subplots(figsize=(24, 6))

ax.set_title("RF Classifier")

tree_disp = PartialDependenceDisplay.from_estimator(clf, X_train, [10, 11, 12], ax=ax)

https://colab.research.google.com/drive/1wKRM8dfCKhA7xFLWgOmTsEcc6pBOIE2O?authuser=3#scrollTo=oeTqwTj3kr_8&printMode=true 1/3
1/27/22, 11:54 PM RF_Classifier.ipynb - Colaboratory

Partial Dependence Plots: Test set

fig, ax = plt.subplots(figsize=(24, 6))

ax.set_title("RF Classifier")

tree_disp = PartialDependenceDisplay.from_estimator(clf, X_test, [10, 11, 12], ax=ax)

https://colab.research.google.com/drive/1wKRM8dfCKhA7xFLWgOmTsEcc6pBOIE2O?authuser=3#scrollTo=oeTqwTj3kr_8&printMode=true 2/3
1/27/22, 11:54 PM RF_Classifier.ipynb - Colaboratory

check 2s completed at 11:53 PM

https://colab.research.google.com/drive/1wKRM8dfCKhA7xFLWgOmTsEcc6pBOIE2O?authuser=3#scrollTo=oeTqwTj3kr_8&printMode=true 3/3

You might also like