-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstock_clustering_db.py
77 lines (71 loc) · 2.36 KB
/
stock_clustering_db.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
import pandas as pd
import numpy as np
import time
import datetime as dt
import os
from tqdm import tqdm
from sklearn.cluster import KMeans
from sklearn.cluster import AgglomerativeClustering
from sklearn.cluster import DBSCAN
from dtw import dtw
from sklearn.metrics import pairwise_distances
PRE_NAME = "onemin_ohlc_"
BEGIN_TIME = "09:00:00"
END_TIME = "11:00:00"
def load_data():
X = []
Y = []
# df = pd.read_csv(os.path.join('dataset', '2327', PRE_NAME+"20180612.csv"))
# mask = (df.loc[:, "time"] >= BEGIN_TIME) & (df.loc[:, "time"] <= END_TIME)
# front_df = df[mask].loc[:, "return"]
# end_df = df[~mask].loc[:, "return"]
# X.append(np.array(front_df))
# Y.append(np.array(end_df))
# """我是分隔線^^~"""
# df = pd.read_csv(os.path.join('dataset', '2327', PRE_NAME+"20180613.csv"))
# mask = (df.loc[:, "time"] >= BEGIN_TIME) & (df.loc[:, "time"] <= END_TIME)
# front_df = df[mask].loc[:, "return"]
# end_df = df[~mask].loc[:, "return"]
# X.append(np.array(front_df))
# Y.append(np.array(end_df))
sid = '2327'
for file in os.listdir(os.path.join('dataset', sid)):
# print(file)
df = pd.read_csv(os.path.join('dataset', sid, file))
mask = (df.loc[:, "time"] >= BEGIN_TIME) & (df.loc[:, "time"] <= END_TIME)
front_df = df[mask].loc[:, "return"]
# print(front_df)
# exit()
end_df = df[~mask].loc[:, "return"]
if len(front_df) == 121:
# print(df.to_string())
# exit()
# print(np.array(front_df).shape)
X.append(np.array(front_df))
Y.append(np.array(end_df))
# print(end_df)
# df = pd.read_csv(os.path.join('dataset', '2327', PRE_NAME+"20180612.csv"))
# print(len(X))
# print(len(X[0]))
# X = np.array(X)
# print(X.shape)
# exit()
return np.array(X), np.array(Y)
def dtw_d(X, Y):
manhattan_distance = lambda x, y: np.abs(x - y)
d, cost_matrix, acc_cost_matrix, path = dtw(X, Y, dist=manhattan_distance)
return d
def dtw_affinity(X):
return pairwise_distances(X, metric=dtw_d)
if __name__ == "__main__":
X, Y = load_data()
# print(X.shape)
# print(X.shape)
# print(np.array)
# print(dtw_d(X[0], X[1]))
# exit()
# print(d)
db = DBSCAN(metric=dtw_d, n_jobs=5)
X_label = db.fit_predict(X)
print(X_label)
print(X.shape)