用pandas函数代替下列语句: elif i>len(data2_0_time3)-4: data2_0_time3.loc[i,'speed_391_24']=0 data2_0_time3.loc[i,'speed_391_23']=0 data2_0_time3.loc[i,'speed_391_22']=0 k=0 r=len(data2_0_time3)-1-i for j in range(1,4): if np.isnan(data2_0_time3.iat[i-j,0])==False: data2_0_time3.loc[i,'speed_391_24']=data2_0_time3.iat[i,0]+data2_0_time3.iat[i-j,0] data2_0_time3.loc[i,'speed_391_23']=data2_0_time3.iat[i,1]+data2_0_time3.iat[i-j,1] data2_0_time3.loc[i,'speed_391_22']=data2_0_time3.iat[i,2]+data2_0_time3.iat[i-j,2] k=k+1 for j in range(1,r): if np.isnan(data2_0_time3.iat[i+j,0])==False: data2_0_time3.loc[i,'speed_391_24']=data2_0_time3.iat[i,0]+data2_0_time3.iat[i+j,0] data2_0_time3.loc[i,'speed_391_23']=data2_0_time3.iat[i,1]+data2_0_time3.iat[i+j,1] data2_0_time3.loc[i,'speed_391_22']=data2_0_time3.iat[i,2]+data2_0_time3.iat[i+j,2] k=k+1 data2_0_time3.loc[i,'speed_391_24']=data2_0_time3.iat[i,0]/k data2_0_time3.loc[i,'speed_391_23']=data2_0_time3.iat[i,1]/k data2_0_time3.loc[i,'speed_391_22']=data2_0_time3.iat[i,2]/k
时间: 2023-06-08 21:08:23 浏览: 117
使用pandas的rolling函数可以代替原始代码中的for循环,通过计算指定区间内的均值来完成相同的功能,具体代码如下:
```python
data2_0_time3['speed_391_24'] = data2_0_time3.iloc[:,0].rolling(window=4, min_periods=1).mean()
data2_0_time3['speed_391_23'] = data2_0_time3.iloc[:,1].rolling(window=4, min_periods=1).mean()
data2_0_time3['speed_391_22'] = data2_0_time3.iloc[:,2].rolling(window=4, min_periods=1).mean()
```
其中,rolling函数中的参数window指定了滑动窗口的大小,min_periods指定了有效数据的最小数目。由于rolling的均值计算默认是包括当前值的,所以在数据较短时需要指定min_periods参数。
相关问题
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()
好的,以下是对你的代码进行改进的建议:
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()` 函数获取颜色映射,以简化绘图代码。
阅读全文