numpy.reshape(-1,1)的具体使用

作者:Pikachu_simple 时间:2021-12-26 13:25:30 

数组新的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)是将一维数据在列上变化

来源:https://blog.csdn.net/qq_42804678/article/details/99062431

标签:numpy.reshape(-1,1)
0
投稿

猜你喜欢

  • ASP+XML制作菜单管理

    2008-05-19 12:38:00
  • 详解Python在使用JSON时需要注意的编码问题

    2022-08-03 22:06:36
  • python实现批量图片格式转换

    2021-07-15 16:07:42
  • Go语言利用time.After实现超时控制的方法详解

    2024-04-26 17:21:50
  • PHP如何实现HTTP验证

    2023-09-04 05:32:46
  • 详解LyScript 内存扫描与查壳实现

    2022-04-18 07:07:31
  • 解决PyCharm 中写 Turtle代码没提示以及标黄的问题

    2023-05-12 09:08:30
  • Vue实现模糊查询的简单方法实例

    2024-04-28 09:20:55
  • Python基于checksum计算文件是否相同的方法

    2022-11-08 20:30:48
  • python多任务及返回值的处理方法

    2023-11-02 14:20:24
  • 基于python利用Pyecharts使高清图片导出并在PPT中动态展示

    2021-02-23 13:02:39
  • python Requsets下载开源网站的代码(带索引 数据)

    2023-01-03 13:19:11
  • 如何随机显示图片计数器?

    2010-05-16 15:21:00
  • 解决python中os.listdir()函数读取文件夹下文件的乱序和排序问题

    2022-10-24 05:34:54
  • 基于pygame实现童年掌机打砖块游戏

    2023-09-18 20:41:28
  • Python根据字典的值查询出对应的键的方法

    2022-04-07 04:01:49
  • python2.7和NLTK安装详细教程

    2021-03-30 22:41:19
  • Django利用AJAX技术实现博文实时搜索

    2023-01-02 01:57:50
  • 页面制作中要注意的编码问题

    2008-08-11 12:43:00
  • python实现从ftp服务器下载文件的方法

    2023-08-02 20:50:54
  • asp之家 网络编程 m.aspxhome.com