报错内容
学习宋天龙老师的python数据化分析与运营
发现这段代码没法运行
ValueError: Expected 2D array, got scalar array instead:
array=84610.
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
我们已经建立了一个二维数组,并且通过建模,得到了线性回归的模型。但是需要输入新的值到模型中new_x,来预测pre_y。
原代码是
new_x = 84610
pre_y = model.predict(new_x)
print(pre_y)
解决方案
因为新的X是单一示例值,应该改为 array.reshape(1, -1),转化成1行:
new_x = 84610
new_x = np.array(new_x).reshape(1, -1)
pre_y = model.predict(new_x)
print(pre_y)
解决Python预测模型中单值输入问题:reshape操作指南
本文讲述了在使用宋天龙老师的Python数据化分析与运营课程中的线性回归模型时,遇到的ValueError:期望二维数组,解决办法是将单值转换为一维数组并通过reshape调整。重要步骤包括创建1行1列的数组。
7万+





