建筑工程网站监理答案,广告设计好学吗难不难,天津网站排名提升,信阳专业做网站公司可视化上证50结构图 缘由收集数据先获取50支成分股列表获取各成分股票K线数据 数据处理找出来#xff0c;再删除#xff0c;然后重新下载数据最终获得每日报价的变化值 图形结构处理聚类分析使用affinity_propagation(亲和传播)聚类 嵌入二维平面空间可视化小结热力图 缘由
… 可视化上证50结构图 缘由收集数据先获取50支成分股列表获取各成分股票K线数据 数据处理找出来再删除然后重新下载数据最终获得每日报价的变化值 图形结构处理聚类分析使用affinity_propagation(亲和传播)聚类 嵌入二维平面空间可视化小结热力图 缘由
前些日子看到sklearn 官网上一个事例可视化的股市结构 由于萌发给咱沪深也整张图经过一段时间的研究现分享给大家。
收集数据
原文是数据是来自2003年至2008年 data.nasdaq.com和alphavantage.co等API中获得。 本文的数据 采用 baostock 2020-至今天2023-09-30的交易数据除权选择上证50成分股。
import numpy as np
import pandas as pd
import baostock as bs先获取50支成分股列表
# 登陆系统
lg bs.login()
# 获取上证50成分股
rs bs.query_sz50_stocks()
# 打印结果集
sz50_stocks []
while (rs.error_code 0) rs.next():# 获取一条记录将记录合并在一起sz50_stocks.append(rs.get_row_data())
result pd.DataFrame(sz50_stocks, columnsrs.fields)
bs.logout()dfresult.iloc[:,1:3]
df1df.set_index(code)
symbol_dict df1.to_dict()[code_name]
symbols, names np.array(sorted(symbol_dict.i如下图
获取各成分股票K线数据
def get_kdata(code):rs bs.query_history_k_data_plus(code,date,open,close,start_date2020-01-01, end_date2023-09-30,frequencyd, adjustflag3)data_list []while (rs.error_code 0) rs.next():data_list.append(rs.get_row_data())result pd.DataFrame(data_list, columnsrs.fields)return result
bs.login()
quotes []
#
for symbol in symbols:#print(symbol)ddfget_kdata(symbol)quotes.append(ddf)
bs.logout()数据处理
由于这里有些股票数据个数不一致导致后继执行出错因此将不一致的数据进行删除。
for i,quote in enumerate(quotes):if (len(quote)!910):print(i,symbols[i],names[i],len(quote))23 sh.600905 三峡能源 563 42 sh.601995 中金公司 711 49 sh.688599 天合光能 806
找出来再删除然后重新下载数据
那样的话并非50支票而是47支。
result.drop(labels[23,42,49],inplaceTrue)
dfresult.iloc[:,1:3]
df1df.set_index(code)
symbol_dict df1.to_dict()[code_name]
symbols, names np.array(sorted(symbol_dict.items())).T
bs.login()
quotes []
for symbol in symbols:ddfget_kdata(symbol)quotes.append(ddf)
bs.logout()最终获得每日报价的变化值
close_prices np.vstack([q[close].astype(float) for q in quotes])
open_prices np.vstack([q[open].astype(float) for q in quotes])# 报价的每日变化是信息最多的地方
variation close_prices - open_prices图形结构处理
我们使用稀疏逆协方差估计来找出哪些引号与其他引号有条件地相关。具体来说稀疏逆协方差给了我们一个图这是一个连接列表。对于每个符号它所连接的符号都是用来解释其波动的。
协方差矩阵的逆矩阵通常称为精度矩阵precision matrix它与部分相关矩阵partial correlation matrix成正比。 它给出部分独立性关系。换句话说如果两个特征在其他特征上有条件地独立 则精度矩阵中的对应系数将为零。这就是为什么估计一个稀疏精度矩阵是有道理的 通过从数据中学习独立关系协方差矩阵的估计能更好处理。这被称为协方差选择。
在小样本的情况即 n_samples 是数量级 n_features 或更小 稀疏的逆协方差估计往往比收敛的协方差估计更好。 然而在相反的情况下或者对于非常相关的数据它们可能在数值上不稳定。 此外与收敛估算不同稀疏估计器能够恢复非对角线结构 off-diagonal structure。
from sklearn import covariancealphas np.logspace(-1.5, 1, num10)
edge_model covariance.GraphicalLassoCV(alphasalphas)#标准化时间序列使用相关性而不是协方差
#前者对结构恢复更有效
X variation.copy().T
X / X.std(axis0)
edge_model.fit(X)聚类分析
使用affinity_propagation(亲和传播)聚类
我们使用聚类将行为相似的引号分组在一起。在这里在scikit-learn中可用的各种集群技术中我们使用Affinity Propagation因为它不强制执行相同大小的集群并且它可以从数据中自动选择集群的数量。 请注意这给了我们一个与图不同的指示因为图反映了变量之间的条件关系而聚类反映了边际性质聚集在一起的变量可以被认为在整个股市水平上具有类似的影响。
Affinity Propagation是一种基于图论的聚类算法旨在识别数据中的exemplars(代表点)和clusters(簇)。与K-Means等传统聚类算法不同Affinity Propagation不需要事先指定聚类数目也不需要随机初始化簇心而是通过计算数据点之间的相似性得出最终的聚类结果。
Affinity Propagation算法的优点是不需要预先指定聚类数目且能够处理非凸形状的簇。但是该算法的计算复杂度较高需要大量的存储空间和计算资源并且对于噪声点和离群点的处理能力较弱。
from sklearn import cluster_, labels cluster.affinity_propagation(edge_model.covariance_, random_state0)
n_labels labels.max()for i in range(n_labels 1):print(fCluster {i 1}: {, .join(names[labels i])})Cluster 1: 中信证券, 上汽集团, 航发动力, 中信建投, 华泰证券 Cluster 2: 包钢股份, 北方稀土, 国电南瑞 Cluster 3: 三一重工, 万华化学, 恒力石化, 紫金矿业, 中远海控 Cluster 4: 通威股份, 隆基绿能, 长城汽车, 合盛硅业, 华友钴业 Cluster 5: 复星医药, 恒瑞医药, 片仔癀, 贵州茅台, 海尔智家, 山西汾酒, 伊利股份, 中国中免, 药明康德, 海天味业 Cluster 6: 中国神华, 陕西煤业, 中国石油 Cluster 7: 中国石化, 农业银行, 工商银行 Cluster 8: 招商银行, 兴业银行, 中国平安, 中国太保, 中国人寿 Cluster 9: 保利发展, 海螺水泥, 长江电力, 中国建筑, 中国电建 Cluster 10: 闻泰科技, 韦尔股份, 兆易创新
嵌入二维平面空间
为了出于可视化目的我们需要在二维画布上显示不同的符号。为此我们使用流形学习技术来检索2D嵌入。我们使用密集的本征分解器来实现再现性arpack是用我们不控制的随机向量启动的。此外我们使用大量的邻居来捕捉大规模的结构。
#为可视化寻找低维嵌入找到 最佳的二维平面位置股票from sklearn import manifoldnode_position_model manifold.LocallyLinearEmbedding(n_components2, eigen_solverdense, n_neighbors6
)
embedding node_position_model.fit_transform(X.T).T可视化
3种模型的输出组合成一个二维平面图其中节点表示股票边缘表示:
集群标签用于定义节点的颜色使用稀疏协方差模型来显示边的强度边的宽度二维嵌入用于定位平面中的节点
这个示例包含大量与可视化相关的代码因为可视化在这里对于显示图形至关重要。其中一个挑战是定位标签使重叠最小化。为此我们使用基于每个轴上最近邻居方向的启发式方法。
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
plt.rcParams[font.sans-serif][SimHei] #用来正常显示中文标签
plt.rcParams[axes.unicode_minus]False #用来正常显示负号 #有中文出现的情况需要u内容plt.figure(1, facecolorw, figsize(20, 16))
plt.clf()
ax plt.axes([0.0, 0.0, 1.0, 1.0])
plt.axis(off)# Plot the graph of partial correlations
partial_correlations edge_model.precision_.copy()
d 1 / np.sqrt(np.diag(partial_correlations))
partial_correlations * d
partial_correlations * d[:, np.newaxis]
non_zero np.abs(np.triu(partial_correlations, k1)) 0.02# Plot the nodes using the coordinates of our embedding
plt.scatter(embedding[0], embedding[1], s100 * d**2, clabels, cmapplt.cm.nipy_spectral
);# Plot the edges
start_idx, end_idx np.where(non_zero)
# a sequence of (*line0*, *line1*, *line2*), where::
# linen (x0, y0), (x1, y1), ... (xm, ym)
segments [[embedding[:, start], embedding[:, stop]] for start, stop in zip(start_idx, end_idx)
]
values np.abs(partial_correlations[non_zero])
lc LineCollection(segments, zorder0, cmapplt.cm.hot_r, normplt.Normalize(0, 0.7 * values.max())
)
lc.set_array(values)
lc.set_linewidths(15 * values)
ax.add_collection(lc)# Add a label to each node. The challenge here is that we want to
# position the labels to avoid overlap with other labels
for index, (name, label, (x, y)) in enumerate(zip(names, labels, embedding.T)):dx x - embedding[0]dx[index] 1dy y - embedding[1]dy[index] 1this_dx dx[np.argmin(np.abs(dy))]this_dy dy[np.argmin(np.abs(dx))]if this_dx 0:horizontalalignment leftx x 0.002else:horizontalalignment rightx x - 0.002if this_dy 0:verticalalignment bottomy y 0.002else:verticalalignment topy y - 0.002plt.text(x,y,name,size10,horizontalalignmenthorizontalalignment,verticalalignmentverticalalignment,bboxdict(facecolorw,edgecolorplt.cm.nipy_spectral(label / float(n_labels)),alpha0.6,),)plt.xlim(embedding[0].min() - 0.15 * embedding[0].ptp(),embedding[0].max() 0.10 * embedding[0].ptp(),
)
plt.ylim(embedding[1].min() - 0.03 * embedding[1].ptp(),embedding[1].max() 0.03 * embedding[1].ptp(),
)plt.show();小结
从上图可以看出同类股票并非分布在同一区域距离的远近也反映了两两的相关性。 从一个侧面也反映了各股票之间的关系。如果有可能将二维平面扩展成三维立体图可能会更加直观。
热力图
基于相关性本文引入了热力图来进一步可视化。
import seaborn as sns
fig plt.figure(figsize (15,10))
ax fig.add_subplot(111)
sns.set()
ax sns.heatmap(edge_model.covariance_)从上图可以看出相关性绝大多数在0.2 左右只有小数达到0.8以上。