Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views3 pages

LastQ PredictedCLasses

The document describes a machine learning workflow using the Iris dataset with Support Vector Machines (SVM) and K-Means clustering. It demonstrates training an SVM model, achieving an accuracy of approximately 95.56%, and predicting the class of a new instance. Additionally, it includes a clustering analysis using MiniBatchKMeans and visualizations of the results.

Uploaded by

160421737033
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)
17 views3 pages

LastQ PredictedCLasses

The document describes a machine learning workflow using the Iris dataset with Support Vector Machines (SVM) and K-Means clustering. It demonstrates training an SVM model, achieving an accuracy of approximately 95.56%, and predicting the class of a new instance. Additionally, it includes a clustering analysis using MiniBatchKMeans and visualizations of the results.

Uploaded by

160421737033
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/ 3

In [1]:

from sklearn import datasets


from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
import numpy as np
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
iris=datasets.load_iris()
x=iris.data
y=iris.target
x_train, x_test, y_train, y_test=train_test_split(x, y, test_size=0.3, random_state=33)
svm=SVC(kernel='linear')
svm.fit(x_train, y_train)
y_pred=svm.predict(x_test)
accuracy=accuracy_score(y_test, y_pred)
print("Accuracy", accuracy)
plt.scatter(x_train[:,0], x_train[:,1], c=y_train)
plt.scatter(svm.support_vectors_[:,0], svm.support_vectors_[:,1], color='red')
plt.xlabel('sepal length')
plt.ylabel('sepal width')
plt.title('Support Vector Machine')
plt.show()

Accuracy 0.9555555555555556

In [4]:

from sklearn import datasets


from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
import numpy as np
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
iris=datasets.load_iris()
x=iris.data
y=iris.target
x_train, x_test, y_train, y_test=train_test_split(x, y, test_size=0.3, random_state=33)
svm=SVC(kernel='linear')
svm.fit(x_train, y_train)
y_pred=svm.predict(x_test)
accuracy=accuracy_score(y_test, y_pred)
print("Accuracy", accuracy)
plt.scatter(x_train[:,0], x_train[:,1], c=y_train)
plt.scatter(svm.support_vectors_[:,0], svm.support_vectors_[:,1], color='red')
plt.xlabel('sepal length')
plt.ylabel('sepal width')
plt.title('Support Vector Machine')
plt.show()

new_instance = [[4.1, 0.5, 5.4, 0.2]]


pred_class=svm.predict(new_instance)
pred_class_name=iris.target_names[int(pred_class)]
print("Pred class is", pred_class_name)

Accuracy 0.9555555555555556

Pred class is virginica

/tmp/ipykernel_30/3488574794.py:26: DeprecationWarning: Conversion of an array with ndim


> 0 to a scalar is deprecated, and will error in future. Ensure you extract a single elem
ent from your array before performing this operation. (Deprecated NumPy 1.25.)
pred_class_name=iris.target_names[int(pred_class)]

In [6]:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.preprocessing import MinMaxScaler
from sklearn.decomposition import PCA
from sklearn.cluster import MiniBatchKMeans

iris=load_iris()
x=iris.data
y=iris.target

scaler=MinMaxScaler()
x_scaled=scaler.fit_transform(x)

pca=PCA(n_components=2)
x_pca=pca.fit_transform(x_scaled)

som=MiniBatchKMeans(n_clusters=3)
som.fit(x_scaled)
y_pred=som.labels_

plt.scatter(x_pca[:,0], x_pca[:,1], c=y_pred)


plt.title("Self Organizing FM")
plt.xlabel("PC1")
plt.ylabel("PC2")
plt.show()

/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: Th
e default value of `n_init` will change from 3 to 'auto' in 1.4. Set the value of `n_init
` explicitly to suppress the warning
warnings.warn(

In [ ]:

You might also like