-
Notifications
You must be signed in to change notification settings - Fork 2
/
ml.py
71 lines (56 loc) · 1.96 KB
/
ml.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
from get_stock_data import *
import json
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
import datetime
# tickers = ['AAPL', 'MSFT', 'GOOG']
# df.to_csv("data/stock_prices.c")
def clean_date(date):
mm = int(date[:2])
dd = int(date[2:4])
yy = int(date[4:])
return datetime.date(yy, mm, dd)
def create_df_for_ticker(ticker):
tickers = [ticker]
dates = pd.date_range('2012-01-01', '2012-12-31')
df = get_data(tickers, dates)
dates = pd.date_range('2013-01-01', '2013-12-31')
df = df.append(get_data(tickers, dates))
dates = pd.date_range('2014-01-01', '2014-12-31')
df = df.append(get_data(tickers, dates))
dates = pd.date_range('2015-01-01', '2015-12-31')
df = df.append(get_data(tickers, dates))
dates = pd.date_range('2016-01-01', '2016-12-31')
df = df.append(get_data(tickers, dates))
df = compute_daily_returns(df)
with open('data/{}.json'.format(ticker)) as json_data:
data = json.load(json_data)
cleaned_data = {}
for date, sentiment in data.items():
cleaned_data[clean_date(date)] = sentiment
cd = pd.DataFrame.from_dict(cleaned_data, orient='index')
cd.columns = ['Polarity', 'Subjectivity']
cd = df.join(cd)
cd = cd.drop('SPY', 1)
# cd[ticker] = cd[ticker].apply(lambda x: 1 if x > 0 else 0)
return cd
aapl = create_df_for_ticker('AAPL')
# aapl = aapl.fillna(method='backfill')
aapl = aapl.dropna(axis='index')
print(aapl)
aapl.to_csv("data/aapl_complete_removed_nan.csv")
# # Initialize the model class.
# model = LinearRegression()
# # Fit the model to the training data.
# model.fit(list(map(lambda k: [k], aapl['Polarity'].tolist())), list(map(lambda k: [k], aapl['AAPL'].tolist())))
# print("fitting complete")
# print(model.coef_)
# print(model.predict(0.4))
# ax = aapl.plot()
# plt.show()
# goog = create_df_for_ticker('GOOG')
# msft = create_df_for_ticker('MSFT')
# print(aapl)
# print(goog)
# print(msft)