-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmeans_clustering.py
More file actions
executable file
·70 lines (56 loc) · 1.89 KB
/
Copy pathkmeans_clustering.py
File metadata and controls
executable file
·70 lines (56 loc) · 1.89 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
# Import dependencies
import pandas as pd
from sklearn.cluster import KMeans
from math import sqrt
import pylab as pl
import numpy as np
import yfinance as yf
import datetime as dt
import requests
from pandas_datareader import data as pdr
from yahoo_fin import stock_info as si
# Loading the data
yf.pdr_override()
stocks = si.tickers_dow()[10:30] # Shorten the code for readability
start = dt.datetime(2010, 1, 1)
now = dt.datetime.now()
data = pdr.get_data_yahoo(stocks, start, now)['Close']
# Calculating annual mean returns and variances
returns = data.pct_change().mean() * 252
variance = data.pct_change().std() * sqrt(252)
# Renaming the columns
returns.name = "Returns"
variance.name = "Variance"
# Concatenating the returns and variances into a single data-frame
ret_var = pd.concat([returns, variance], axis=1).dropna()
ret_var.columns = ["Returns", "Variance"]
X = ret_var.values # Converting ret_var into a numpy array
sse = []
for k in range(2, 15):
kmeans = KMeans(n_clusters=k)
kmeans.fit(X)
sse.append(kmeans.inertia_) # SSE for each n_clusters
# Plotting the elbow curve
pl.plot(range(2, 15), sse)
pl.title("Elbow Curve")
pl.show()
# Plotting the clustering result
kmeans = KMeans(n_clusters=5).fit(X)
centroids = kmeans.cluster_centers_
pl.scatter(X[:, 0], X[:, 1], c=kmeans.labels_, cmap="rainbow")
pl.show()
# Dropping the row with maximum return value
ret_var.drop(returns.idxmax(), inplace=True)
X = ret_var.values
kmeans = KMeans(n_clusters=5).fit(X)
centroids = kmeans.cluster_centers_
# Plotting the clustering result
pl.scatter(X[:, 0], X[:, 1], c=kmeans.labels_, cmap="rainbow")
pl.show()
# Creating a new data frame with stock tickers and their assigned clusters
company = pd.DataFrame(ret_var.index)
cluster_labels = pd.DataFrame(kmeans.labels_)
df = pd.concat([company, cluster_labels], axis=1)
df.columns = ['Stock', 'Cluster']
df.set_index('Stock', inplace=True)
print(df)