-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing_data_for_ML.py
More file actions
105 lines (84 loc) · 2.98 KB
/
preprocessing_data_for_ML.py
File metadata and controls
105 lines (84 loc) · 2.98 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
from collections import Counter
import numpy as np
import pandas as pd
import pickle
from sklearn import svm, neighbors
from sklearn.model_selection import cross_validate
from sklearn.model_selection import train_test_split
from sklearn.ensemble import VotingClassifier, RandomForestClassifier
def process_data_for_labels(ticker):
hm_days = 7
df = pd.read_csv('sp500_joined_closes.csv', index_col = 0)
tickers = df.columns.values.tolist()
df.fillna(0, inplace = True)
for i in range(1, hm_days + 1):
df['{}_{}d'.format(ticker, i)] = (df[ticker].shift(-i) -df[ticker]) / df[ticker]
df.fillna(0, inplace = True)
return tickers, df
#process_data_for_labels('AES')
def buy_sell_hold(*args):
cols = [c for c in args]
requirement = 0.02 #threshold -> tweak this number so that it is even btw buy, sell, hold
for col in cols:
if col > requirement:
return 1
if col < -requirement:
return -1
return 0
### extract feature sets and labels
def extract_featuresets(ticker):
tickers, df = process_data_for_labels(ticker)
df['{}_target'.format(ticker)] = list(map(
buy_sell_hold,
df['{}_1d'.format(ticker)],
df['{}_2d'.format(ticker)],
df['{}_3d'.format(ticker)],
df['{}_4d'.format(ticker)],
df['{}_5d'.format(ticker)],
df['{}_6d'.format(ticker)],
df['{}_7d'.format(ticker)]
))
vals = df['{}_target'.format(ticker)].values.tolist()
str_vals = [str(i) for i in vals]
print("Data spread:", Counter(str_vals))
df.fillna(0, inplace = True)
df = df.replace([np.inf, -np.inf], np.nan)
df.dropna(inplace = True)
df_vals = df[[ticker for ticker in tickers]].pct_change()
df_vals = df_vals.replace([np.inf, -np.inf], 0)
df_vals.fillna(0, inplace = True)
X = df_vals.values
y = df['{}_target'.format(ticker)].values
return X, y, df
def do_ml_kneighbour_classifier(ticker):
X, y, df = extract_featuresets(ticker)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size = 0.25
)
clf = neighbors.KNeighborsClassifier()
clf.fit(X_train, y_train)
confidence = clf.score(X_test, y_test)
print("Accuracy", confidence)
predictions = clf.predict(X_test)
print("Predicted spread: ", Counter(predictions))
return confidence
do_ml_kneighbour_classifier('AES')
def do_ml_voting_classifier(ticker):
X, y, df = extract_featuresets(ticker)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size = 0.25
)
clf = VotingClassifier(
[
('lsvc', svm.LinearSVC()),
('knn', neighbors.KNeighborsClassifier()),
('rfor', RandomForestClassifier())
]
)
clf.fit(X_train, y_train)
confidence = clf.score(X_test, y_test)
print("Accuracy", confidence)
predictions = clf.predict(X_test)
print("Predicted spread: ", Counter(predictions))
return confidence
do_ml_voting_classifier('ABT')