ML Project – Iris Flower Prediction using Random Forest Algorithm

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

Program 1

Iris Dataset

import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.ensemble import RandomForestClassifier

df=pd.read_csv("D://scikit_data/IrisData/Iris.csv")

df.head()

df.shape

df.info()

df.isnull().sum()

df=df.drop('Id',axis='columns')

df.head()

le=LabelEncoder()
df['Type_new']=le.fit_transform(df['Type'])

df.head()

df

df=df.drop('Type',axis='columns')

df.head()

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

x

# Depended variables
y=df.Type_new

y

len(x)

len(y)

# Split Data set into training and testing
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)

len(x_train)

len(x_test)

len(y_train)

len(y_test)

# Prepare model Random Forest
model=RandomForestClassifier(n_estimators=50)

model.fit(x_train,y_train)

model.score(x_test,y_test)

y_pred_test=model.predict(x_test)

y_pred_test

cm=confusion_matrix(y_test,y_pred_test)
cm

y_pred_train=model.predict(x_train)

y_pred_train

model.score(x_train,y_train)

m=model.predict([[6.9,3.2,5.7,2.3]])
if(m==0):
    print('Iris-setosa')
elif(m==1):
    print('Iris-versicolor')
else:
    print('Iris-virginica')

 

Did we exceed your expectations?
If Yes, share your valuable 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 *