numpy学习整理

xiaoxiao2021-02-28  107

今天先整理到这里,剩下的下次再整理 1.改变形状: reshape()返回改变的数组形状,但无法改变源数组形状 resize() 可以改变源数组形状 ravel() 输出类似C数组的列表,和reshape()一样,返回C似的数组但无法改变源数组形状 例如:

>>> from numpy import * >>> c = arange(24) >>> print c [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23] >>> c.resize(4,6) >>> print c [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]] >>> c.reshape(3,8) array([[ 0, 1, 2, 3, 4, 5, 6, 7], [ 8, 9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22, 23]]) >>> print c [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]] >>> print c.ravel() [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23] >>> print c [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]]

2.组合(stack)不同的数组 vstack():横向组合数组 hstack():纵向组合数组 column_stack():纵向组合数组,和hstack()效果一样,区别在哪,目前我也不懂… row_stack(): 横向组合数组,和vstack()效果一样,区别在哪,目前我也不懂…

>>> b = floor(10*random.random((2,2))) >>> a = floor(10*random.random((2,2))) >>> a array([[ 6., 3.], [ 9., 9.]]) >>> b array([[ 9., 3.], [ 6., 5.]]) >>> column_stack((a,b)) array([[ 6., 3., 9., 3.], [ 9., 9., 6., 5.]]) >>> hstack((a,b)) array([[ 6., 3., 9., 3.], [ 9., 9., 6., 5.]]) >>> row_stack((a,b)) array([[ 6., 3.], [ 9., 9.], [ 9., 3.], [ 6., 5.]]) >>> vstack((a,b)) array([[ 6., 3.], [ 9., 9.], [ 9., 3.], [ 6., 5.]]) >>>

3.复制(视图复制) 不同的数组对象分享同一个数据。视图方法创造一个新的数组对象“指向”同一数据。视图复制之后,有独立的数据形状 但是这是浅复制,数据是同步的

>>> a = arange(12).reshape((3,4)) >>> a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> c = a.view() >>> c array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> c is a False >>> c.resize((2,6)) >>> c array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11]]) >>> a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> c[0] = 1234 >>> c array([[ 1234, 1234, 1234, 1234, 1234, 1234], [ 6, 7, 8, 9, 10, 11]]) >>> a array([[1234, 1234, 1234, 1234], [ 1234, 1234, 6, 7], [ 8, 9, 10, 11]])
转载请注明原文地址: https://www.6miu.com/read-17755.html

最新回复(0)