-
Notifications
You must be signed in to change notification settings - Fork 15
/
KNN.py
73 lines (56 loc) · 2.4 KB
/
KNN.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 9 11:25:00 2020
@author: Shreya
"""
import pandas as pd
import numpy as np
datainput = pd.read_csv('datafile.csv')
#preprocessing
X = datainput[['Crop', 'State', 'Cost of Cultivation (`/Hectare) A2+FL', 'Cost of Cultivation (`/Hectare) C2','Cost of Production (`/Quintal) C2' ]].values
from sklearn import preprocessing
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
#label encoder to categorical data
labelencoder_X = preprocessing.LabelEncoder()
X[:,0] = labelencoder_X.fit_transform(X[:, 0])
X[:,1] = labelencoder_X.fit_transform(X[:, 1])
#One hot encoder
columnTransformer = ColumnTransformer([('encoder', OneHotEncoder(),[0])], remainder='passthrough')
x2 = np.array(columnTransformer.fit_transform(X), dtype = np.float)
columnTransformer = ColumnTransformer([('encoder', OneHotEncoder(),[10])], remainder='passthrough')
x3 = np.array(columnTransformer.fit_transform(x2), dtype = np.float)
y = datainput.iloc[:,-1]
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import DecisionTreeRegressor
from sklearn import metrics
from sklearn import utils
X_train, X_test, y_train, y_test = train_test_split(x3, y, test_size=0.3, random_state=3)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
from sklearn import neighbors
from sklearn.metrics import mean_squared_error
from math import sqrt
import matplotlib.pyplot as plt
rmse_val = [] #to store rmse values for different k
for K in range(20):
K = K+1
model = neighbors.KNeighborsRegressor(n_neighbors = K)
model.fit(X_train, y_train) #fit the model
pred=model.predict(X_test) #make prediction on test set
error = sqrt(mean_squared_error(y_test,pred)) #calculate rmse
rmse_val.append(error) #store rmse values
print('RMSE value for k= ' , K , 'is:', error)
curve = pd.DataFrame(rmse_val) #elbow curve
curve.plot()
from sklearn.neighbors import KNeighborsClassifier
regressor = neighbors.KNeighborsRegressor(n_neighbors = 4)
regressor.fit(X_train, y_train)
# Predicting the Test set results
y_pred = regressor.predict(X_test)
error = sqrt(mean_squared_error(y_test,y_pred)) #calculate rmse
rmse_val.append(error)
print(error)