numpy beginner 3rdv ch2a (jupyter note)

numpy beginner 3rdv ch2a (jupyter note)

In [3]:

#coding=utf-8
import numpy as np
a = np.arange(5)
print(a)
print(a.dtype)
print(a.shape)

Output:

[0 1 2 3 4]
int32
(5,)

In [15]:

m = np.array([np.arange(2),np.arange(2,4)])
print(m)
print(m.shape)
print(m[1,1],m[1,0],m[0,1],m[0,0])

Output:

[[0 1]
 [2 3]]
(2, 2)
3 2 1 0

In [18]:

#Type: Description
#bool: Boolean (True or False) stored as a bit
#inti: Platform integer (normally either int32 or int64)
#int8: Byte (-128 to 127)
#int16: Integer (-32768 to 32767)
#int32: Integer (-2 ** 31 to 2 ** 31 -1)
#int64: Integer (-2 ** 63 to 2 ** 63 -1)
#uint8: Unsigned integer (0 to 255)
#uint16: Unsigned integer (0 to 65535)
#uint32: Unsigned integer (0 to 2 ** 32 - 1)
#uint64: Unsigned integer (0 to 2 ** 64 - 1)
#float16: Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
#float32: Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
#float64: or float Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
#complex64: Complex number, represented by two 32-bit floats (real andimaginary components)
#complex128(OR complex): Complex number, represented by two 64-bit floats (real andimaginary components)
# to show details of floating type 
np.finfo(np.float16)

Output:

Out[18]:

finfo(resolution=0.001, min=-6.55040e+04, max=6.55040e+04, dtype=float16)

In [21]:

ar = np.arange(7,dtype=np.uint16)
print(ar)
print(ar.dtype)
print(ar[3].dtype)

Output:

[0 1 2 3 4 5 6]
uint16
uint16

In [ ]:

# not recommended, only for backward compatible
#Type                     Character code
#Integer                  i
#Unsigned integer         u
#Single precision float   f
#Double precision float   d
#Boolean                  b
#Complex                  D
#String                   S
#Unicode                  U
#Void                     V

Output:

In [27]:

cr = np.arange(7, dtype='D')
print(cr)
print(cr.dtype)
print(cr[3].dtype)

Output:

[0.+0.j 1.+0.j 2.+0.j 3.+0.j 4.+0.j 5.+0.j 6.+0.j]
complex128
complex128

In [34]:

#all data type names
np.sctypeDict.keys()

Output:

Out[34]:

dict_keys(['?', 0, 'byte', 'b', 1, 'ubyte', 'B', 2, 'short', 'h', 3, 'ushort', 'H', 4, 'i', 5, 'uint', 'I', 6, 'intp', 'p', 9, 'uintp', 'P', 10, 'long', 'l', 7, 'L', 8, 'longlong', 'q', 'ulonglong', 'Q', 'half', 'e', 23, 'f', 11, 'double', 'd', 12, 'longdouble', 'g', 13, 'cfloat', 'F', 14, 'cdouble', 'D', 15, 'clongdouble', 'G', 16, 'O', 17, 'S', 18, 'unicode', 'U', 19, 'void', 'V', 20, 'M', 21, 'm', 22, 'bool8', 'Bool', 'b1', 'float16', 'Float16', 'f2', 'float32', 'Float32', 'f4', 'float64', 'Float64', 'f8', 'complex64', 'Complex32', 'c8', 'complex128', 'Complex64', 'c16', 'object0', 'Object0', 'bytes0', 'Bytes0', 'str0', 'Str0', 'void0', 'Void0', 'datetime64', 'Datetime64', 'M8', 'timedelta64', 'Timedelta64', 'm8', 'int32', 'uint32', 'Int32', 'UInt32', 'i4', 'u4', 'int64', 'uint64', 'Int64', 'UInt64', 'i8', 'u8', 'int16', 'uint16', 'Int16', 'UInt16', 'i2', 'u2', 'int8', 'uint8', 'Int8', 'UInt8', 'i1', 'u1', 'complex_', 'int0', 'uint0', 'single', 'csingle', 'singlecomplex', 'float_', 'intc', 'uintc', 'int_', 'longfloat', 'clongfloat', 'longcomplex', 'bool_', 'unicode_', 'object_', 'bytes_', 'str_', 'string_', 'int', 'float', 'complex', 'bool', 'object', 'str', 'bytes', 'a'])

In [59]:

tt = np.dtype([('name', np.str_, 40), ('numitems', np.int32), ('price',np.float32)])
print(t)
tt['name']

Output:

[('name', '<U40'), ('numitems', '<i4'), ('price', '<f4')]
Out[59]:

dtype('<U40')

In [60]:

#default is floating for array
itemz = np.array([('Meaning of life DVD', 42, 3.14), ('Butter', 13,2.72)], dtype=tt)
print(itemz)
print(itemz[1])

Output:

[('Meaning of life DVD', 42, 3.14) ('Butter', 13, 2.72)]
('Butter', 13, 2.72)

In [71]:

aa = np.arange(9)
print(aa[3:7])
print(aa[:7:2])
print(aa[::-1])

Output:

[3 4 5 6]
[0 2 4 6]
[8 7 6 5 4 3 2 1 0]

In [77]:

#3d array
bb = np.arange(24).reshape(2,3,4)
print(bb.shape)
print(bb)
print(bb[0,0,0])
print("第一维度全部,第二维度第2个,第三维度第2个:相当于在plane0/1上分别找[1,1]")
print(bb[:,1,1])
print("第一维度全部,第二维度第1个,第三维度全部:相当于在plane0/1上分别找第一行")
print(bb[:,0,:])

Output:

(2, 3, 4)
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
0
第一维度全部,第二维度第2个,第三维度第2个:相当于在plane0/1上分别找[1,1]
[ 5 17]
第一维度全部,第二维度第1个,第三维度全部:相当于在plane0/1上分别找第一行
[[ 0  1  2  3]
 [12 13 14 15]]

In [83]:

print("剩余所有维度全要")
print("way #1:")
print(bb[0,...])
print("way #2:")
print(bb[1])
print(bb[0,1])

Output:

剩余所有维度全要
way #1:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
way #2:
[[12 13 14 15]
 [16 17 18 19]
 [20 21 22 23]]
[4 5 6 7]

In [91]:

print(bb[0,1,::2])
print("指定最后一个维度的1位置:")
print(...,1)
print(bb[...,1])
print("指定第二个维度的1位置:")
print(bb[:,1])

Output:

[4 6]
指定最后一个维度的1位置:
Ellipsis 1
[[ 1  5  9]
 [13 17 21]]
指定第二个维度的1位置:
[[ 4  5  6  7]
 [16 17 18 19]]

In [94]:

print(bb[0,:,-1])
print(bb[0,::-1,-1])
print("注意,下面的逆转仅仅发生在第一维度")
print(bb[::-1])

Output:

[ 3  7 11]
[11  7  3]
注意,下面仅仅发生在第一维度
[[[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]

 [[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]]

In [102]:

#flatten,打平
#ravel只是视图,与源数据有关联
print(bb.ravel())
#flatten,简历新的array
print(bb.flatten())

Output:

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]

In [108]:

bb.shape = (6,4)
print(bb)
bb = bb.reshape(4,6)
print(bb)
print(bb.transpose())
bb.resize(2,12)
print(bb)

Output:

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]
 [20 21 22 23]]
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]
[[ 0  6 12 18]
 [ 1  7 13 19]
 [ 2  8 14 20]
 [ 3  9 15 21]
 [ 4 10 16 22]
 [ 5 11 17 23]]
[[ 0  1  2  3  4  5  6  7  8  9 10 11]
 [12 13 14 15 16 17 18 19 20 21 22 23]]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值