-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsrc_code.py
67 lines (46 loc) · 1.55 KB
/
src_code.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
#!/usr/bin/env python
# coding: utf-8
#To predict quality of Air Pollution
#Author: Akshay Mattoo
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
get_ipython().run_line_magic('matplotlib', 'inline')
df_train = pd.read_csv('pred_air_quality/Train.csv')
df_test = pd.read_csv('pred_air_quality/Test/Test.csv')
#Store the training data
train_data = df_train.values
Y_train = train_data [1:,-1]
X_train = train_data [1:,:-1]
X_train = np.mat(X_train)
Y_train = np.mat(Y_train)
Y_train = Y_train.reshape ((1599, 1))
#Obtain the weight matrix
def getWeight (query_point, X_train, tau):
M = X_train.shape[0]
W = np.mat(np.eye(M))
for i in range (M):
xi = X_train[i]
x = query_point
W[i,i] = np.exp((np.sum(xi-x) ** 2)/(-2*tau*tau)) #Compute the weight for each query point and store it in the diagonal Weight matrix
return W
X_test = df_test.values
X_test = X_test[0:,:]
X_test = np.mat(X_test)
X_test.size
def predict (X_train, Y_train, query_point, tau):
ones = np.ones ((X_train.shape[0], 1))
X_ = np.hstack((X_train, ones))
qx = np.hstack((query_point, np.ones((query_point.shape[0], 1))))
W = getWeight(qx, X_, tau)
theta = np.linalg.pinv(X_.T*(W*X_))*(X_.T*(W*Y_train))
pred = np.dot(qx, theta)
return theta, pred
with open ("result.csv",'w') as f:
f.write("Id,target \n")
for i in range(0, 400, 1):
theta, pred = predict (X_train, Y_train, X_test[i], 0.1)
f.write(str(i))
f.write(",")
f.write(str(pred)[2:-2])
f.write("\n")