Untitled

 avatar
user_9363972
python
7 months ago
2.7 kB
1
Indexable
Never
import pandas as pd
import seaborn as sb
import numpy as np


iris_data = pd.read_csv("iris.data")
colums_name = ['Sepal length', 'Sepal width', 'Petal length', 'Petal width', 'class'] 
iris_data.columns = colums_name 
# iris_data.head()

# iris_data.isnull().sum()
# iris_data.value_counts("class")

# sb.countplot(x="class", data = iris_data)


fig = sb.scatterplot(x="Sepal length", y="Sepal width", hue="class", data=iris_data)

fig.set(title="Sepal Length and Width")


features = iris_data.iloc[:, 0:4] 
label = iris_data.iloc[:, 4] 
print("Features : ") 
print(features) 
print("Label : ") 
print(label)

from sklearn.model_selection import train_test_split 
X_train, X_test, y_train, y_test = train_test_split( features, label, test_size=0.2, random_state=0) 
print('Number of train data' ,X_train.shape[0]) 
print('Number of data test', y_test.shape[0])

from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier(n_neighbors=8, weights="uniform")
knn.fit(X_train, y_train)

train_acc = knn.score(X_train, y_train)

knn_test_acc = knn.score(X_test, y_test)
print("The accuracy Of KNN classifier on training data is : {:.3f}".format(knn_test_acc))


from sklearn.metrics import ConfusionMatrixDisplay 
disp = ConfusionMatrixDisplay.from_estimator(knn,X_test ,y_test) 
disp.ax_.set_title("Confusion Matrix")




######################################!SECTION

from subprocess import check_output 
from sklearn import tree 
from sklearn.pipeline import make_pipeline 
from sklearn import preprocessing 
from sklearn.model_selection import ShuffleSplit 
from sklearn.model_selection import cross_val_score 
from sklearn.model_selection import GridSearchCV

data = pd.read_csv('iris.data') 
features = data.iloc[:, 0:4] 
label= data.iloc[:, 4]

from sklearn.neighbors import KNeighborsClassifier 


n_range = list(range(1,10)) 
weight_list = ['uniform','distance'] 
param_grid = {'n_neighbors': n_range, 'weights':weight_list} 
grid = GridSearchCV(estimator = KNeighborsClassifier(), param_grid = param_grid, cv = 3, scoring = 'accuracy', refit = True) 
knn_model = make_pipeline(preprocessing.StandardScaler(), grid) 
knn_model.fit(features, label) 
print('Accuracy of the tuned model: %.4f' %grid.best_score_ ) 
print(grid.best_params_)

import pickle

with open("knn_model.pickle","wb") as f:
    pickle.dump(knn_model,f)

with open("knn_model.pickle", "rb") as f:
    model = pickle.load(f)

new_data = [6.2,3.4,5.4,2.2]

test = np.array([new_data])

prediction = model.predict(test)

print("Prediction of Species: {}".format(prediction))