python Numpy学习(二)

1.组合(stack):

#coding=utf-8
import numpy as np

# stack different arrays
a = np.floor(10*np.random.random((2,2)))
print a
'''
[[4. 4.]
 [7. 2.]]
'''
b = np.floor(10*np.random.random((2,2)))
print b
'''
[[4. 7.]
 [9. 0.]]
'''
print np.vstack((a,b)) # 将a和b纵向组合在一起
'''
[[4. 4.]
 [7. 2.]
 [4. 7.]
 [9. 0.]]
'''
print np.hstack((a,b))  #将a和b横向组合在一起
'''
[[4. 4. 4. 7.]
 [7. 2. 9. 0.]]
'''

注意:行向组合(hstack)得保障行数一样;纵向组合(stack)得保障列数一样。

#coding=utf-8
import numpy as np

# stack different arrays
a = np.floor(10*np.random.random((2,2)))
print a
'''
[[6. 0.]
 [9. 8.]]
'''
b = np.floor(10*np.random.random((2,3)))
print b
'''
[[9. 0. 5.]
 [6. 8. 3.]]
'''
# 函数column_stack以列将一维数组合成二维数组,它等同与vstack对一维数组
print np.column_stack((a,b)) # # With 2D arrays
'''
[[6. 0. 9. 0. 5.]
 [9. 8. 6. 8. 3.]]
'''

2.数组增加维度:

#coding=utf-8
import numpy as np

a = np.array([4.,2.])
print a.ndim  #1
b = np.array([2.,8.])
c=a[:,np.newaxis] # This allows to have a 2D columns vector
print c
'''
[[4.]
 [2.]]
'''
print c.ndim  #2

#coding=utf-8
import numpy as np

a = np.array([4.,2.])
print a.ndim  #1
b = np.array([2.,8.])
c=a[:,np.newaxis] # This allows to have a 2D columns vector
print c
'''
[[4.]
 [2.]]
'''
print c.ndim  #2
print np.column_stack((a[:,np.newaxis],b[:,np.newaxis]))
'''
[[4. 2.]
 [2. 8.]]
'''
print np.vstack((a[:,np.newaxis],b[:,np.newaxis])) #The behavior of vstack is different
'''
[[4.]
 [2.]
 [2.]
 [8.]]
'''

3.分割。使用hsplit你能将数组沿着它的水平轴分割,或者指定返回相同形状数组的个数,或者指定在哪些列后发生分割:

#coding=utf-8
import numpy as np

# split an array into several pieces
a = np.floor(10*np.random.random((2,12)))
print a
'''
[[7. 8. 9. 0. 1. 3. 6. 9. 6. 4. 8. 3.]
 [4. 1. 8. 6. 8. 6. 8. 6. 5. 2. 6. 8.]]
'''
print np.hsplit(a,3) # split into 3
'''
[array([[7., 8., 9., 0.],
       [4., 1., 8., 6.]]), array([[1., 3., 6., 9.],
       [8., 6., 8., 6.]]), array([[6., 4., 8., 3.],
       [5., 2., 6., 8.]])]
'''
print np.hsplit(a,4)
'''
[array([[7., 8., 9.],
       [4., 1., 8.]]), array([[0., 1., 3.],
       [6., 8., 6.]]), array([[6., 9., 6.],
       [8., 6., 5.]]), array([[4., 8., 3.],
       [2., 6., 8.]])]
'''
print np.hsplit(a,(3,4)) # split a after the third and the fourth column
'''
[array([[7., 8., 9.],
       [4., 1., 8.]]), array([[0.],
       [6.]]), array([[1., 3., 6., 9., 6., 4., 8., 3.],
       [8., 6., 8., 6., 5., 2., 6., 8.]])]
'''
# vsplit沿着纵向的轴分割,array split允许指定沿哪个轴分割

4.拷贝

(1)完全不拷贝:简单的赋值不拷贝数据对象或其数据

#coding=utf-8
import numpy as np

# 1.totally not copied
a = np.arange(12)  #[ 0  1  2  3  4  5  6  7  8  9 10 11]
print a
b = a
print b  #[ 0  1  2  3  4  5  6  7  8  9 10 11]
print b is a  #True
b.shape = 3,4 # change the shape of a
print a.shape  #(3, 4)
print a
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

'''

(2)视图view和浅复制:不同的数组对象分享同一个数据,视图方法创造一个新的数组对象指向同一数据

#coding=utf-8
import numpy as np

a = np.arange(12)  #[ 0  1  2  3  4  5  6  7  8  9 10 11]
print a
b = a
#print b is a
b.shape = 3,4 # change the shape of a
print a
# 2.view and shadow copy
# 不同的数组对象分享同一个数据。视图方法创造一个新的数组对象指向同一数据,数据其实是存在源数组的
c = a.view()
print 'c:',c
print c is a   #False
print c.base is a # c is a vie of the data owned by a   #True
print c.flags.owndata # judge,c has no data    #False
c.shape = 2,6
print a.shape # chage c's shape but a's shape goesnbt change   #(3,4)
c[0,4] = 1234
print a # a's data changes
'''
[[   0    1    2    3]
 [1234    5    6    7]
 [   8    9   10   11]]
'''
# 切片数组返回它的一个视图:
s = a[:,1:3] # spaces added for clarity; could also be written "s = a[:,1:3]"
s[:] = 10 #  # s[:] is a view of s. Note the difference between s=10 and s[:]=10
print a
'''
[[   0   10   10    3]
 [1234   10   10    7]
 [   8   10   10   11]]
'''

(3)深复制:完全赋值数据及其数据

#coding=utf-8
import numpy as np

a = np.arange(12)  #[ 0  1  2  3  4  5  6  7  8  9 10 11]
print a
d = a.copy() # a new array object with new data is created
print d  #[ 0  1  2  3  4  5  6  7  8  9 10 11]
print d is a  #False
print d.base is a # d doesn't share anything with a  #False
d[0] = 9999
print a #[ 0  1  2  3  4  5  6  7  8  9 10 11]
print d  #[9999    1    2    3    4    5    6    7    8    9   10   11]

5.函数

'''
# 1.创建数组
    arange, array, copy, empty_like, eye, fromfile, fromfunction, identity, linspace, logspace, mgrid, ogrid, ones, ones_like, r, zeros, zeros_like
  ​
    # 2.转化 
    astype, atleast ld, atleast 2d, atleast 3d, mat
  ​
    # 3.操作
    array split, column stack, concatenate, diagonal, dsplit, dstack, hsplit, hstack, item, newsxis,ravel, repeat, reshape, resize, squeeze,swapaxes, take,transpose
  ​
    # 4.询问
    all, any, nonzero, where
  ​
    # 5.排序
    argmax, argmin, argsort, max, min, ptp, searchsorted, sort
  ​
    # 6.运算
    choose, compress, cumprod, cumsum, inner, fill, imag, prod, putmask, real, sum
  ​
    # 7.基本统计
    cov, mean, std, var
  ​
    # 8.基本线性代数
    cross, dot, outer, svd, vdot
'''

猜你喜欢

转载自blog.csdn.net/nanxiaoting/article/details/80612507
今日推荐