python中 .reshape 的用法:reshape(1,-1)

1、numpy中reshape函数的几种常见相关用法

reshape(1,-1)转化成1行:

reshape(2,-1)转换成两行:

reshape(-1,1)转换成1列:

reshape(-1,2)转化成两列

reshape(2,8)转化成两行八列

test = [[1,2,3],[2,3,4]]
test_x = np.reshape(test_x, (2, 1, 3)) # 转二维为三维

2、该篇博客的起源是sklearn时fit的报错

error:

x = np.array([6, 8, 10, 14, 18])
y = np.array([7, 9, 13, 17.5, 18])
model.fit(x, y)

运行后显示: ValueError: Expected 2D array, got 1D array instead
大概意思是期望2维数组,但输入的却是一维数组
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.

solve:

x= np.array([6, 8, 10, 14, 18]).reshape(-1, 1)
y = np.array([7, 9, 13, 17.5, 18]).reshape(-1, 1)
model.fit(x, y)

这是由于在sklearn中,所有的数据都应该是二维矩阵,哪怕它只是单独一行或一列(比如前面做预测时,仅仅只用了一个样本数据),所以需要使用numpy库的.reshape(1,-1)进行转换,而reshape的意思以及常用用法即上述内容。

  • 41
    点赞
  • 255
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值