Numpy Learning

一些numpy学习要注意的地方

  • resize和reshape的不同
import numpy as np
a = np.array([[ 2.,  8.,  0.,  6.],[ 4.,  5.,  1.,  1.],[ 8.,  9.,  3.,  6.]])
a.reshape(1,1)  # 输出失败,行列和原来不相等
a.reshape(1, -1)    # 自动计算行列
a.resize((1,1)) # 没有强制要求,无所谓,数列变成(1,1)
print(a)
  • 显示所有被省略的数据
import numpy as np
print(np.arange(10000))
# 这里讲所有的数据都显示了出来
# 但是注意,使用了‘np.nan’作为参数如果不指定import的numpy为np的话,结果如下
np.set_printoptions(threshold=np.nan)# 不指定module
from numpy import *
print(arange(10000))
set_printoptions(threshold=nan)
  • 横向一条一条堆叠
from numpy import newaxis
import numpy as np
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print(a ,'\n\n', b, '\n\n')
print (np.column_stack((a,b)))
#[[ 0.  8.]
#[ 2.  4.]] #[[ 8.  1.]
#[ 7.  5.]] #[[ 0.  8.  8.  1.]
# [ 2.  4.  7.  5.]]# 将原来的数组进行更高维度的装换,之后拼接
c = np.column_stack((a[:,newaxis],b[:,newaxis]))
print (c)
c.shape # (2, 2, 2)
#[[ 3.  1.]
# [ 8.  5.]] # [[ 6.  3.]
# [ 0.  7.]] #[[[ 3.  1.]
#  [ 6.  3.]]
# [[ 8.  5.]
#  [ 0.  7.]]]# For arrays of with more than two dimensions, hstack stacks along their second axes,  vstack stacks along their first axes, and concatenate allows for an optional arguments giving the number of the axis along which the concatenation should happen.
  • 数组拆分
from numpy import newaxis
import numpy as npa = np.floor(10*np.random.random((2,12)))
print(a)
a.shape
print(100* '*')# 原始的数组竖直切断,分成原始数组的3份的数组
c = np.hsplit(a,3)   # Split a into 3
print(c)
print(len(c))
print(c[0].shape)
print(100* '*')new = np.floor(10*np.random.random((2,12)))
print(new)
print(100* '*')
d = np.hsplit(new,(3,4))   # Split a after the third and the fourth column
print(d)
print(len(d))
i = 0
while i < len(d):   print(d[i].shape)i = i + 1
print(100* '*')
[[ 9.  7.  3.  5.  6.  8.  5.  0.  3.  8.  4.  2.][ 6.  9.  4.  5.  8.  0.  9.  7.  7.  5.  4.  9.]]
****************************************************************************************************
[array([[ 9.,  7.,  3.,  5.],[ 6.,  9.,  4.,  5.]]), array([[ 6.,  8.,  5.,  0.],[ 8.,  0.,  9.,  7.]]), array([[ 3.,  8.,  4.,  2.],[ 7.,  5.,  4.,  9.]])]
3   # 分成组数
(2, 4)  # 每个拆分的size
****************************************************************************************************
[[ 9.  6.  2.  7.  3.  5.  3.  0.  1.  4.  3.  5.][ 2.  9.  1.  7.  9.  0.  8.  7.  4.  5.  1.  8.]]
****************************************************************************************************
[array([[ 9.,  6.,  2.],[ 2.,  9.,  1.]]), array([[ 7.],[ 7.]]), array([[ 3.,  5.,  3.,  0.,  1.,  4.,  3.,  5.],[ 9.,  0.,  8.,  7.,  4.,  5.,  1.,  8.]])]
3 # 分成的组数
(2, 3)  # 每个拆分的size
(2, 1)
(2, 8)
***************************************************************************************************
  • 创建一个array的view,这个根据数据类型不同可能有不同拷贝,如果需要一个完全新的不受影响的数据,就使用copy()
from numpy import newaxis
import numpy as npa = np.arange(12).reshape(2,6)
c = a.view()
print(a, type(a))
print(c is a)print(c.base is a)                        # c is a view of the data owned by aprint(c.flags.owndata)c.resize(3, 4)                      # a's shape doesn't change
print(a.shape)c[0,2] = 1234                      # a's data changes
print(a)
print(100 * '-')
print(c)
 # 原始的a生成的数组,(2, 6)
[[ 0  1  2  3  4  5][ 6  7  8  9 10 11]] <class 'numpy.ndarray'>
# c is a 
False
# c.base is a
False
False
# a原始数组的大小
(2, 6)
# a
[[   0    1 1234    3    4    5][   6    7    8    9   10   11]]
----------------------------------------------------------------------------------------------------
# c, 改变了c的大小
[[   0    1 1234    3][   4    5    6    7][   8    9   10   11]]
  • 索引
from numpy import newaxis
import numpy as npa = np.arange(12)**2                       # the first 12 square numbers
i = np.array( [ 1,1,3,8,5 ] )              # an array of indices
print(a)
# [  0   1   4   9  16  25  36  49  64  81 100 121]j = np.array( [ [ 3, 4], [ 9, 7 ] ] )      # a bidimensional array of indices
# 这里支持交叉组合索引
print(a[j])# [[ 9 16]
# [81 49]]


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部