-
Notifications
You must be signed in to change notification settings - Fork 0
/
KNN and Logistic.py
66 lines (35 loc) · 1.06 KB
/
KNN and Logistic.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
from fancyimpute import KNN
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.model_selection import train_test_split
from sklearn import datasets
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# In[17]:
data = pd.read_csv("train_with_missing/1.csv",index_col = False)
# In[18]:
values = data.values
X_filled_knn = KNN(k=2).fit_transform(values)
y = values[:,13]
x = np.delete(X_filled_knn, 0, 1)
# In[19]:
xtr,xt,ytr,yt=train_test_split(x, y, test_size=0.1, random_state=42)
# In[20]:
log = LogisticRegression()
xtr[np.isnan(xtr)] = 0
ytr[np.isnan(ytr)] = 0
from sklearn import preprocessing
from sklearn import utils
lab_enc = preprocessing.LabelEncoder()
encoded_ytr = lab_enc.fit_transform(ytr)
log.fit(xtr,encoded_ytr)
# In[21]:
ytp= log.predict(xt)
# In[23]:
from sklearn.metrics import mean_squared_error
print("LR MSE = " , mean_squared_error(yt, ytp)*100 , "%")