-
Notifications
You must be signed in to change notification settings - Fork 2
/
simpleANN.py
91 lines (71 loc) · 2.64 KB
/
simpleANN.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
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import math
from keras.models import Sequential
from keras.layers.recurrent import LSTM
from keras import regularizers
from keras.layers.normalization import BatchNormalization
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, CSVLogger, EarlyStopping
from sklearn import preprocessing
matplotlib.style.use('ggplot')
# Stocks information
# date,open,high,low,volume,close
df_st = pd.read_csv('data/ts1.csv', usecols=[1,2,3,4,5],
engine='python')
# Stock+tweets
# date,tweets,pos,neg,neutr,open,high,low,volume,close
df = pd.read_csv('data/full_tesla.csv',
usecols=[0,1,2,3,4,5,6,7,8,9],
engine='python')
# correlation for stocks+tweets
dfTS = pd.DataFrame(df)
corr_tw = dfTS.corr()
print corr_tw
# stocks and tweets visualization
volume = df.ix[:, 'volume'].tolist()
tweets = df.ix[:, 'tweets'].tolist()
vol = preprocessing.scale(np.array(volume))
tw = preprocessing.scale(np.array(tweets))
plt.plot(tw, color='black', label = 'Amount of tweets')
plt.plot(vol, color='green', label = 'Traded volume')
plt.legend(loc='best')
plt.title('Number of tweets and traded volume for TSLA')
plt.show()
# LEARNING
dataset = df.values
X = dataset[:,1:9]
Y = dataset[:,9]
min_max_scaler = preprocessing.MinMaxScaler()
X_scaled = min_max_scaler.fit_transform(np.array(X))
Y_scaled = min_max_scaler.fit_transform(np.array(Y))
def build_model():
# create model
model = Sequential()
model.add(Dense(8, input_dim=8, kernel_initializer='normal', activation='relu'))
model.add(BatchNormalization())
model.add(Dense(6, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, kernel_initializer='normal'))
#Compile model
model.compile(loss='mean_squared_error', optimizer='adam')
return model
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.9, patience=5, min_lr=0.000001, verbose=1)
model = build_model()
history = model.fit(X_scaled, Y_scaled,
batch_size=128,
epochs=120,
callbacks=[reduce_lr],
validation_split=0.1)
predicted = model.predict(np.array(X_scaled))
original = Y_scaled
print model.summary()
plt.plot(original, color='black', label = 'Original price')
plt.plot(predicted, color='blue', label = 'Predicted price')
plt.legend(loc='best')
plt.title('Actual and predicted price')
plt.show()