World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
Download our e-book of Introduction To Python
4.5 (1,292 Ratings)
559 Learners
Sneha Bose
2 years ago
# Importing libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# dataset path link
data_path = “https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data”
# Providing dataset headers
headers = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'Class']
#Loading and printing dataframe
dataset = pd.read_csv(data_path, names = headers)
dataset.head()
# Assigning independent and dependent variables (target variable)
X = dataset.iloc[:, :-1].values #(independent variable)
y = dataset.iloc[:, 4].values #(dependent variable)
#splitting dataset into the ratio of 80:20
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)
# Training the Random forest model
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(n_estimators = 50)
classifier.fit(X_train, y_train)
#Making predictions
y_pred = classifier.predict(X_test)
# Printing the result
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
result = confusion_matrix(y_test, y_pred)
print(“confusion Matrix:”)
result1 = classification_report(y_test, y_pred)
print(“Classification Report:”)
print(result1)
result2 = accuracy_score(y_test, y_pred)
print(“Accuray:”, result2)
Confusion Matrix:
[[14 0 0]
[ 0 18 1]
[ 0 0 12]]
Classification Report:
precision recall f1-score support
Iris-setosa 1.00 1.00 1.00 14
Iris-versicolor 1.00 0.95 0.97 19
Iris-virginica 0.92 1.00 0.96 12
micro avg 0.98 0.98 0.98 45
macro avg 0.97 0.98 0.98 45
weighted avg 0.98 0.98 0.98 45
Accuracy: 0.9777777777777777