这个错误是由于在objective_function函数中,predicted_failure_time和actual_failure_time都是DataFrame类型,而计算mse时需要将它们转换为numpy数组。可以使用.values将DataFrame转换为numpy数组。具体修改如下:
import pandas as pd
import numpy as np
from scipy.optimize import minimize
# 加载数据
data = pd.read_csv('E:/lunwenpython/measures_v21.csv')
data= data.iloc[610000:630000, ]
data = (data - data.mean()) / data.std()
def pearson_fusion(data, weights):
# 计算每个特征与目标变量之间的皮尔逊相关系数
corr = data.corrwith(data['pm'])
corr.drop('pm', inplace=True)
# 根据皮尔逊相关系数和权重计算复合健康指标
composite_index = 0
for i in range(len(corr)):
composite_index += weights[i]*corr[i]*data.iloc[:,i]
return composite_index, corr
# 加载设备实际寿命数据
actual_failure_time = data.iloc[:-1]['time'].values # 假设设备实际寿命为1000秒
# 定义目标函数
def objective_function(x, data):
weights = x[:6]
T = x[6]
data = data.iloc[:-1, :]
composite_index, corr = pearson_fusion(data, weights)
N = len(composite_index)
dt = T/N
t = np.linspace(0, T, N+1)
dW = np.sqrt(dt)*np.random.randn(N)
W = np.cumsum(dW)
W = np.insert(W, 0, 0)
weights = np.repeat(weights, N+1).reshape((6,N+1))
Z =np.dot(weights, W.flatten())
predicted_failure_time = t[np.argmax(Z > 0.9*np.max(Z))]
mse = ((predicted_failure_time - actual_failure_time)**2)/len(data.iloc[:-1])
return mse
res = minimize(objective_function, [0.1]*6 + [1000], args=(data,), method='Nelder-Mead')
optimized_weights