NumPy array of common operations

NumPy array of common operations

 

Import NumPy

NumPy is an external library. It refers to the so-called external libraries not included in the standard Python them. So I want to use the need to introduce in advance.

import numpy as np

 which introduced a python library files need to be introduced by import. import herein may be literally translated as "the np NumPy as introduction." After NumPy method can be invoked by both np.

 

NumPy array generation

To generate a NumPy array, use np.array () this api.

The list of parameters for receiving api python, thereby generating NumPy array (numpy.ndarray).

 

Import numpy AS NP 

X = np.array ([1.0,2.0,3.0 ]) 

Print (X) 

'' ' 
output results: 
[1.] 
' '' 

# can view the type by type 
type (X) 

'' ' 
output: 
<class' numpy.ndarray '> 
' ''

 NumPy arithmetic

The following is an example of the arithmetic operation NumPy array:

Import numpy NP AS 

# numpy arithmetic operation of 

X = np.array ([l, 2,3 ]) 
Y = np.array ([4,5,6 ]) 

Print (X + Y) # [5,7,9 ] 


A = np.array ([1.0,2.0,3.0 ]) 
B = np.array ([4.0,5.0,6.0 ]) 

Print (A + B) # [5. The, 7. The,. 9.] 

# when two the number of elements in the array are the same, the operation may be made. But if the number of different elements, the program will error. 
# "Corresponds to the element" which translates to element-wise multiplication of the corresponding element is the element-wise product such as

 

 

In addition NumPy array can summation may be performed other mathematical operations.

 Not only can NumPy array element-wise operation, and may be a single value (scalar) calculates combination. In this case, operation is required between the various elements and scalar NumPy array. This feature is also known as broadcast.

>>> x = np.array([1.0, 2.0, 3.0])
>>> x / 2.0
array([ 0.5, 1. , 1.5])

 

N-dimensional array of NumPy

NumPy can not only generate one-dimensional arrays (arranged in an array), you can also generate multidimensional arrays. For example, two-dimensional array may be generated as follows (the matrix).

>>> A = np.array([[1, 2], [3, 4]])
>>> print(A)
[[1 2]
[3 4]]
>>> A.shape
(2, 2)
>>> A.dtype
dtype('int64')

 

 

In NumPy array, () method see shape, () methods by looking at the type through dtype shape.

 

Matrix arrays can also perform mathematical operations.

>>> A = np.array([[1, 2], [3, 4]])
>>> B = np.array([[3, 0],[0, 6]]) >>> A + B array([[ 4, 2], [ 3, 10]]) >>> A * B array([[ 3, 0], [ 0, 24]])

 

And arithmetic operations, like array, the matrix arithmetic operation may be performed in a manner corresponding element in the matrix between the same shape. And may be performed by a matrix arithmetic scalar (single value). It is also based broadcast capabilities.

>>> A = np.array([[1, 2], [3, 4]])
>>> A * 10 array([[ 10, 20], [ 30, 40]])

 

NumPy array (np.array) N-dimensional array may be generated, which can generate an array of any number of dimensions one-dimensional array, two dimensional arrays, three dimensional arrays like. Mathematically one-dimensional array called a vector, a two-dimensional array called a matrix. Further, after the generalized vector or other matrix may be referred to as tensor (tensor).

 

broadcast

NumPy, the array of different shapes may be operated. In the previous example, between the matrix and the scalar A 2 × 2 10 of the multiplication. In this process, the scalar 10 is expanded into the shape of a 2 × 2, then the multiplication with the matrix A. This clever feature called Radio (broadcast).

 

 

The following operation is an example of the broadcast.

>>> A = np.array([[1, 2], [3, 4]])
>>> B = np.array([10, 20])
>>> A * B
array([[ 10, 40],
[ 30, 80]])

 

In the above operation, one-dimensional array B is "cleverly" into the group A and two bits of the same shape, and then as to the corresponding elements operated.

 

 

In sum, because there NumPy broadcasting function, it is also possible to smoothly carry out operation between the array of different shapes.

 

Among the elements of the array access

Index of the element to start from zero. Access to the respective elements proceed as follows.

= X-np.array >>> ([[51 is, 55], [14,. 19], [0,. 4 ]])
 >>> Print (X-) 
[[ 51 is 55 ] 
[ 14. 19 ] 
[0 . 4 ]]
 >>> X-[0]
 # row 0 
Array ([51 is, 55 ])
 >>> X-[0] [. 1] # (0,1) of the element 55

 

You can also be used for access to the various elements of the statement.

>>> X = np.array([[51, 55], [14, 19], [0, 4]])
>>> for row in X: ... print(row) ... [51 55] [14 19] [0 4]

 

In addition to indexing operation described previously, NumPy may also be used to access the various elements of the array.

X = >>> X.flatten () # X-converted to one-dimensional array 
>>> Print (X) 
[ 51 is 55 14. 19 0. 4 ]
 >>> X [np.array ([0, 2,. 4]) ] # Get index of 0,2,4 element 
array ([51, 14, 0 ])

 

Such markers by the above manner can be acquired elements meet certain conditions. For example, to get the element is greater than 15, can be written in the following format.

Import numpy NP AS   

A = np.array ([[11,22,33], [44,55,66], [77,88,99 ]]) 


B = a.flatten () # convert a tensor dimensional array 
Print (B) # [. 11 22 is 33 is 44 is 55 66 77 88 99] 

# acquiring a one-dimensional array by array element 
# Print (B [np.array ([1,3,5,7])]) # [22 is 66 88 44 is] 

Print (B> 15)   # [True True True False True True True True True] 

Print (B [B> 15])   # [22 is 33 is 44 is 55 66 77 88 99]

 

Guess you like

Origin www.cnblogs.com/qfxz/p/11654628.html