You need to enable JavaScript to run this app. Flask中逻辑回归预测生成空值数组但Python Notebook运行正常
优惠活动
大模型
产品
解决方案
定价
更多

Flask中逻辑回归预测生成空值数组但Python Notebook运行正常

问题原因与解决方案

核心原因

  • 索引不对齐导致运算全空:pandas中Series与DataFrame的算术运算会自动按索引/列名对齐,你创建的test_data使用默认整数索引,未和df的列名匹配,对齐失败的位置会自动填充NaN,最终产生全空数组。
  • 特征维度不匹配:你输出的xpred形状为(1,40),说明df删除指定列后还有40列特征,但你构造的test_data仅包含20个特征值,维度不匹配的位置也会被填充空值。
  • 数据类型异常:打印的test_data数据类型为object,混合了整数、浮点数类型,和df的数值型列运算时也可能触发空值生成。

修复步骤

  1. 构造test_data时直接绑定df的列名作为索引,保证对齐逻辑正常:
test_data = pd.Series(
    [battery_power, blue, clock_speed, dual_sim, fc, four_g,
     int_memory, m_dep, mobile_wt, n_cores, pc, px_height,
     px_width, ram, sc_h, sc_w, talk_time, three_g,
     touch_screen, wifi],
    index=df.columns
)
  1. 校验特征数量:执行print(len(df.columns), len(test_data)),如果df确实有40列特征,需要补全test_data中缺失的20个特征输入值,保证维度一致。
  2. 转换test_data为数值类型,避免运算异常:
test_data = test_data.astype('float64')
  1. 调整归一化代码,确保输出正确:
xpred = ((test_data - df.min())/(df.max() - df.min())).values.reshape(1, -1)

内容的提问来源于stack exchange,提问作者Sharan Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 21:24:07