首页 > 其他分享 >无涯教程-NumPy - resize函数

无涯教程-NumPy - resize函数

时间:2023-10-17 12:32:31浏览次数:35  
标签:numpy 无涯 np shape print array NumPy resize

此函数返回具有指定大小的新数组,该函数采用以下参数。

numpy.resize(arr, shape)
Sr.No. 描述
1

arr

输入数组要调整大小

2

shape

输出数组的新维度

import numpy as np 
a = np.array([[1,2,3],[4,5,6]]) 

print 'First array:' 
print a 
print '\n'

print 'The shape of first array:' 
print a.shape 
print '\n'  
b = np.resize(a, (3,2)) 

print 'Second array:' 
print b 
print '\n'  

print 'The shape of second array:' 
print b.shape 
print '\n'  
# Observe that first row of a is repeated in b since size is bigger 

print 'Resize the second array:' 
b = np.resize(a,(3,3)) 
print b

上面的程序将产生以下输出-

First array:
[[1 2 3]
 [4 5 6]]

The shape of first array:
(2, 3)

Second array:
[[1 2]
 [3 4]
 [5 6]]

The shape of second array:
(3, 2)

Resize the second array:
[[1 2 3]
 [4 5 6]
 [1 2 3]]

参考链接

https://www.learnfk.com/numpy/numpy-resize.html

标签:numpy,无涯,np,shape,print,array,NumPy,resize
From: https://blog.51cto.com/u_14033984/7902138

相关文章

  • 无涯教程-NumPy - broadcast_to函数
    此功能将数组广播为新维度,它返回原始数组的只读视图,如果新维度不符合NumPy的广播规则,则该函数可能会引发ValueError。注意-此功能自版本1.10.0起可用。该函数采用以下参数。numpy.broadcast_to(array,shape,subok)importnumpyasnpa=np.arange(4).reshape(1,4)prin......
  • 无涯教程-NumPy - swapaxes函数
    此函数互换数组的两个轴,对于1.10之后的NumPy版本,将返回交换数组的视图,该函数采用以下参数。numpy.swapaxes(arr,axis1,axis2)Sr.No.Parameter&描述1arr要交换其轴的输入数组2axis1与第一个轴对应的int3axis2与第二个轴对应的int#Itcreatesa3dimen......
  • 无涯教程-NumPy - rollaxis函数
    此功能使指定的轴向后滚动,直到它位于指定的位置,该函数具有三个参数。numpy.rollaxis(arr,axis,start)Sr.No.Parameter&描述1arr输入数组2axis轴向后滚动。其他轴的位置相对彼此不变3start默认情况下为零,以完成整个滚动。滚动直到到达指定位置#Itcreate......
  • numpy基础
    In [1]:pipinstallnumpy  Requirementalreadysatisfied:numpyinc:\users\dengzl\.conda\envs\data_analysis\lib\site-packages(1.26.0)Note:youmayneedtorestartthekerneltouseupdatedpackages. In [2]:#创建一个......
  • 创建numpy数组
     1.2.1创建NumPy数组的多种方式¶array:将数组转换为ndarray,推断dtype或者显示指定arange:类似内置函数range,返回ndarrayzeros:创建全0数组,可指定形状和dtypeones:创建全1数组,可指定形状和dtypeempty:创建新数组,只分配内存空间、不填充任何值1.2.2转换NumPy数......
  • numpy基本操作
      1.3.1索引¶单个元素索引:一维数组、负数索引二维数组的索引1.3.2切片¶切片跨步索引数组:针对多为数组的索引索引结合切片 In [1]:importnumpyasnp In [2]:#一维数组索引array1=np.array([1,2,3,4,5])array......
  • Numpy数据运算
    1.4-NumPy数组运算  1.4.1算术运算¶add加subtract减multipie乘divide除1.4.2数学运算¶三角函数:sin,cos,tan算术运算1.4.3统计运算¶mean均值average均值var方差std标准差1.4.4聚合运算¶性能区别:np聚合函数和py内置函数sum......
  • 无涯教程-NumPy - reshape函数
    此函数在不更改数据的情况下为数组提供了新的维度,它接受以下参数-numpy.reshape(arr,newshape,order')Sr.No.描述1arr数组2newshapeint或int的元组,新维度应与原始维度兼容3order如果数组存储在类似Fortran的连续内存中,则"C"表示C风格,"F"表示Fortran风格,"......
  • 无涯教程-NumPy - Matplotlib
    Matplotlib是Python的绘图库,它与NumPy一起使用,提供了一个环境,是MatLab的有效开源替代方案,它可以与PyQt和wxPython等图形工具包一起使用Matplotlib模块最初由JohnD.Hunter编写。自2012年以来,MichaelDroettboom是主要开发人员。目前,Matplotlib版本。1.5.1是可用的稳定版本。......
  • 无涯教程-NumPy - 算术运算
    用于执行算术运算(如add(),subtract(),multipli()和divide())的输入数组必须具有相同的维度或符合数组broadcasting规则。importnumpyasnpa=np.arange(9,dtype=np.float_).reshape(3,3)print'Firstarray:'printaprint'\n'print'Secondarray:'b=......