m0_61705011 2021-11-26 14:35 采纳率: 50%
浏览 348
已结题

AttributeError: 'History' object has no attribute 'predict' 做集成时碰到这个问题怎么办呢?

问题遇到的现象和发生背景

做集成算法时出现了下列错误,求帮忙

问题相关代码,请勿粘贴截图
def TrainPredict(self,x_train,y_train,x_test):          #训练基础模型,并返回模型预测结果
    clf = self.estimator.fit(np.array(x_train),np.array(y_train))
    result = clf.predict(x_test)
    return result

def Bagging_clf(self, x_train, x_test, y_train, y_test, sample_type="RepetitionRandomSampling"):
    print("self.Bagging single_basemodel")
    result = ()

    if sample_type == "RepetitionRandomSampling":
        print("选择的采样方法:", sample_type)
        sample_function = self.RepetitionRandomSampling
    elif sample_type == "UnderSampling":
        print("选择的采样方法:", sample_type)
        sample_function = self.UnderSampling
        print("采样率", self.rate)
    elif sample_type == "IF_SubSample":
        print("选择的采样方法:", sample_type)
        sample_function = self.IF_SubSample
        print("采样率", (1.0 - self.rate))
    print(sample_function(train, len(train)))
    for i in range(self.n_estimators):
        sample = sample_function(train, len(train))  # 构建数据集
        x_train = np.array(sample)[:, :, 0:-1]
        y_train = np.array(sample)[:, :, -1]
        list(result).append(self.TrainPredict(x_train, y_train, x_test))  # 训练模型 返回每个模型的输出
    print(np.array(result))
    score = self.Voting(result)
    recall, precision = self.Metrics(score, y_test)
    return recall, precision

recall_self,precision_self = clf_self.Bagging_clf(x_train, x_test, y_train, y_test)
print("recall:",'\n',recall_self)
print("precision",'\n',precision_self)
运行结果及报错内容

Traceback (most recent call last):
File "D:\pycharm\practice\待改进(smote与模仿github中的自写bagging).py", line 236, in
recall_self,precision_self = clf_self.Bagging_clf(x_train, x_test, y_train, y_test)
File "D:\pycharm\practice\待改进(smote与模仿github中的自写bagging).py", line 170, in Bagging_clf
list(result).append(self.TrainPredict(x_train, y_train, x_test)) # 训练模型 返回每个模型的输出
File "D:\pycharm\practice\待改进(smote与模仿github中的自写bagging).py", line 133, in TrainPredict
result = clf.predict(x_test)
AttributeError: 'History' object has no attribute 'predict'

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • 於黾 2021-11-26 14:54
    关注
    clf = self.estimator.fit(np.array(x_train),np.array(y_train))
    

    这个返回的是个History类型的对象
    而History里面没有predict这个方法
    你是不是搞错了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月9日
  • 已采纳回答 12月1日
  • 创建了问题 11月26日

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗