Write a python program to fit and predict based on the KMeans algorithm. 1. Take a random sample of 100 rows 2. Compute KMeans on the first two rows: s_length, s_width (sepal length and width) 3. Print out your predictions for k=5andk-7 4. for k = 3, predict the following os_length = 5.2, s_width = 3.8, os_length = 4.2, s_width = 2.2,
Pls solve this question correctly in 5 min i will give u like for sure
import seaborn as sns
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
from sklearn import datasets
# download the iris datafile
X,y = datasets.load_iris(return_X_y=True,)
cols = ['s_length','s_width','p_length','p_width']
df = pd.DataFrame(X, columns=cols)
df['class'] = y
df
sns.scatterplot(data=df, x='s_length', y='p_width', hue='class')
from sklearn.model_selection import train_test_split
train, test = train_test_split(df, test_size=0.4)
X = train[['s_length', 'p_width']].to_numpy()
y = train[['class']].to_numpy()
k = 3
model = KMeans(n_clusters=k)
model.fit_predict(X, y)
s = model.score(X,y)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images