数据集stock_std.csv有三个属性,code(股票的代码),change(第一天的开盘价和最后一天的收盘价之间的涨幅),amount(成交额)组成的。并且change和amount已经做了归一化计算。 采用层次聚类算法对股票的信息进行挖掘,聚类类别为3,并用图像将结果表示出来。
时间: 2024-02-20 15:02:20 浏览: 28
好的,可以使用Python的scikit-learn库进行层次聚类的实现。首先,导入需要的库和数据集:
```python
import pandas as pd
from sklearn.cluster import AgglomerativeClustering
import matplotlib.pyplot as plt
# 导入数据集
data = pd.read_csv('stock_std.csv', index_col='code')
```
然后,进行层次聚类的实现,设定聚类类别为3:
```python
# 进行层次聚类
model = AgglomerativeClustering(n_clusters=3)
model.fit(data)
```
最后,将聚类结果可视化:
```python
# 将聚类结果可视化
labels = model.labels_
plt.scatter(data['change'], data['amount'], c=labels)
plt.xlabel('change')
plt.ylabel('amount')
plt.show()
```
完整的代码如下:
```python
import pandas as pd
from sklearn.cluster import AgglomerativeClustering
import matplotlib.pyplot as plt
# 导入数据集
data = pd.read_csv('stock_std.csv', index_col='code')
# 进行层次聚类
model = AgglomerativeClustering(n_clusters=3)
model.fit(data)
# 将聚类结果可视化
labels = model.labels_
plt.scatter(data['change'], data['amount'], c=labels)
plt.xlabel('change')
plt.ylabel('amount')
plt.show()
```
该代码将会对数据集进行层次聚类,聚类类别为3,并且将聚类结果用图像表示出来。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)