续接上篇
通过聚类算法分析股票,选股的策略代码如下(一)!
(3032, 2)
import numpy as np from sklearn.cluster import KMeans from sklearn.manifold import TSNE import matplotlib.pyplot as plt from sklearn.metrics import pairwise_distances_argmin_min
# 假设您的数据存储在名为DATA的NumPy数组中,其形状为(3032, 225) # 请替换为您的实际数据 # 提取降维后的坐标 x = embedded_data[:, 0] y = embedded_data[:, 1] # 初始化K均值模型 num_clusters = 10 kmeans = KMeans(n_clusters=num_clusters, random_state=42) # 用K均值对降维后的数据进行聚类 labels = kmeans.fit_predict(embedded_data) # 提取簇中心坐标 cluster_centers = kmeans.cluster_centers_ print(cluster_centers) center_sample_indices, _ = pairwise_distances_argmin_min(cluster_centers, embedded_data) # 输出每个簇中心样本的ID for i, index in enumerate(center_sample_indices): print(f"Cluster {i + 1} center sample ID: {index}") # 绘制散点图,并按簇着色 plt.scatter(x, y, c=labels, cmap='viridis', alpha=0.5) plt.scatter(cluster_centers[:, 0], cluster_centers[:, 1], marker='X', s=200, color='red', label='Centroids') # 标记每个簇的中心样本ID ID = [] for i, index in enumerate(center_sample_indices): plt.text(cluster_centers[i, 0], cluster_centers[i, 1], str(index), fontsize=8, ha='center', va='center', color='black') ID.append(index) plt.title('K-Means Clustering with t-SNE Visualization') plt.legend() plt.show()
# 获取一只股票的名称 def get_stock_name(code): return get_security_info(code).display_name # 获取一只股票的行业
def get_stock_industry(code): return get_industry(code, date=None)['jq_l2']['industry_name'] LIST = [] industry = [] name =[] for i in ID: LIST.append(stockList[i]) industry.append(get_industry(stockList[i], date=_end_date)[stockList[i]]['jq_l2']['industry_name']) name.append(get_security_info(stockList[i]).display_name) q = query(valuation.code, valuation.circulating_market_cap, indicator.eps).filter( valuation.code.in_(LIST)).order_by(valuation.circulating_market_cap.asc()) df = get_fundamentals(q) df['name']=name df['industry']=industry print(df)
code circulating_market_cap eps name industry 0 603813.XSHG 15.1462 -0.0705 协和电子 印制电路板 1 002823.XSHE 20.8682 0.1277 元祖股份 休闲食品 2 605258.XSHG 24.2088 0.0729 黑牡丹 房地产开发 3 605177.XSHG 31.4445 0.2725 凯中精密 电动机与工控自动化
4 603886.XSHG 43.0080 1.0569 宁波银行 区域性银行 5 603686.XSHG 43.1451 0.1316 原尚股份 物流综合 6 603322.XSHG 58.4770 0.0505 福龙马 市政环卫 7 600510.XSHG 58.6703 0.2593 超讯通信 通信技术服务 8 603077.XSHG 208.4175 0.0456 东亚药业 原料药 9 002142.XSHE 1332.1523 0.9695 和邦生物 农药