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

Kmeans

The document outlines the implementation of various machine learning classifiers using the Iris dataset, including SVC, Logistic Regression, Random Forest, and Decision Tree. Each classifier is trained and evaluated, achieving a perfect score of 1.0 on the test set. Additionally, cross-validation scores are calculated for each model, revealing high average accuracy across all classifiers.

Uploaded by

Kavya Padarthi
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)
5 views2 pages

Kmeans

The document outlines the implementation of various machine learning classifiers using the Iris dataset, including SVC, Logistic Regression, Random Forest, and Decision Tree. Each classifier is trained and evaluated, achieving a perfect score of 1.0 on the test set. Additionally, cross-validation scores are calculated for each model, revealing high average accuracy across all classifiers.

Uploaded by

Kavya Padarthi
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 [2]: from sklearn.

svm import SVC


from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
import numpy as np
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
iris = load_iris()

In [4]: from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2)

In [8]: svm = SVC()


svm.fit(X_train,y_train)
svm.score(X_test,y_test)

Out[8]: 1.0

In [6]: lr = LogisticRegression()
lr.fit(X_train,y_train)
lr.score(X_test,y_test)

Out[6]: 1.0

In [10]: rf = RandomForestClassifier()
rf.fit(X_train,y_train)
rf.score(X_test,y_test)

Out[10]: 1.0

In [12]: dt = DecisionTreeClassifier()
dt.fit(X_train,y_train)
dt.score(X_test,y_test)

Out[12]: 1.0

In [14]: from sklearn.model_selection import cross_val_score

In [24]: LR = cross_val_score(LogisticRegression(),iris.data,iris.target,cv=5)
LR

C:\Users\MyPc\anaconda3\Lib\site-packages\sklearn\linear_model\_logistic.py:469: ConvergenceWarning: lbfgs failed to converge (status=1):


STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.

Increase the number of iterations (max_iter) or scale the data as shown in:
https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
n_iter_i = _check_optimize_result(
Out[24]: array([0.96666667, 1. , 0.93333333, 0.96666667, 1. ])

In [26]: SC = cross_val_score(SVC(),iris.data,iris.target,cv=5)
SC

Out[26]: array([0.96666667, 0.96666667, 0.96666667, 0.93333333, 1. ])

In [28]: R = cross_val_score(RandomForestClassifier(),iris.data,iris.target,cv=5)
R

Out[28]: array([0.96666667, 0.96666667, 0.93333333, 0.93333333, 1. ])

In [30]: D = cross_val_score(DecisionTreeClassifier(),iris.data,iris.target,cv=5)
D

Out[30]: array([0.96666667, 0.96666667, 0.9 , 0.93333333, 1. ])

In [32]: np.average(LR)

Out[32]: 0.9733333333333334

In [35]: np.average(R)

Out[35]: 0.96

In [37]: np.average(D)

Out[37]: 0.9533333333333334

In [39]: np.average(SC)
Out[39]: 0.9666666666666666

In [ ]:

You might also like