import math import matplotlib.pyplot as plt import pandas as pd file_path = r'C:\Users\520605\Desktop\data.xlsx' raw_data = pd.read_excel(file_path, header=0) data = raw_data.values viscosity = data[0:,:2] print(viscosity) density = data[0:,:3] print(density) num_1 =data[-1,0:0] C=torch.zeros(10) def dis(a,b): return math.sqrt((a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1])) K = 4 CentPoint = [] for i in range(K): CentPoint.append([torch.randint(0,10000,(1,)).item(), torch.randint(0,10,(1,)).item()]) print(CentPoint) for p in range(10): NewPoint = [[0, 0] for i in range(K)] for i in range(len(viscosity)): mDis=1e9 mC=0 for j in range(len(CentPoint)): cp=CentPoint[j] D = dis([viscosity[1,i].int(), density[1,i].int()], cp) if mDis>D: mDis=D mC=j C[i]=mC NewPoint[mC][0]+=viscosity[1,i] NewPoint[mC][1]+=density[1,i] for i in range(K): CentPoint[i][0]=NewPoint[i][0]/num_1 CentPoint[i][1]=NewPoint[i][1]/num_1 print(CentPoint) cc=list(C) for i in range(len(viscosity)): if cc[i]==0: plt.plot(viscosity[1,i].item(), density[1,i].item(), 'r.') elif cc[i]==1: plt.plot(viscosity[1,i].item(), density[1,i].item(), 'g.') elif cc[i]==2: plt.plot(viscosity[1,i].item(), density[1,i].item(), 'b.') elif cc[i]==3: plt.plot(viscosity[1,i].item(), density[1,i].item(), color='pink', marker='.') elif cc[i]==4: plt.plot(viscosity[1,i].item(), density[1,i].item(), color='orange', marker='.') for CP in CentPoint: plt.plot(CP[0], CP[1], color='black', marker='X') plt.show()
时间: 2024-01-25 17:04:31 浏览: 59
好的,以下是对你的代码进行改进的建议:
1. 在代码开头,需要将导入模块的语句分行书写,以提高代码的可读性;
2. 在读取 Excel 表格时,建议使用相对路径,以使代码在其他环境中也能正常运行;
3. 在计算样本点到聚类中心的距离时,建议使用 NumPy 库提供的矩阵计算方法,以提高代码的效率;
4. 在绘制数据图形时,可以使用 Matplotlib 的颜色映射函数 `plt.cm`,以简化代码。
改进后的代码如下:
```python
import math
import matplotlib.pyplot as plt
import pandas as pd
import torch
import numpy as np
file_path = './data.xlsx'
raw_data = pd.read_excel(file_path, header=0)
data = raw_data.values
viscosity = data[:, :2]
density = data[:, :3]
num_1 = data[-1, 0]
C = torch.zeros(len(viscosity))
def dis(a, b):
return np.linalg.norm(a-b)
K = 4
CentPoint = []
for i in range(K):
CentPoint.append([torch.randint(0, 10000, (1,)).item(),
torch.randint(0, 10, (1,)).item()])
for p in range(10):
NewPoint = [[0, 0] for i in range(K)]
for i in range(len(viscosity)):
mDis = 1e9
mC = 0
for j in range(len(CentPoint)):
cp = CentPoint[j]
D = dis([viscosity[i, 1].item(), density[i, 1].item()], cp)
if mDis > D:
mDis = D
mC = j
C[i] = mC
NewPoint[mC][0] += viscosity[i, 1].item()
NewPoint[mC][1] += density[i, 1].item()
for i in range(K):
CentPoint[i][0] = NewPoint[i][0] / num_1
CentPoint[i][1] = NewPoint[i][1] / num_1
cmap = plt.cm.get_cmap('viridis', K)
for i in range(len(viscosity)):
plt.plot(viscosity[i, 1].item(), density[i, 1].item(), '.', color=cmap(int(C[i])))
for CP in CentPoint:
plt.plot(CP[0], CP[1], color='black', marker='X')
plt.show()
```
这段代码中,我们使用了 NumPy 库提供的 `linalg.norm()` 函数计算样本点到聚类中心的距离,使用了 Matplotlib 的 `cm.get_cmap()` 函数获取颜色映射,以简化绘图代码。
阅读全文