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 np

a = 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 np

a = 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 a

print(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 np

a = 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]]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值