Original: https://blog.csdn.net/fu6543210/article/details/83240024
Why use numpy
Python provides a list of the container can be used like an array. However, the list element can be any object, so the list is stored in a pointer to an object, so that, in order to preserve a simple list [1,2,3]. It requires three pointers and integers three objects. For the numerical calculation, this structure is clearly not efficient enough.
Although Python provides a module array, but supports only one-dimensional array, it does not support multi-dimensional arrays (in TensorFlow matrix which tend to be understood), nor various arithmetic functions. And is not suitable numerical calculation.
NumPy appeared to make up for these deficiencies.
(- THE CHEMICAL taken from the "Python scientific computing")
import numpy as np
Array creation
## conventional creation method
A = np.array ([2,3,4])
B = np.array ([2.0,3.0,4.0])
C = np.array ([[1.0,2.0], [3.0, 4.0 ]])
D = np.array ([[1,2], [3,4-]], DTYPE = Complex) specify the data type #
Print A, a.dtype
Print B, b.dtype
Print C, c.dtype
Print d, d.dtype
[2 3 4] int32
[ 2. 3. 4.] float64
[[ 1. 2.]
[ 3. 4.]] float64
[[ 1.+0.j 2.+0.j]
[ 3.+0.j 4.+0.j]] complex128
Common function array
print np.arange (0,7,1, dtype = np.int16 ) # 0 as a starting point, the interval is 1 may be a default (default is not caused by the ambiguity)
Print np.ones ((2,3 , 4), dtype = np.int16) # 2 pages, row 3, 4, a whole, specify the data type
print np.zeros ((2,3,4)) # 2 pages, row 3, 4, the whole 0
Print np.empty ((2,3)) # value depends on the memory
print np.arange (0,10,2) # 0 as the start point, no more than 10, a step size of 2
Print np.linspace (-1,2 5) -1 # starting point, end point 2, to take five points
random integer print np.random.randint (0,3, (2,3) ) # than 0 and less than 3,2 rows and 3 columns
[0 1 2 3 4 5 6]
[[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]]
[[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]]
[[1.39069238e-309 1.39069238e-309 1.39069238e-309]
[1.39069238 309-1.39069238e E-309 1.39069238e-309]]
[0 2 4 6 8]
[-1. -0.25 0:51:25 2.]
[[1 0 1]
[0 1 0]]
Type conversion
Print a float (. 1)
Print int (1.0)
Print BOOL (2)
Print a float (True)
1.0
1
True
1.0
Array output
From left to right, from top to bottom
one-dimensional array to print a row, a two-dimensional matrix array of printing, printing three-dimensional array in a matrix listing
np.arange Print (1,6,2)
Print np.arange (12 is) .reshape (3,4-) # shape can change the output
print np.arange (24) .reshape (2,3,4 ) # 2 pages, line 3, 4
[1 3 5]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
Basic operations
Stage operational element ##
A = np.array ([1,2,3,4])
B = np.arange (. 4)
Print A, B
Print ab &
Print A * B
Print A ** 2
Print 2 * np.sin (A)
Print A> 2
Print np.exp (A) index #
[1 2 3 4] [0 1 2 3]
[1 1 1 1]
[ 0 2 6 12]
[ 1 4 9 16]
[ 1.68294197 1.81859485 0.28224002 -1.51360499]
[False False True True]
[ 2.71828183 7.3890561 20.08553692 54.59815003]
## matrix operation (two-dimensional array)
A = np.array ([[1,2], [3,4-]]) # 2 rows 2
b = np.arange (6) .reshape ( (2, -1 )) # 2 rows 3
Print A, B
Print a.dot (B) rows and three columns # 2
[[1 2]
[3 4]] [[0 1 2]
[3 4 5]]
[[ 6 9 12]
[12 19 26]]
## is not an array operator, invoke methods
A np.random.randint = (0,5, (2,3))
Print A
Print a.sum (), a.sum (Axis =. 1), a.sum (0) # axis specifies the operation axis (default all, or may specify 0. 1)
Print a.min (), a.max (axis =. 1), a.mean (axis =. 1) # axis = 0: calculated column by column, axis = 1: calculated row
print a.cumsum (1) # rows and calculates a cumulative
[[2 3 3]
[0 2 1]]
11 [8 3] [2 5 4]
0 [3 2] [ 2.66666667 1. ]
[[2 5 8]
[0 2 3]]
Indexing, slicing, iterative
## one-dimensional array
A np.arange = (0,10,1) ** 2
Print A
Print A [0], A [2], A [-1], A [-2] # index starts from 0, -1 is the last index
print a [2: 5], a [-5: -1] # including the starting point, the end point does not include
a [-1] = 100; print a # assigned
a [1: 4] = 100 ; batch print a # assigned
a [: 6: 2] = -100; print a # assigned from the beginning to the sixth index, every element (step 2 =)
Print a [:: -1]; print a # the output of a reverse, not itself a change
b = [np.sqrt (np.abs (i )) for i in a]; print b # assigned by traversing
[ 0 1 4 9 16 25 36 49 64 81]
0 4 81 64
[ 4 9 16] [25 36 49 64]
[ 0 1 4 9 16 25 36 49 64 100]
[ 0 100 100 100 16 25 36 49 64 100]
[-100 100 -100 100 -100 25 36 49 64 100]
[ 100 64 49 36 25 -100 100 -100 100 -100]
[-100 100 -100 100 -100 25 36 49 64 100]
[10.0, 10.0, 10.0, 10.0, 10.0, 5.0, 6.0, 7.0, 8.0, 10.0]
Multidimensional arrays ##
A = np.arange (0,20) .reshape ((4,5))
Print A, A [2,3], A [:,. 1], A [. 1: 4,2], A : [13 ,:]
when print a [-1] # corresponds to a [-1 ,:], i.e. the number of axes is less than the index, the index does default entire slice
np.arange = B (0,24) .reshape ((2,3,4))
Print B, B [. 1] # corresponds to b [1,:,:] and B [. 1, ...]
Print ' ------------------- '
for in Row a:
Print Row # traversed to the first axis based
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]] 13 [ 1 6 11 16] [ 7 12 17] [[ 5 6 7 8 9]
[10 11 12 13 14]]
[15 16 17 18 19]
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
[[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]
Operation shape
np.floor = a (10 * np.random.random ((3,4-)))
Print a, a shape a.shape # output
print a.ravel a post () # output planarization (a itself does not change )
a.shape = (6,2); Print # changing a shape of a
print a.transpose () # output a transpose
[[ 0. 4. 3. 2.]
[ 1. 1. 3. 3.]
[ 4. 4. 6. 5.]] (3, 4)
[ 0. 4. 3. 2. 1. 1. 3. 3. 4. 4. 6. 5.]
[[ 0. 4.]
[ 3. 2.]
[ 1. 1.]
[ 3. 3.]
[ 4. 4.]
[ 6. 5.]]
[[ 0. 3. 1. 3. 4. 6.]
[ 4. 2. 1. 3. 4. 5.]]
## supplement: reshape and a resize
A = np.array ([[l, 2,3], [4,5,6]])
B = A
a.reshape ((3,2-)) does not change the array itself # shape
Print A
b.resize ((3,2-)) # array itself changes the shape of
print b
[[1 2 3]
[4 5 6]]
[[1 2]
[3 4]
[5 6]]
---------------------
In numpy module, we often use the resize and reshape, in particular use is usually used to change the array size resize, reshape used to increase the dimension of the array.
1.resize
Before seeing someone else's blog said, resize no return value, in fact, depending on how you use resize, resize used in two ways, one is no return value directly to the original data to be modified, there is a usage is returns a value, it will not modify the original array values.
1.1 has a return value, does not modify the original data
import numpy as np
X=np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12]])
X_new=np.resize(X,(3,3)) # do not change the original X
print("X:\n",X) #original X
print("X_new:\n",X_new) # new X
>>
X:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
X_new:
[[1 2 3]
[4 5 6]
[7 8 9]]
1.2 no return value, directly modifying the original size of the array
import numpy as np
X=np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12]])
X_2=X.resize((3,3)) #change the original X ,and do not return a value
print("X:\n",X) # change the original X
print("X_2:\n",X_2) # return None
X:
[[1 2 3]
[4 5 6]
[7 8 9]]
X_2:
None
2.reshape
To an array of new data without changing the shape of
import numpy as np
X=np.array([1,2,3,4,5,6,7,8])
X_2=X.reshape((2,4)) #retuen a 2*4 2-dim array
X_3=X.reshape((2,2,2)) # retuen a 2*2*2 3-dim array
print("X:\n",X)
print("X_2:\n",X_2)
print("X_3:\n",X_3)
>>
X:
[1 2 3 4 5 6 7 8]
X_2:
[[1 2 3 4]
[5 6 7 8]]
X_3:
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
---------------------
Author: ink Xiaobai
---------------------
Author: furuit
source: CSDN
original: https: //blog.csdn.net/fu6543210/article/details/83240024
copyright: This article is a blogger original article, reproduced, please attach Bowen link!