numpy 二维数组怎样用三个数组

Numpy使用教程3
Numpy 使用教程--Numpy 数组操作及随机抽样
如果你使用
语言进行科学计算,那么一定会接触到 Numpy。Numpy 是支持 Python 语言的数值计算扩充库,其拥有强大的高维度数组处理与矩阵运算能力。除此之外,Numpy 还内建了大量的函数,方便你快速构建数学模型。
Numpy 数组的基本操作
python2.7Xfce 终端ipython 终端
本课程难度为一般,属于初级级别课程,适合具有 Python 基础,并对使用 Numpy 进行科学计算感兴趣的用户。
上一个章节,我们了解了如何利用 numpy 创建各式各样的 ndarray。本章节,我们将利用学会针对 ndarray 的各种花式操作技巧。
reshape 可以在不改变数组数据的同时,改变数组的形状。其中,numpy.reshape() 等效于 ndarray.reshape()。reshape 方法非常简单:
numpy.reshape(a, newshape)
其中,a 表示原数组,newshape 用于指定新的形状(整数或者元组)。
举个例子:
import numpy as np
np.arange(10).reshape((5, 2))
ravel 的目的是将任意形状的数组扁平化,变为 1 维数组。ravel 方法如下:
numpy.ravel(a, order='C')
其中,a 表示需要处理的数组。order 表示变换时的读取顺序,默认是按照行依次读取,当 order='F' 时,可以按列依次读取排序。
import numpy as np
a = np.arange(10).reshape((2, 5))
np.ravel(a)
np.ravel(a, order='F')
moveaxis 可以将数组的轴移动到新的位置。其方法如下:
numpy.moveaxis(a, source, destination)
a:数组。source:要移动的轴的原始位置。destination:要移动的轴的目标位置。
举个例子:
import numpy as np
a = np.ones((1, 2, 3))
np.moveaxis(a, 0, -1)
你可能没有看明白是什么意思,我们可以输出二者的 shape属性:
和 moveaxis 不同的是,swapaxes 可以用来交换数组的轴。其方法如下:
numpy.swapaxes(a, axis1, axis2)
a:数组。axis1:需要交换的轴 1 位置。axis2:需要与轴 1 交换位置的轴 1 位置。
举个例子:
import numpy as np
a = np.ones((1, 4, 3))
np.swapaxes(a, 0, 2)
我们直接输出两个数组的 shape 值。
transpose 类似于矩阵的转置,它可以将 2 维数组的横轴和纵轴交换。其方法如下:
numpy.transpose(a, axes=None)
a:数组。axis:该值默认为 none,表示转置。如果有值,那么则按照值替换轴。
举个例子:
import numpy as np
a = np.arange(4).reshape(2,2)
np.transpose(a)
atleast_xd 支持将输入数据直接视为 x维。这里的 x 可以表示:1,2,3。方法分别维:
numpy.atleast_1d()
numpy.atleast_2d()
numpy.atleast_3d()
举个例子:
import numpy as np
np.atleast_1d([1])
np.atleast_2d([1])
np.atleast_3d([1])
在 numpy 中,还有一系列以 as 开头的方法,它们可以将特定输入转换为数组,亦可将数组转换为矩阵、标量,ndarray 等。如下:
asarray(a,dtype,order):将特定输入转换为数组。asanyarray(a,dtype,order):将特定输入转换为 ndarray。asmatrix(data,dtype):将特定输入转换为矩阵。asfarray(a,dtype):将特定输入转换为 float 类型的数组。asarray_chkfinite(a,dtype,order):将特定输入转换为数组,检查 NaN 或 infs。asscalar(a):将大小为 1 的数组转换为标量。
这里以 asmatrix(data,dtype) 方法举例:
import numpy as np
a = np.arange(4).reshape(2,2)
np.asmatrix(a)
concatenate 可以将多个数组沿指定轴连接在一起。其方法为:
numpy.concatenate((a1, a2, ...), axis=0)
(a1, a2, ...):需要连接的数组。axis:指定连接轴。
举个例子:
import numpy as np
a = np.array([[1, 2], [3, 4], [5, 6]])
b = np.array([[7, 8], [9, 10]])
c = np.array([[11, 12]])
np.concatenate((a, b, c), axis=0)
这里,我们可以尝试沿着横轴连接。但要保证连接处的维数一致,所以这里用到了 .T 转置。
a = np.array([[1, 2], [3, 4], [5, 6]])
b = np.array([[7, 8, 9]])
np.concatenate((a, b.T), axis=1)
在 numpy 中,还有一系列以 as 开头的方法,它们可以将特定输入转换为数组,亦可将数组转换为矩阵、标量,ndarray 等。如下:
stack(arrays,axis):沿着新轴连接数组的序列。column_stack():将 1 维数组作为列堆叠到 2 维数组中。hstack():按水平方向堆叠数组。vstack():按垂直方向堆叠数组。dstack():按深度方向堆叠数组。
这里以 stack(arrays,axis) 方法举例:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.stack((a, b))
当然,也可以横着堆叠。
np.stack((a, b), axis=-1)
split 及与之相似的一系列方法主要是用于数组的拆分,列举如下:
split(ary,indices_or_sections,axis):将数组拆分为多个子数组。dsplit(ary,indices_or_sections):按深度方向将数组拆分成多个子数组。hsplit(ary,indices_or_sections):按水平方向将数组拆分成多个子数组。vsplit(ary,indices_or_sections):按垂直方向将数组拆分成多个子数组。
下面,我们看一看 split 到底有什么效果:
import numpy as np
a = np.arange(10)
np.split(a, 5)
除了 1 维数组,更高维度也是可以直接拆分的。例如,我们可以将下面的数组按行拆分为 2。
import numpy as np
a = np.arange(10).reshape(2,5)
np.split(a, 2)
numpy 中还有针对数组元素添加或移除的一些方法。
delete(arr,obj,axis):沿特定轴删除数组中的子数组。
下面,依次对 4 种方法进行示例,首先是 delete 删除:
import numpy as np
a = np.arange(12).reshape(3,4)
np.delete(a, 2, 1)
这里代表沿着横轴,将第 3 列(索引 2)删除。
当然,你也可以沿着纵轴,将第三行删除。
np.delete(a, 2, 0)
insert(arr,obj,values,axis):依据索引在特定轴之前插入值。
再看一看 insert插入, 用法和 delete 很相似,只是需要在第三个参数位置设置需要插入的数组对象:
import numpy as np
a = np.arange(12).reshape(3,4)
b = np.arange(4)
np.insert(a, 2, b, 0)
append(arr,values,axis):将值附加到数组的末尾,并返回 1 维数组。
append 的用法也非常简单。只需要设置好需要附加的值和轴位置就好了。它其实相当于只能在末尾插入的 insert,所以少了一个指定索引的参数。
import numpy as np
a = np.arange(6).reshape(2,3)
b = np.arange(3)
np.append(a, b)
注意 append方法返回值,默认是展平状态下的 1 维数组。
resize(a,new_shape):对数组尺寸进行重新设定。
resize 就很好理解了,直接举例子吧:
import numpy as np
a = np.arange(10)
a.resize(2,5)
你可能会纳闷了,这个 resize 看起来和上面的 reshape 一样呢,都是改变数组原有的形状。
其实,它们直接是有区别的,区别在于对原数组的影响。reshape 在改变形状时,不会影响原数组,相当于对原数组做了一份拷贝。而 resize 则是对原数组执行操作。
在 numpy 中,我们还可以对数组进行翻转操作:
fliplr(m):左右翻转数组。flipud(m):上下翻转数组。
举个例子:
import numpy as np
a = np.arange(16).reshape(4,4)
n.fliplr(a)
n.flipud(a)
Numpy 的随机抽样功能非常强大,主要由 numpy.random 模块完成。
首先,我们需要了解如何使用 numpy 也就是生成一些满足基本需求的随机数据。主要由以下一些方法完成:
numpy.random.rand(d0, d1, ..., dn) 方法的作用为:指定一个数组,并使用 [0, 1) 区间随机数据填充,这些数据均匀分布。
import numpy as np
np.random.rand(2,5)
numpy.random.randn(d0, d1, ..., dn) 与 numpy.random.rand(d0, d1, ..., dn) 的区别在于,返回的随机数据符合标准正太分布。
import numpy as np
np.random.randn(1,10)
randint(low, high, size, dtype) 方法将会生成 [low, high) 的随机整数。注意这是一个半开半闭区间。
import numpy as np
np.random.randint(2,5,10)
random_integers(low, high, size) 方法将会生成 [low, high] 的 np.int 类型随机整数。注意这是一个闭区间。
import numpy as np
np.random.random_integers(2,5,10)
random_sample(size) 方法将会在 [0, 1) 区间内生成指定 size 的随机浮点数。
import numpy as np
np.random.random_sample([10])
与 numpy.random.random_sample 类似的方法还有:
numpy.random.random([size])numpy.random.ranf([size])numpy.random.sample([size])
它们 4 个的效果都差不多。
choice(a, size, replace, p) 方法将会给定的 1 维数组里生成随机数。
import numpy as np
np.random.choice(10,5)
上面的代码将会在 np.arange(10) 中生成 5 个随机数。
除了上面介绍的 6 中随机数生成方法,numpy 还提供了大量的满足特定概率密度分布的样本生成方法。它们的使用方法和上面非常相似,这里就不再一一介绍了。列举如下:
numpy.random.beta(a,b,size):从 Beta 分布中生成随机数。numpy.random.binomial(n, p, size):从二项分布中生成随机数。numpy.random.chisquare(df,size):从卡方分布中生成随机数。numpy.random.dirichlet(alpha,size):从 Dirichlet 分布中生成随机数。numpy.random.exponential(scale,size):从指数分布中生成随机数。numpy.random.f(dfnum,dfden,size):从 F 分布中生成随机数。numpy.random.gamma(shape,scale,size):从 Gamma 分布中生成随机数。numpy.random.geometric(p,size):从几何分布中生成随机数。numpy.random.gumbel(loc,scale,size):从 Gumbel 分布中生成随机数。numpy.random.hypergeometric(ngood, nbad, nsample, size):从超几何分布中生成随机数。numpy.random.laplace(loc,scale,size):从拉普拉斯双指数分布中生成随机数。numpy.random.logistic(loc,scale,size):从逻辑分布中生成随机数。numpy.random.lognormal(mean,sigma,size):从对数正态分布中生成随机数。numpy.random.logseries(p,size):从对数系列分布中生成随机数。numpy.random.multinomial(n,pvals,size):从多项分布中生成随机数。numpy.random.multivariate_normal(mean, cov, size):从多变量正态分布绘制随机样本。numpy.random.negative_binomial(n, p, size):从负二项分布中生成随机数。numpy.random.noncentral_chisquare(df,nonc,size):从非中心卡方分布中生成随机数。numpy.random.noncentral_f(dfnum, dfden, nonc, size):从非中心 F 分布中抽取样本。numpy.random.normal(loc,scale,size):从正态分布绘制随机样本。numpy.random.pareto(a,size):从具有指定形状的 Pareto II 或 Lomax 分布中生成随机数。numpy.random.poisson(lam,size):从泊松分布中生成随机数。numpy.random.power(a,size):从具有正指数 a-1 的功率分布中在 0,1 中生成随机数。numpy.random.rayleigh(scale,size):从瑞利分布中生成随机数。numpy.random.standard_cauchy(size):从标准 Cauchy 分布中生成随机数。numpy.random.standard_exponential(size):从标准指数分布中生成随机数。numpy.random.standard_gamma(shape,size):从标准 Gamma 分布中生成随机数。numpy.random.standard_normal(size):从标准正态分布中生成随机数。numpy.random.standard_t(df,size):从具有 df 自由度的标准学生 t 分布中生成随机数。numpy.random.triangular(left,mode,right,size):从三角分布中生成随机数。numpy.random.uniform(low,high,size):从均匀分布中生成随机数。numpy.random.vonmises(mu,kappa,size):从 von Mises 分布中生成随机数。numpy.random.wald(mean,scale,size):从 Wald 或反高斯分布中生成随机数。numpy.random.weibull(a,size):从威布尔分布中生成随机数。numpy.random.zipf(a,size):从 Zipf 分布中生成随机数。
本章节介绍了针对 Ndarray 的常用操作,并了解了 Numpy.randon 类下的随机抽样方法。这两点内容都非常重要,也非常实用。由于随机抽样的方法太多,全部记忆下来不太实际,你可以多浏览几遍留下印象,需要时再查阅官方文档。numpy教程:数组操作
时间: 16:03:26
&&&& 阅读:556
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&http://Array manipulation routinesnumpy数组基本操作,包括copy, shape, 转换(类型转换), type, 重塑等等。这些操作应该都可以使用numpy.fun(array)或者array.fun()来调用。Basic operations(dst,&src[,&casting,&where])Copies values from one array to another, broadcasting as necessary.Changing array shape(a,&newshape[,&order])Gives a new shape to an array without changing its data.(a[,&order])Return a contiguous flattened array.A 1-D iterator over the array.属性,会改变原数组。([order])Return a copy of the array collapsed into one dimension.方法,不会改变原数组。Array的形态操作-numpy更改数组的形状与数组堆叠修改ndarray.shape属性&.shape · reshape() : 改变array的形态可以通过修改shape属性,在保持数组元素个数不变的情况下,改变数组每个轴的长度。下面的例子将数组c的shape改为(4,3),注意从(3,4)改为(4,3)并不是对数组进行转置,而只是改变每个轴的大小,数组元素在内存中的位置并没有改变:&&& c.shape = 4,3
array([[ 1,
当某个轴的元素为-1时,将根据数组元素的个数自动计算此轴的长度,因此下面的程序将数组c的shape改为了(2,6):&&& c.shape = 2,-1
array([[ 1,
使用数组的reshape方法,可以创建一个改变了尺寸的新数组,原数组的shape保持不变:&&& d = a.reshape((2,2))
array([[1, 2],
array([1, 2, 3, 4])
数组a和d其实共享数据存储内存区域,因此修改其中任意一个数组的元素都会同时修改另外一个数组的内容!&&& a[1] = 100 # 将数组a的第一个元素改为100
&&& d # 注意数组d中的2也被改变了
Note:&需要注意的是,这里与MATLAB不一样,MATLAB变换是按列向量来的,而NUMPY是基于行向量[[ 1. & 4. ]&[ 2.2 &5. ]&[ 3. & 6. ]]a.reshape(6,1)& -- 将3x2矩阵变成列向量(6x1)所以numpy的运行结果为:[[ 1. ]&[ 4. ]&[ 2.2]&[ 5. ]&[ 3. ]&[ 6. ]] (列向量)而MATLAB的运行结果为 :& 1 2.2 3 4 5 6 (列向量)注意: 对应的MATLAB很多向量默认为列向量,numpy中默认为行向量numpy中多维数组转换为一维向量&&& · flatten(): 复制一个一维的array出来ndarray.reshape(-1)& {shape: (4,)} 或者 a.reshape(1, -1) {(1, 4)}要注意的是reshape(返回?)后的数组不是原数组的复制,reshape前后的数组指向相同的地址(只是维度重新定义了一下)也可以用flatten函数将高维数组转化为向量,和reshape不同的是,flatten函数会生成原始数组的复制In [40]: a = np.array([[2,2], [2,3]])In [41]: a.flatten()Out[41]: array([2, 2, 2, 3])In [43]: a.reshape(-1)Out[43]: array([2, 2, 2, 3])但是像这种不规则维度的多维数组就不能转换成功了,还是本身 a = np.array([[[2,3]], [2,3]]).ravel() # flatten the array· resize(): 也是改变array的形态。不同的是,resize是直接修改这个对象的,而reshape则会生成一个新的对象Transpose-like operations(a,&axis[,&start])Roll the specified axis backwards, until it lies in a given position.(a,&axis1,&axis2)Interchange two axes of an array.Same as self.transpose(), except that self is returned if self.ndim & 2.(a[,&axes])Permute the dimensions of an array.· swapaxes(): 将n个维度中任意两个维度(坐标轴)进行调换· transpose(): 这个就是矩阵的转置操作Changing number of dimensions(*arys)Convert inputs to arrays with at least one dimension.(*arys)View inputs as arrays with at least two dimensions.(*arys)View inputs as arrays with at least three dimensions.Produce an object that mimics broadcasting.(array,&shape[,&subok])Broadcast an array to a new shape.(*args,&**kwargs)Broadcast any number of arrays against each other.(a,&axis)Expand the shape of an array.(a[,&axis])Remove single-dimensional entries from the shape of an array.squeeze(a)也就是将所有维度为1的维度去掉。这个操作应该等价于a.reshape(-1)。scipy.spatial.distance.euclidean()函数源码:u = np.asarray(u, dtype=dtype, order=‘c‘).squeeze()
# Ensure values such as u=1 and u=[1] still return 1-D arrays.使用示例1In [54]: a = np.array([[2,2], [2,3]])In [55]: aarray([[2, 2],&&&&&& [2, 3]])In [56]: a = a.reshape(1, -1)In [57]: aarray([[2, 2, 2, 3]])In [58]: a.shape&(1, 4)In [59]: a.squeeze().shape(4,)使用示例2In [19]: x = np.array([[[0], [1], [2]]])In [20]: np.squeeze(x)&&&&& #或者x.squeeze()Out[20]: array([0, 1, 2])In [21]: x.shapeOut[21]: (1, 3, 1)In [22]: np.squeeze(x).shapeOut[22]: (3,)Changing kind of array(a[,&dtype,&order])Convert the input to an array.(a[,&dtype,&order])Convert the input to an ndarray, but pass ndarray subclasses through.(data[,&dtype])Interpret the input as a matrix.(a[,&dtype])Return an array converted to a float type.(a[,&dtype])Return an array laid out in Fortran order in memory.(a[,&dtype])Return a contiguous array in memory (C order).(a[,&dtype,&order])Convert the input to an array, checking for NaNs or Infs.(a)Convert an array of size 1 to its scalar equivalent.(a[,&dtype,&requirements])Return an ndarray of the provided type that satisfies requirements.Joining arrays((a1,&a2,&...)[,&axis])Join a sequence of arrays along an existing axis.(arrays[,&axis])Join a sequence of arrays along a new axis.(tup)Stack 1-D arrays as columns into a 2-D array.(tup)Stack arrays in sequence depth wise (along third axis).(tup)Stack arrays in sequence horizontally (column wise).(tup)Stack arrays in sequence vertically (row wise).numpy更改数组的形状与数组堆叠numpy.concatenate()函数函数原型:numpy.concatenate((a1, a2, ...), axis=0)numpy.stack()函数函数原型:numpy.stack(arrays, axis=0)水平组合hstack和垂直组合vstack函数对那些维度比二维更高的数组,hstack沿着第二个轴组合,vstack沿着第一个轴组合,concatenate允许可选参数给出组合时沿着的轴。函数原型:numpy.hstack(tup)其中tup是arrays序列,The arrays must have the same shape, except in the dimensioncorresponding toaxis (the first, by default).等价于:np.concatenate(tup, axis=1)函数原型:numpy.vstack(tup)等价于:np.concatenate(tup, axis=0)&if tup contains arrays thatare at least 2-dimensional.new_matrix=np.hstack([mat1,mat2])& 或按行合并矩阵(要求两矩阵列数一样):new_matrix=np.vstack([mat1,mat2]) 合并矩阵的命令同样可以用于合并向量,但是合并向量的时候有时会提示行列数不对,那可能是因为一个的维度是(n个),而另一个的维度是(n列,1行),这种情况下,可用reshape来进行转换:array2=array2.reshape(n)new_array=np.hstack([array1,array2])Note:函数column_stack以列将一维数组合成二维数组,它等同与vstack对一维数组。row_stack函数,另一方面,将一维数组以行组合成二维数组。对那些维度比二维更高的数组,hstack沿着第二个轴组合,vstack沿着第一个轴组合,concatenate允许可选参数给出组合时沿着的轴。在复杂情况下,r_[]和c_[]对创建沿着一个方向组合的数很有用,它们允许范围符号(“:”):&&& r_[1:4,0,4]array([1, 2, 3, 0, 4])当使用数组作为参数时,r_和c_的默认行为和vstack和hstack很像,但是允许可选的参数给出组合所沿着的轴的代号。Note: numpy.hstack()和numpy.column_stack()函数略有相似,numpy.vstack()与numpy.row_stack()函数也是挺像的。[]深度组合numpy.dstack()在数组的第三个轴(即深度)上组合,对应的元素都组合成一个新的列表,该列表作为新的数组的元素。This is a simple way to stack 2D arrays (images) into a single 3D array for processing.函数原型:numpy.dstack(tup)等价于:np.concatenate(tup, axis=2)x, y = np.meshgrid(np.linspace(-1, 1, 3), np.linspace(-1, 1, 3))
print(‘x=\n‘, x)
print(‘y=\n‘,y)
print(‘stack = \n‘, np.dstack((x, y)))
&[[-1.& 0.& 1.]
&[-1.& 0.& 1.]
&[-1.& 0.& 1.]]
&[[-1. -1. -1.]
&[ 0.& 0.& 0.]
&[ 1.& 1.& 1.]]
&[[[-1. -1.]& [ 0. -1.]& [ 1. -1.]]
&[[-1.& 0.]& [ 0.& 0.]& [ 1.& 0.]]
&[[-1.& 1.]& [ 0.& 1.]& [ 1.& 1.]]]
可以看成是两个二维坐标值组合成三维坐标,可用于三维图形绘制。[]行组合row_stack行组合可将多个一维数组作为新数组的每一行进行组合&&& one = arange(2) &&&& one &array([0, 1]) &&&& two = one + 2 &&&& two &array([2, 3]) &&&& row_stack((one, two)) &array([[0, 1], &&&&&& [2, 3]]) &对于2维数组,其作用就像垂直组合一样。列组合column_stack&&& column_stack((oned, twiceoned)) &array([[0, 2], &&&&&& [1, 3]]) &对于2维数组,其作用就像水平组合一样。 不同stack函数使用示例In [3]: a = np.array([1, 2, 3])
In [4]: b = np.array([2, 3, 4])
In [6]: print(a)
In [7]: print(b)
&&& np.stack((a, b))
array([[1, 2, 3],
[2, 3, 4]])
&&& np.vstack((a,b))
array([[1, 2, 3],
[2, 3, 4]])&&& np.hstack((a,b))
array([1, 2, 3, 2, 3, 4])
&&& np.dstack((a,b))
array([[[1, 2],
[3, 4]]])In [8]: a = np.array([[1], [2], [3]])In [9]: b = np.array([[2], [3], [4]])In [11]: print(a)[[1]&[2]&[3]]In [12]: print(b)[[2]&[3]&[4]]&&& np.stack((a, b), axis=-1)
array([[1, 2],
&&& np.vstack((a,b))array([[1],&&&&&& [2],&&&&&& [3],&&&&&& [2],&&&&&& [3],&&&&&& [4]])In [14]: np.hstack((a,b))array([[1, 2],&&&&&& [2, 3],&&&&&& [3, 4]])In [15]: np.dstack((a,b))array([[[1, 2]],&&&&&& [[2, 3]],&&&&&& [[3, 4]]])#不处理xy的最后一列xy = np.hstack([preprocessing.scale(xy[:, 0:-1]), xy[:, -1].reshape(-1, 1)])[][]Splitting arrays(ary,&indices_or_sections[,&axis])Split an array into multiple sub-arrays.(ary,&indices_or_sections[,&axis])Split an array into multiple sub-arrays.(ary,&indices_or_sections)Split array into multiple sub-arrays along the 3rd axis (depth).(ary,&indices_or_sections)Split an array into multiple sub-arrays horizontally (column-wise).(ary,&indices_or_sections)Split an array into multiple sub-arrays vertically (row-wise).Tiling arrays:numpy多维数组重塑(A,&reps)Construct an array by repeating A the number of times given by reps.(a,&repeats[,&axis])Repeat elements of an array.tile函数模板numpy.lib.shape_base中的函数。函数的形式是tile(A,reps)A和reps都是array_like的,几乎所有类型都可以:array, list, tuple, dict, matrix以及基本数据类型int,string, float以及bool类型。reps的类型也很多,可以是tuple,list, dict, array, int,bool.但不可以是float,string, matrix类型。就是重塑后新数组A的对应维上重复多少次,并且从高维开始?假定A的维度为d,reps的长度为len当d&=len时,将reps长度补足为d,即在reps前面加上d-len个1。这里的意思是,假设A为k维数组,每一维都有一定长度,新的A构成的维度向量为D。而长度为len的reps有len个数,进行tile函数运算时补足d位,前面加d-len个1,如下图所示:经过tile运算,生成新的A,A的各维维度为:Note:相乘的意思为,将原来A中每一维度的元素进行copy,生成的A中此元素出现次数为新的reps对应维度的数目。操作从低维度向高维进行。举个栗子例一:&&& tile([[1,2,3],[4,5,5]],2)array([[1, 2, 3, 1, 2, 3],& & &&[4, 5, 5, 4, 5, 5]])Note:A的维度d=2 & len(reps)=1,这样reps补齐为(1,2),即A在0维上每个元素都copy为2倍,在1维上不copy.例二:W = np.tile([[3,5,6]],[3,1])
print(W)[[3 5 6]&[3 5 6]&[3 5 6]]例三:&&& tile([1,2,3],[2,2,2,2])array([[[[1, 2, 3, 1, 2, 3],& & && &[1, 2, 3, 1, 2, 3]],& & && [[1, 2, 3, 1, 2, 3],& & && &[1, 2, 3, 1, 2, 3]]],& & &&[[[1, 2, 3, 1, 2, 3],& & && &[1, 2, 3, 1, 2, 3]],& & && [[1, 2, 3, 1, 2, 3],& & && &[1, 2, 3, 1, 2, 3]]]])Note:A的维度d=1 & len(reps)=4,这样A在0维上每个元素都copy为2倍,在1维上每个元素都copy为2倍,在2维上每个元素都copy为2倍,在3维上每个元素都copy为2倍.[]repeatrepeat(6,axis=0)表示的是将a按照第一轴的方向扩展6次得到的数组。axis=0表示的是按照第一轴的方向操作,也就是列方向上;若是axis=1就是行方向上面;这个也是等价于axis=-1的。因为-1表示的是它的最后那个轴方向。所以也就是行方向上面。Adding and removing elements(arr,&obj[,&axis])Return a new array with sub-arrays along an axis deleted.(arr,&obj,&values[,&axis])Insert values along the given axis before the given indices.(arr,&values[,&axis])Append values to the end of an array.(a,&new_shape)Return a new array with the specified shape.(filt[,&trim])Trim the leading and/or trailing zeros from a 1-D array or sequence.(ar[,&return_index,&return_inverse,&...])Find the unique elements of an array.append函数(将一个列表加入多维数组ndarray中; 实现matlab &data=[data1;data2]的功能)data1 = random.randint(1, 10, (2, 3))
data2 = random.randint(-10, -1, (2, 3))
data = append(data1, data2, axis=0)
print(data1)
print(data2)
print(data)
[[ -3& -8& -6]
&[ -3 -10 -10]]
[[& 1&& 3&& 7]
&[& 8&& 3&& 3]
&[ -3& -8& -6]
&[ -3 -10 -10]]Rearranging elements(m)Flip array in the left/right direction.(m)Flip array in the up/down direction.(a,&newshape[,&order])Gives a new shape to an array without changing its data.(a,&shift[,&axis])Roll array elements along a given axis.(m[,&k])Rotate an array by 90 degrees in the counter-clockwise direction.from:&ref: 标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&原文地址:http://blog.csdn.net/pipisorry/article/details/
&&国之画&&&& &&&&chrome插件&&
版权所有 京ICP备号-2
迷上了代码!}

我要回帖

更多关于 numpy 三维数组 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信