-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpredict.py
More file actions
229 lines (171 loc) · 5.29 KB
/
predict.py
File metadata and controls
229 lines (171 loc) · 5.29 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import pandas as pd
import numpy as np
import plotly
import peakutils
plotly.tools.set_credentials_file(username='sonijigar', api_key='5QyOYUZdkMRHBl4UUtU8')
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as FF
from sklearn import datasets, linear_model
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
data = pd.read_csv('price-sentiment.csv')
time_series = data['sentiment']
time_series = time_series.tolist()
df = data['date']
ind_arr = []
indp_arr = []
cb = np.array(time_series)
indices = peakutils.indexes((-cb), thres=0.02/max(cb), min_dist=0.1)
indices_peak = peakutils.indexes(cb, thres=0.02/max(cb), min_dist=0.1)
# signal graph
# all_peaks = indices + indices_peak
i = 0
for x in indices:
ind_arr.append(x)
for y in indices_peak:
indp_arr.append(y)
ind_arr.sort()
# print("all_peak",all_peak.array)
# print("indices_peak",ind_arr)
it = 0
ts = []
count = 0;
for t in range(len(df)):
if it in ind_arr:
ts.append(1)
elif it in indp_arr:
ts.append(-1)
else:
ts.append(0)
it = it + 1
# print("ts_array",ts)
tr1 = go.Scatter(
# x = [df[j] for j in range(len(time_series))],
# y = ts,
x = [df[j] for j in range(len(time_series))],
y = data['signal'],
mode='markers',
marker=dict(
size=15,
color='rgb(255,0,0)',
symbol='circle'
),
connectgaps=True,
name='signal'
)
dtplot = [tr1]
layout = go.Layout(yaxis=dict(
autorange=True
# showgrid=False,
# zeroline=False,
# showline=False,
# autotick=True,
# ticks='',
# showticklabels=False
)
)
# layout={
# yaxis: {range: [-180, -88]}
# }
fig = go.Figure(data=dtplot, layout=layout)
py.plot(fig, filename='signal-buy-sell')
# print("minimas",indices)
glob_x = indices;
glob_y = [time_series[j] for j in indices]
# print("y\n",[df[j] for j in indices])
trace1 = go.Scatter(
x=[df[j] for j in range(len(time_series))],
y=time_series,
mode='lines',
name='Sentiment'
)
trace2 = go.Scatter(
x=df[indices],
y=[time_series[j] for j in indices],
mode='markers',
marker=dict(
size=8,
color='rgb(255,0,0)',
symbol='dash'
),
name='Detected valley'
)
trace3 = go.Scatter(
x=df[indices_peak],
y=[time_series[j] for j in indices_peak],
mode='markers',
marker=dict(
size=8,
color='rgb(0,255,0)',
symbol='cross'
),
name='Detected Peaks'
)
dataplot = [trace1, trace2, trace3]
# py.plot(dataplot, filename='sentiment-with-peaks')
# var layout={
# yaxis: {range: [-180, -88]}
# }
# Plotly.newPlot('myDiv', data, layout);
dif = pd.read_csv('price-sentiment.csv')
sample_data_table = FF.create_table(dif.head())
# py.plot(sample_data_table, filename='data-table')
trace1 = go.Scatter(x = dif['date'], y = dif['price'],
name='Price Values', marker=dict(size=8,color='rgb(0,255,0)'))
trace2 = go.Scatter(x = dif['date'], y = 11000+dif['sentiment']*5000,
name='Scaled sentiment values', marker=dict(size=8,color='rgb(255,0,0)'))
# layout = go.Layout(title='Bitcoin Prices Over Time',
# plot_bgcolor='rgb(230, 230,230)',
# showlegend=True)
# fig = go.Figure(data=[trace1, trace2], layout=layout)
data = [trace1, trace2]
# py.plot(data, filename='bitcoin-prices-sentiment')
df2 = pd.read_csv('price-sentiment.csv')
sample_data_table = FF.create_table(df2.head())
# py.plot(sample_data_table, filename='data-table')
trace = go.Scatter(x = df2['date'], y = df2['price'],
name='Price Values')
layout = go.Layout(title='Bitcoin Prices Over Time',
plot_bgcolor='rgb(230, 230,230)',
showlegend=True)
fig = go.Figure(data=[trace], layout=layout)
# py.plot(fig, filename='bitcoin-prices')
df1 = pd.read_csv('features.csv')
sample_data_table = FF.create_table(df1.head())
# py.plot(sample_data_table, filename='sample-data-table')
trace = go.Scatter(x = df1['date'], y = df1['sentiment'],
name='Sentiment Values')
layout = go.Layout(title='Tweet Sentiments Over Time',
plot_bgcolor='rgb(230, 230,230)',
showlegend=True)
fig = go.Figure(data=[trace], layout=layout)
# py.plot(fig, filename='apple-stock-prices')
df = pd.read_csv('feature.csv')
X = df.iloc[:, :-1]
y = df.iloc[:, -1:].values.ravel()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
# print("x train is {0}".format(X_train))
# print("y train is {0}".format(y_train))
# print(len(y_train))
# print("x test is {0}".format(X_test))
# print("y test is {0}".format(y_test))
# Create linear regression object
#regr = linear_model.LogisticRegression()
regr = linear_model.LinearRegression()
# print(type(y_train[0]))
# Train the model using the training sets
regr.fit(X_train, map(lambda number:float(number),y_train))
# Make predictions using the testing set
y_pred = regr.predict(X_test)
# The coefficients
# print('Coefficients: \n', regr.coef_)
# The mean squared error
print("Mean squared error: %.2f"
% mean_squared_error(y_test, y_pred))
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % r2_score(y_test, y_pred))
error = np.mean(y_pred != y_test)
# print(y_pred)
# print(y_test)
# print(error)