您的位置:首页 > 产品设计 > UI/UE

《NumPy Beginner's Guide》笔记Chapter2

2016-03-12 12:49 127 查看
# -*- coding: utf-8 -*-

__author__ = 'ZengDong'
#日期 =  23:23
import numpy as np

"""
1. NumPy has a multi-dimensional array object called ndarray. It consists of two parts:
The actual data
Some metadata describing the data

2. The NumPy array is in general homogeneous, the items in the array have to be of the same type
主要包括:float, integers, strings
而list则可以包括各种不同类型

ndarray  Vs  list/dictionary
快,类型少           灵活,类型多,慢

"""
a = np.arange(5)
print(a.dtype)    #输出  int32

print(a.shape)    #输出  (5L,)

#Create a multidimensional array.
m = np.array([np.arange(2), np.arange(2)])
print(m.shape)   #输出(2L, 2L)

#Selecting elements
"""
3. From time to time, we will want to select a particular element of an array.

the indices are numbered starting from 0
matlab中好像是a(m)(n)  m表示行,n表示列  且从1开始计数
numpy中是     a[m]
同上             但从0开始计数
"""
a = np.array([[1, 2], [3, 4]])
print(a)
print(a[0][0])  #输出1
print(a[1][1])  #输出4

"""
4. NumPy numerical types

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 and
imaginary components)
complex128 or
complex
Complex number, represented by two 64-bit floats (real and
imaginary components)
"""
a = np.arange(7, dtype=np.int64)
print(a.dtype)   #输出int64
print(a.dtype.itemsize)

"""
5. One-dimensional slicing and indexing
Slicing of one-dimensional NumPy arrays works just like slicing of Python lists. (Python基础教程Page31)
eg: a[left:right:step]
"""
a = np.arange(9)
print(a)    #输出 [0 1 2 3 4 5 6 7 8]
print(a[3:7])   #输出  [3 4 5 6]
print(a[:7:2])  #输出  [0 2 4 6]
print(a[::-1])   #输出 [8 7 6 5 4 3 2 1 0]

"""
6. slicing and indexing multidimensional arrays
b的排布:
(0,1,2,3)     (4,5,6,7)    (8,9,10,11)
(12,13,14,15) (16,17,18,19) (20,21,22,23)

(1) reshape()函数
(2) b.shape直接赋值等价(1)
(3)resize
eg:
print("@@@@")
b.resize(2, 12)
print(b)

"""
b = np.arange(24).reshape(2, 3, 4)
print(b.shape)    #输出(2L, 3L, 4L)
print(b)

print(b[:, 0, 0])  #输出[0 12]
print(b[0])       #输出[ 0  1  2  3] [ 4  5  6  7]  [ 8  9 10 11]
print(b[0, :, :])   #同上
print(b[0, ...])    #同上

print(b[0, 1, ::2])   #输出[4 6]
print(b[..., 1])   #输出 [ 1  5  9] [13 17 21]

print(b[0, ::-1, -1])  #输出[11 7 3]

print("#########################")
b.shape = (6, 4)
print(b)

"""
7. ravel()函数   flattening arrays  和 reshap()函数一样是浅复制
flatten()  函数    功能和ravel()一样,  但是开辟新的存储空间

"""
flatten = b.flatten()
flatten[0 : 3] = 0
print(b)     #b和原来一样

ravel = b.ravel()
ravel[0 : 3] = 0;
print(b)     #b发生了变化

"""
8. 转置  transpose()
"""
print(b.transpose())

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""以上总结:
We manipulated the shapes of NumPy arrays using the ravel function, the flatten
function, the reshape function, and the resize method.
ravel
flatten
reshape
resize
"""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
print("------------------------------------------------------------------------------------------")

"""
1. stacking : Arrays can be stacked horizontally, depth-wise, or vertically.
vstack
dstack
hstack
column_stack
row_stack
concatenate

"""

a = np.arange(9).reshape(3, 3)
print(a)
b = 2 * a
print(b)

# (1)   horizontal stacking
print(np.hstack((a, b)))
"""
输出:
[[ 0  1  2  0  2  4]
[ 3  4  5  6  8 10]
[ 6  7  8 12 14 16]]
"""

#we can achieve the same with the [concatenate] function
print(np.concatenate((a, b), axis = 1))  #输出同上
print(np.concatenate((a, b), axis = 0))  #vertical stack

# (2) vertical stacking
print(np.vstack((a, b)))
"""
输出:
[[ 0  1  2]
[ 3  4  5]
[ 6  7  8]
[ 0  2  4]
[ 6  8 10]
[12 14 16]]
"""
#同理:
print(np.concatenate((a, b), axis = 0))

# (3) depth stacking
print(np.dstack((a, b)))
"""
输出:
[[[ 0  0]
[ 1  2]
[ 2  4]]

[[ 3  6]
[ 4  8]
[ 5 10]]

[[ 6 12]
[ 7 14]
[ 8 16]]]

"""

# (4) column stacking
"""
1. The column_stack function stacks 1D arrays column-wise.
2. 2D arrays are stacked the way hstack stacks them:
"""
oned = np.arange(2)
twice_onde = 2 * oned
print(np.column_stack((oned, twice_onde)))
"""
输出:
[[0 0]
[1 2]]
"""

print(np.column_stack((a, b)) == np.hstack((a, b)))
"""
输出:
[[ True  True  True  True  True  True]
[ True  True  True  True  True  True]
[ True  True  True  True  True  True]]
"""

# (5) row stacking
"""
1. for 1D arrays, it just stacks the arrays in rows into a 2D array.
2. The row_stack function results for 2D arrays are equal to, yes, exactly,
the vstack function results.
"""
print(np.row_stack((oned, twice_onde)))
"""
输出:
[[0 1]
[0 2]]
"""
print(np.row_stack((a, b)))
"""
输出:
[[ 0  1  2]
[ 3  4  5]
[ 6  7  8]
[ 0  2  4]
[ 6  8 10]
[12 14 16]]
"""
print(np.row_stack((a, b)) == np.vstack((a, b)))
"""
输出:
[[ True  True  True]
[ True  True  True]
[ True  True  True]
[ True  True  True]
[ True  True  True]
[ True  True  True]]
"""

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""以上总结:
We stacked arrays horizontally, depth-wise, and vertically. We used the vstack, dstack,
hstack, column_stack, row_stack, and concatenate functions
vstack
dstack
hstack
column_stack
row_stack
concatenate
"""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
print("------------------------------------------------------------------------------------------")

"""
1. Arrays can be split vertically, horizontally, or depth wise. The functions involved are hsplit,
vsplit, dsplit, and split. We can either split into arrays of the same shape or indicate
the position after which the split should occur.
"""

"""
1.1 Horizontal splitting  : splits an array along its horizontal axis
"""
print(np.hsplit(a, 3))
"""
输出:
[array([[0],
[3],
[6]]),
array([[1],
[4],
[7]]),
array([[2],
[5],
[8]])]
"""
print(np.split(a, 3, axis=1))
"""
输出:与上面等价
[array([[0],
[3],
[6]]),
array([[1],
[4],
[7]]),
array([[2],
[5],
[8]])]
"""

"""
1.2 Vertical splitting  : splits an array along its vertical axis
"""
print(np.vsplit(a, 3))
"""
输出:
[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
"""
print(np.split(a, 3, axis=0))
"""
输出:与上面等价
[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
"""

"""
1.3 Depth-wise splitting  : splits an array along its depth-wise
"""
c = np.arange(27).reshape(3, 3, 3)
print(c)
"""
输出:  【VIP  ?? 】numpy三维分布和opencv不一样,先存c[0][0][:] = [0,1,2]
[[[ 0  1  2]
[ 3  4  5]
[ 6  7  8]]

[[ 9 10 11]
[12 13 14]
[15 16 17]]

[[18 19 20]
[21 22 23]
[24 25 26]]]
"""
print(np.dsplit(c, 3))
"""
输出:
[array([[[ 0],
[ 3],
[ 6]],

[[ 9],
[12],
[15]],

[[18],
[21],
[24]]]), array([[[ 1],
[ 4],
[ 7]],

[[10],
[13],
[16]],

[[19],
[22],
[25]]]), array([[[ 2],
[ 5],
[ 8]],

[[11],
[14],
[17]],

[[20],
[23],
[26]]])]

"""

print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")

"""
1. Array attributes
shape
dtype
ndim
size
"""

"""
1.1 ndim : gives the number of dimensions:
"""
print(b)
print(b.ndim)   #输出  2

"""
1.2 size : contains the number of elements
"""

print(b.size)   #输出  9

"""
1.3 itemsize : gives the number of bytes for each element in the array
"""

print(b.itemsize)   #输出  4   (32bit)

"""
1.4 nbytes : the total number of bytes the array requires
nbytes = size * itemsize
"""

print(b.nbytes)   #输出 36
print(b.nbytes == b.size*b.itemsize)   #输出  True

"""
1.5 nbytes : T   (The T attribute has the same effect as the transpose function)

"""

print(b.transpose() == b.T)
"""
输出:
[[ True  True  True]
[ True  True  True]
[ True  True  True]]
"""

"""
1.6 complex numbers: represented by j
real:   gives us the real part of the array
imag:   contains the imaginary part of the array

"""

b = np.array([1.j + 1, 2.j + 3])
print(b)    #输出: [ 1.+1.j  3.+2.j]
print(b.real)  #输出: [ 1.  3.]
print(b.imag)  #输出: [ 1.  2.]
"""
输出:
[[ True  True  True]
[ True  True  True]
[ True  True  True]]
"""

"""
2.tolist:
convert a NumPy array to a Python list with the tolist function:
"""
c = np.arange(9)
d = c.tolist()
print(d)
print(type(d))    #输出 <type 'list'>

"""
3.astype:
converts the array to an array of the specified type
"""
b = np.array([1.+1.j, 3.+2.j])
b1 = b.astype(int)
print(b1)    #输出 [1 3]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  numpy