ML Project – Digits Image Classification using Random Forest Algorithm

Machine Learning courses with 100+ Real-time projects Start Now!!

Program 1

import pandas as pd
from sklearn.datasets import load_digits
import matplotlib.pyplot as plt

digits=load_digits()

digits

dir(digits)

digits.target

digits.data

plt.gray()
plt.matshow(digits.images[4])
plt.show()

for i in range(5):
    plt.matshow(digits.images[i])
    

digits.target

digits.target[:4]

# Data Frame 
df=pd.DataFrame(digits.data)

df.head()

df['target']=digits.target

df.head()

# Depended and Independed variables
x=df.drop('target',axis='columns')

x

y=df.target

y

len(x)

len(y)

# Split Data into training and testing
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(digits.data,digits.target,test_size=0.2)


len(x_train)

len(y_train)

len(x_test)

len(y_test)

from sklearn.ensemble import RandomForestClassifier
model=RandomForestClassifier(n_estimators=50)
model.fit(x_train,y_train)
model.score(x_test,y_test)


y_pred=model.predict(x_test)

y_pred


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

import seaborn as se
plt.figure(figsize=(10,7))
se.heatmap(cm,annot=True)
plt.xlabel('Predication')
plt.ylabel('Truth')

 

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *