Last active
October 21, 2019 07:20
-
-
Save CodersArts/9ab1846130dc6b78147e6191193d9eb8 to your computer and use it in GitHub Desktop.
kNN (k- Nearest Neighbors) - Fit and Predict, train and test data model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' For more visit | |
https://www.codersarts.com | |
''' | |
import pandas as pd | |
from sklearn.neighbors import KNeighborsClassifier | |
from sklearn.metrics import accuracy_score | |
# read the train and test dataset | |
train_data = pd.read_csv('train_data.csv') | |
test_data = pd.read_csv('test_data.csv') | |
print(train_data.head()) | |
# shape of the dataset | |
print('\nShape of training data :',train_data.shape) | |
print('\nShape of testing data :',test_data.shape) | |
# Predict the missing target variable in the test data | |
train_x = train_data.drop(columns=['Sales_item'],axis=1) | |
train_y = train_data['Sales_item'] | |
# seperate the independent and target variable on training data | |
test_x = test_data.drop(columns=['Sales_item'],axis=1) | |
test_y = test_data['Sales_item'] | |
model = KNeighborsClassifier() | |
# fit the model with the training data | |
model.fit(train_x,train_y) | |
# coefficeints of the trained model | |
model.coef_ | |
# intercept of the model | |
model.intercept_ | |
# predict the target on the train dataset | |
predict_train_data = model.predict(train_x) | |
predict_train_data | |
# Accuray Score on train dataset | |
accuracy_train_data = accuracy_score(train_y,predict_train) | |
accuracy_train_data | |
# predict the target on the test dataset | |
predict_test_data = model.predict(test_x) | |
predict_test_data | |
# Accuracy Score on test dataset | |
accuracy_test_data = accuracy_score(test_y,predict_test) | |
accuracy_test_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment