Sync local Space with Hub
Browse files- prediction_helper.py +31 -0
prediction_helper.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
import numpy as np
|
| 3 |
+
from sklearn import datasets
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
iris_k_mean_model=pickle.load(open('model.sav', 'rb'))
|
| 7 |
+
classes=['versicolor', 'setosa' , 'virginica']
|
| 8 |
+
|
| 9 |
+
iris = datasets.load_iris()
|
| 10 |
+
x = pd.DataFrame(iris.data, columns=['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width'])
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def predict_class_way1(new_data_point):
|
| 14 |
+
# Calculate the Euclidean distances between the new data point and each of the training data points.
|
| 15 |
+
distances = np.linalg.norm(x - new_data_point, axis=1)
|
| 16 |
+
# print(distances,len(distances),np.argmin(distances))
|
| 17 |
+
|
| 18 |
+
# The data point with the minimum Euclidean distance is the class of the new data point.
|
| 19 |
+
class_label = classes[iris_k_mean_model.labels_[np.argmin(distances)]]
|
| 20 |
+
|
| 21 |
+
return class_label
|
| 22 |
+
|
| 23 |
+
def predict_class_way2(new_data_point):
|
| 24 |
+
# Calculate the distances between the new data point and each of the cluster centers.
|
| 25 |
+
distances = np.linalg.norm(iris_k_mean_model.cluster_centers_ - new_data_point, axis=1)
|
| 26 |
+
# print(distances,len(distances),np.argmin(distances))
|
| 27 |
+
|
| 28 |
+
# The data point with the minimum Euclidean distance is the class of the new data point.
|
| 29 |
+
class_label = classes[np.argmin(distances)]
|
| 30 |
+
|
| 31 |
+
return class_label
|