-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathq.py
93 lines (85 loc) · 2.35 KB
/
q.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#print(__doc__)
import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt
###############################################################################
# Generate sample data
parent = []
target = []
with open('sample_train.csv') as f:
for line in f:
line = line.split(',')
count = 0
count1 = 0
fill = 0.000001
length = -1
line1 = line[26:209]
for i in line1:
length = length + 1
if i == '':
line1[length] = fill
#print fill
#count = count + 1
else:
fill = line1[length]
#count1 = count1 + 1
#print count1
#for space in line1:
# if space == '':
# count= count+1
#print line1
#a = []
train_X = line1[0:120]
parent.append(train_X)
#print parent
y = line1[121:209]
target.append(y)
#############################################################################
# before 2 days and 180 values every minute value and after 2 days
#parent = np.sort(train_X, axis=0)
New_train_X = []
with open('test.csv') as f:
for line in f:
line = line.split(',')
count = 0
count1 = 0
fill = 0.000001
length = -1
line1 = line[26:145]
for i in line1:
length = length + 1
if i == '':
line1[length] = fill
#print fill
#count = count + 1
else:
fill = line1[length]
New_train_X.append(line1)
#############################################################################
#print X
#y = np.sin(X).ravel()
#print y
###############################################################################
# Add noise to targets
#y[::5] += 3 * (0.5 - np.random.rand(8))
#print y
###############################################################################
# Fit regression model
svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
#svr_lin = SVR(kernel='linear', C=1e3)
#svr_poly = SVR(kernel='poly', C=1e3, degree=2)
y_rbf = svr_rbf.fit(parent, target).predict(New_train_X)
#y_lin = svr_lin.fit(X, y).predict(X)
#y_poly = svr_poly.fit(X, y).predict(X)
###############################################################################
# look at the results
plt.scatter(parent, target, c='k', label='data')
plt.hold('on')
plt.plot(New_train_X, y_rbf, c='g', label='RBF model')
#plt.plot(X, y_lin, c='r', label='Linear model')
#plt.plot(X, y_poly, c='b', label='Polynomial model')
plt.xlabel('data')
plt.ylabel('target')
plt.title('Support Vector Regression')
plt.legend()
plt.show()