沪深300股票聚类可视化案例||tushare完整可运行代码逐行解释

本文主要是介绍沪深300股票聚类可视化案例||tushare完整可运行代码逐行解释,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

上篇文章:《可视化股票市场结构||沪深300股票聚类可视化》逐行代码解释了sklearn中的一个案例:可视化股票市场结构。案例中采用的数据是美股。这篇文章将其移植到A股市场,看看我们的沪深300股票市场结构如何。采用的分类及可视化手段与sklearn案例完全一样。

在这里插入图片描述

在这里插入图片描述

  • 沪深300

沪深300指数1是由上海和深圳证券市场中选取市值大、流动性好的300支A股作为样本编制而成的成份股指数。沪深300指数样本覆盖了沪深市场六成左右的市值,具有良好的市场代表性。由中证指数有限公司2编制负责。

可以通过tushare获取:

  1. 首先获取沪深300成分列表

在这里插入图片描述

  1. 再获取个股历史纪录,只保留时间、开盘价、收盘价,截取2017年到2019年间数据
import numpy as np
import matplotlib.pyplot as plt
import tushare as ts
hs_datas = ts.get_hs300s()
symbols_name = np.array(hs_datas['name'])
symbols_code = np.array(hs_datas['code'])
quotes = []
for index, code in enumerate(symbols_code):stock_data = ts.get_hist_data(code, start='2017-01-01', end='2019-01-01')stock_data.sort_values(by=['date'], inplace=True)stock_data.reset_index(inplace=True)stock_data = stock_data[['date', 'open', 'close']]quotes.append(stock_data)row_now = hs_datas[hs_datas['code'] == code]name = row_now.iloc[0]['name']print('已获取第', index + 1, '只股:', code, name, '2017-01-01 到 2019-01-01的历史数据')# exit()
print(quotes)

在这里插入图片描述

  1. 数据整理,转为可为模型使用的数据
close_prices = np.vstack([q['close'] for q in quotes])
open_prices = np.vstack([q['open'] for q in quotes])
# 每日价格变换可能承载我们所需信息
variation = close_prices - open_prices

在这里插入图片描述

通过这三步操作,就完成了沪深300指数个股的历史记录。

上述第2部分的代码所得结果,在处理第3步时,会出现如下错误:(已解决)ValueError: all the input array dimensions except for the concatenation axis must match exactly。3上面给出了原因及解决方案,仔细研究应该时可以解决的,如果没搞懂,可以留言问我要完整代码。

  • 学习一个图结构

采用稀疏逆协方差评估来寻找哪些报价之间存在有条件的关联。

edge_model = covariance.GraphicalLassoCV(cv=5)   
X = variation.copy().T
X /= X.std(axis=0)
edge_model.fit(X)
  • 聚类

采用Affinity Propagation(近邻传播);因为它不强求相同大小的类,并且能从数据中自动确定类的数目。

_, labels = cluster.affinity_propagation(edge_model.covariance_)
n_labels = labels.max()
names = symbols_name[0:11]
for i in range(n_labels + 1):print('Cluster %i: %s' % ((i + 1), ', '.join(names[labels == i])))
  • 嵌入到2D画布

采用 Manifold learning(流形学习)技术来实现2D嵌入。

node_position_model = manifold.LocallyLinearEmbedding(n_components=2, eigen_solver='dense', n_neighbors=6)embedding = node_position_model.fit_transform(X.T).T
  • 可视化

3个模型的输出结合在一个2D图形上,节点表示股票,边表示:

  1. 簇标签用于定义节点颜色
  2. 稀疏协方差模型用于展示边的强度
  3. 2D嵌入用于定位平面中的节点
# Visualization
plt.figure(1, facecolor='w', figsize=(10, 8))
plt.clf()
ax = plt.axes([0., 0., 1., 1.])
plt.axis('off')# Display a graph of the 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, k=1)) > 0.02)# Plot the nodes using the coordinates of our embedding
plt.scatter(embedding[0], embedding[1], s=100 * d ** 2, c=labels,cmap=plt.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,zorder=0, cmap=plt.cm.hot_r,norm=plt.Normalize(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))]# print(dx)# print(this_dx)# exit()if this_dx > 0:horizontalalignment = 'left'x = x + .002else:horizontalalignment = 'right'x = x - .002if this_dy > 0:verticalalignment = 'bottom'y = y + .002else:verticalalignment = 'top'y = y - .002plt.text(x, y, name, size=10,horizontalalignment=horizontalalignment,verticalalignment=verticalalignment,bbox=dict(facecolor='w',edgecolor=plt.cm.nipy_spectral(label / float(n_labels)),alpha=.6))plt.xlim(embedding[0].min() - .15 * embedding[0].ptp(),embedding[0].max() + .10 * embedding[0].ptp(),)
plt.ylim(embedding[1].min() - .03 * embedding[1].ptp(),embedding[1].max() + .03 * embedding[1].ptp())plt.show()
  • 输出结果

在这里插入图片描述

在这里插入图片描述

聚类后结果

综述,整个过程除了获取沪深300指数个股资料部分的代码,其余各部分操作与《可视化股票市场结构||沪深300股票聚类可视化》4中完全一样,如需详细了解,可参考上文,特别是上文附录了大量相关细节。
如需完整代码,请留言索取。

  • Reference


  1. 维基百科 ↩︎

  2. 中证指数有限公司 ↩︎

  3. (已解决)ValueError: all the input array dimensions except for the concatenation axis must match exactly ↩︎

  4. 《可视化股票市场结构||沪深300股票聚类可视化》 ↩︎

这篇关于沪深300股票聚类可视化案例||tushare完整可运行代码逐行解释的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/861266

相关文章

IDEA运行spring项目时,控制台未出现的解决方案

《IDEA运行spring项目时,控制台未出现的解决方案》文章总结了在使用IDEA运行代码时,控制台未出现的问题和解决方案,问题可能是由于点击图标或重启IDEA后控制台仍未显示,解决方案提供了解决方法... 目录问题分析解决方案总结问题js使用IDEA,点击运行按钮,运行结束,但控制台未出现http://

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

vscode保存代码时自动eslint格式化图文教程

《vscode保存代码时自动eslint格式化图文教程》:本文主要介绍vscode保存代码时自动eslint格式化的相关资料,包括打开设置文件并复制特定内容,文中通过代码介绍的非常详细,需要的朋友... 目录1、点击设置2、选择远程--->点击右上角打开设置3、会弹出settings.json文件,将以下内

SQL Server使用SELECT INTO实现表备份的代码示例

《SQLServer使用SELECTINTO实现表备份的代码示例》在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误,在SQLServer中,可以使用SELECTINT... 在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误。在 SQL Server 中,可以使用 SE

SpringBoot实现动态插拔的AOP的完整案例

《SpringBoot实现动态插拔的AOP的完整案例》在现代软件开发中,面向切面编程(AOP)是一种非常重要的技术,能够有效实现日志记录、安全控制、性能监控等横切关注点的分离,在传统的AOP实现中,切... 目录引言一、AOP 概述1.1 什么是 AOP1.2 AOP 的典型应用场景1.3 为什么需要动态插

Golang操作DuckDB实战案例分享

《Golang操作DuckDB实战案例分享》DuckDB是一个嵌入式SQL数据库引擎,它与众所周知的SQLite非常相似,但它是为olap风格的工作负载设计的,DuckDB支持各种数据类型和SQL特性... 目录DuckDB的主要优点环境准备初始化表和数据查询单行或多行错误处理和事务完整代码最后总结Duck

python实现pdf转word和excel的示例代码

《python实现pdf转word和excel的示例代码》本文主要介绍了python实现pdf转word和excel的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一、引言二、python编程1,PDF转Word2,PDF转Excel三、前端页面效果展示总结一

在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码

《在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码》在MyBatis的XML映射文件中,trim元素用于动态添加SQL语句的一部分,处理前缀、后缀及多余的逗号或连接符,示... 在MyBATis的XML映射文件中,<trim>元素用于动态地添加SQL语句的一部分,例如SET或W

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如