Python skfuzzy库cmeans_predict()函数使用说明 - TSK-FS模型示例
很抱歉,我之前提供的代码有误。在skfuzzy库中,'cmeans_predict()'函数不再接受第一个参数作为数据集,而是需要传入已经训练好的模型。以下是修正后的代码:
import numpy as np
import skfuzzy as fuzz
# 成分数据矩阵
data = np.array([[0.758, 0.171, 0.049, 0.022],
[0.758, 0.172, 0.047, 0.023],
...
[0.562, 0.179, 0.175, 0.084]])
# 计算熵序列
entropy_sequence = -np.sum(data * np.log2(data), axis=1)
# TSK模型参数设置
N = 5 # 集群数
cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(entropy_sequence.reshape(-1, 1), N, 2, error=0.005, maxiter=1000, init=None)
# TSK模型预测
target_values = np.zeros_like(entropy_sequence)
for i in range(len(entropy_sequence)):
alphas = u[:, i] # 获取隶属度
rules = np.argmax(u[:, i]) # 获取控制规则
target_values[i] = np.mean(data[i]) + alphas[rules] * (np.std(data[i]) / np.std(entropy_sequence)) * (entropy_sequence[i] - np.mean(entropy_sequence))
# CRMSE计算
crmse = np.sqrt(np.mean((entropy_sequence - target_values)**2))
# CMAPE计算
cmape = np.mean(np.abs(entropy_sequence - target_values) / entropy_sequence) * 100
print("CRMSE: ", crmse)
print("CMAPE: ", cmape)
请确保已经安装了NumPy和scikit-fuzzy库,然后运行以上修正后的代码以计算CRMSE和CMAPE。
原文地址: https://www.cveoy.top/t/topic/cf7F 著作权归作者所有。请勿转载和采集!