File size: 1,220 Bytes
15365f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import pickle
import numpy as np
from sklearn import datasets
import pandas as pd

iris_k_mean_model=pickle.load(open('model.sav', 'rb'))
classes=['versicolor', 'setosa' , 'virginica']

iris = datasets.load_iris()
x = pd.DataFrame(iris.data, columns=['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width'])


def predict_class_way1(new_data_point):
    # Calculate the Euclidean distances between the new data point and each of the training data points.
    distances = np.linalg.norm(x - new_data_point, axis=1)
    # print(distances,len(distances),np.argmin(distances))

    # The data point with the minimum Euclidean distance is the class of the new data point.
    class_label = classes[iris_k_mean_model.labels_[np.argmin(distances)]]

    return class_label

def predict_class_way2(new_data_point):
    # Calculate the distances between the new data point and each of the cluster centers.
    distances = np.linalg.norm(iris_k_mean_model.cluster_centers_ - new_data_point, axis=1)
    # print(distances,len(distances),np.argmin(distances))

    # The data point with the minimum Euclidean distance is the class of the new data point.
    class_label = classes[np.argmin(distances)]

    return class_label