-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.py
80 lines (62 loc) · 2.4 KB
/
4.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
74
75
76
77
78
import numpy as np
import matplotlib.pyplot as plt
from weight import weight
from distance import getDistanceMatrix
from distance import getDistanceMatrixTrainTest
from distance import distance
def ker(t):
return np.max([1-t, 0])
trainCSV = np.genfromtxt('quasar_train.csv',delimiter=",")
lambdas = trainCSV[0,:]
lambdas = lambdas.reshape((lambdas.shape[0],1))
train = np.genfromtxt('quasar_train_smooth.csv',delimiter=",")
test = np.genfromtxt('quasar_test_smooth.csv',delimiter=",")
right_index = np.where(lambdas == 1300)[0][0]
left_index = np.where(lambdas == 1200)[0][0]
lambdas_right = lambdas[right_index:,:]
lambdas_left = lambdas[:left_index,:]
train_right = train[:,right_index:]
test_right = test[:,right_index:]
train_left = train[:,:left_index]
test_left = test[:,:left_index]
mm = train.shape[0]
neighborhood_size = 3
train_right_distance = getDistanceMatrix(train_right)
train_left_hat = np.zeros(train_left.shape)
error = np.zeros((mm,1))
for i in range(0,mm):
indices_of_neighbors = train_right_distance[i].argsort()[:neighborhood_size]
distance_with_neighbors = train_right_distance[i][indices_of_neighbors]
h = np.nanmax(train_right_distance[i])
upper = 0
lower = 0
for j in range(0, neighborhood_size):
kernel = ker(distance_with_neighbors[j]/h)
upper = upper + kernel * train_left[indices_of_neighbors[j],:]
lower = lower + kernel
train_left_hat[i] = upper / lower
error[i] = distance(train_left[i], train_left_hat[i])
print(np.average(error))
mm_test = test.shape[0]
distanceM = getDistanceMatrixTrainTest(train_right, test_right)
test_left_hat = np.zeros(test_left.shape)
error = np.zeros((mm_test,1))
for i in range(0,mm_test):
indices_of_neighbors = distanceM[i].argsort()[:neighborhood_size]
distance_with_neighbors = distanceM[i][indices_of_neighbors]
h = np.nanmax(distanceM[i])
upper = 0
lower = 0
for j in range(0, neighborhood_size):
#kernel = ker(distance_with_neighbors[j]/h)
kernel = ker(distance_with_neighbors[j] / h)
upper = upper + kernel * train_left[indices_of_neighbors[j],:]
lower = lower + kernel
test_left_hat[i] = upper / lower
error[i] = distance(test_left[i], test_left_hat[i])
print(np.average(error))
plt.figure(1, figsize=(4, 4))
plt.plot(lambdas, test[5], ".", label="true" )
plt.plot(lambdas_left, test_left_hat[5], ".", label="true" )
plt.legend()
plt.show()