导读 本文主要介绍了numpy.reshape(-1,1)的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

数组新的shape属性应该要与原来的配套,如果等于-1的话,那么Numpy会根据剩下的维度计算出数组的另外一个shape属性值。

举个例子:

x = np.array([[2, 0], [1, 1], [2, 3]])

指定新数组行为3,列为,2,则:

y = x.reshape(3,2)
  
y
Out[43]: 
array([[2, 0],
       [1, 1],
       [2, 3]])

指定新数组列为1,则:

y = x.reshape(-1,1)
  
y
Out[34]: 
array([[2],
       [0],
       [1],
       [1],
       [2],
       [3]])

指定新数组列为2,则:

y = x.reshape(-1,2)
  
y
Out[37]: 
array([[2, 0],
       [1, 1],
       [2, 3]])

指定新数组行为1,则:

y = x.reshape(1,-1)
  
y
Out[39]: array([[2, 0, 1, 1, 2, 3]])

指定新数组行为2,则:

y = x.reshape(2,-1)
  
y
Out[41]: 
array([[2, 0, 1],
       [1, 2, 3]])
numpy中reshape(-1,1)与reshape(1,-1)的作用

如果你的数据只有一个特征,可以用reshape(-1,1)改变你的数据形状;或者如果你的数据只包含一个样本,可以使用reshape(1,-1)来改变。

e = np.array([1]) #只包含一个数据
f = e.reshape(1,-1) #改变形状,输出f之后发现它已经变成了二维数据
import numpy as np
a = np.array([[1,2,3],[4,5,6]]) #是两行三列的数据,二维
b = np.array([1,2])    #是一维数据
c = b.reshape(-1,1)    #c已经变成了二维数据,变成了两行一列
d = b.reshape(1,-1)    #d变成了一行两列的数据,
print('b.shape is {0}'.format(b.shape))
print(b)
print('c.shape is {0}'.format(c.shape))
print(c)
print('d.shape is {0},d array is {1}'.format(d.shape,d))

可以发现reshape(-1,1)是将一维数据在行上变化,而reshape(1,-1)是将一维数据在列上变化

到此这篇关于numpy.reshape(-1,1)的具体使用的文章就介绍到这了

原文来自:https://www.jb51.net/article/256601.htm

本文地址:https://www.linuxprobe.com/numpy-reshape-linux.html编辑:向云艳,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/

Linux系统大全:https://www.linuxdown.com/

红帽认证RHCE考试心得:https://www.rhce.net/