from preprocess import retProcessedData
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score,GridSearchCV
from sklearn.metrics import confusion_matrix
import pickle as pl
import pandas as pd


data_path = "winequality-red.csv"
df=pd.read_csv(data_path)
data = retProcessedData(df)
print("Cols:\n",data.columns)
target = input("Enter the name of the column to use as the target: ")
try:
    X= df.drop([target],axis=1)
    y = df[target]
except:
    print("Target name not found.")
    exit(0)
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.33,random_state=42)



X_train_scaled=StandardScaler().fit_transform(X_train)
X_test_scaled=StandardScaler().fit_transform(X_test)
knn= KNeighborsClassifier()
knn_classifier=knn
knn_classifier.fit(X_train_scaled,y_train)
y_pred=knn_classifier.predict(X_test_scaled)
output="  \nKNN Classifier  \nBefore GridSearchCV  \n"
output+=str(confusion_matrix(y_test.values,y_pred))
output+="  \n  Train Accuracy: "+str(knn_classifier.score(X_train_scaled,y_train))+"  \n  Test Accuracy: "+str(knn_classifier.score(X_test_scaled,y_test))
grid_params={
    'n_neighbors':[3,5,7,9],
    'weights' :['uniform','distance'],
    'metric':['euclidean','manhattan','minkowski']
}
#n_jobs = -1 tells the computer to use all of it's processors to perform the model
knn_2 = KNeighborsClassifier()
clf = GridSearchCV(knn,grid_params,cv=3,n_jobs=-1)
best_classifier = clf.fit(X_train,y_train)
y_pred = best_classifier.predict(X_test_scaled)
output+="  \n  \nKNN Classifier  \nAfter GridSearchCV  \n"
output+=str(confusion_matrix(y_test,y_pred))
output+="  \n Train Accuracy: "+str(best_classifier.score(X_train,y_train))+"  \n  Test Accuracy: "+str(best_classifier.score(X_test,y_test))
print(output)
pl.dump(best_classifier,open("KNN_model", 'wb'))
print("The trained model has been saved in your local folder.\nThank you for using our tool\n")
from preprocess import retProcessedData
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score,GridSearchCV
from sklearn.metrics import confusion_matrix
import pickle as pl
import pandas as pd


data_path = "winequality-red.csv"
df=pd.read_csv(data_path)
data = retProcessedData(df)
print("Cols:\n",data.columns)
target = input("Enter the name of the column to use as the target: ")
try:
    X= df.drop([target],axis=1)
    y = df[target]
except:
    print("Target name not found.")
    exit(0)
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.33,random_state=42)



X_train_scaled=StandardScaler().fit_transform(X_train)
X_test_scaled=StandardScaler().fit_transform(X_test)
knn= KNeighborsClassifier()
knn_classifier=knn
knn_classifier.fit(X_train_scaled,y_train)
y_pred=knn_classifier.predict(X_test_scaled)
output="  \nKNN Classifier  \nBefore GridSearchCV  \n"
output+=str(confusion_matrix(y_test.values,y_pred))
output+="  \n  Train Accuracy: "+str(knn_classifier.score(X_train_scaled,y_train))+"  \n  Test Accuracy: "+str(knn_classifier.score(X_test_scaled,y_test))
grid_params={
    'n_neighbors':[3,5,7,9],
    'weights' :['uniform','distance'],
    'metric':['euclidean','manhattan','minkowski']
}
#n_jobs = -1 tells the computer to use all of it's processors to perform the model
knn_2 = KNeighborsClassifier()
clf = GridSearchCV(knn,grid_params,cv=3,n_jobs=-1)
best_classifier = clf.fit(X_train,y_train)
y_pred = best_classifier.predict(X_test_scaled)
output+="  \n  \nKNN Classifier  \nAfter GridSearchCV  \n"
output+=str(confusion_matrix(y_test,y_pred))
output+="  \n Train Accuracy: "+str(best_classifier.score(X_train,y_train))+"  \n  Test Accuracy: "+str(best_classifier.score(X_test,y_test))
print(output)
pl.dump(best_classifier,open("KNN_model", 'wb'))
print("The trained model has been saved in your local folder.\nThank you for using our tool\n")
#n_jobs = -1 tells the computer to use all of it's processors to perform the model