This is the 17th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

NumPy- Get started quickly

Array creation

NumPy is characterized by its N-dimensional array object NDARray.

Ndarray is a collection of data of the same type.

The ndarray object is used to hold multi-dimensional arrays of elements of the same type.

import numpy as np
a=np.array([1.2.3])
print(a)
print(type(a))
Copy the code

Creating an interval array

The arange argument (initial value, end value, step size) does not contain an end value

import numpy as np
a=np.arange(1.10.3)
print(a)
b=np.arange(1.10.0.5)
print(b)
Copy the code

Creating a two-dimensional array

Ndim dimension

Shape Number of rows and columns

Number of size elements

import numpy as np
arr=np.array([[1.2], [4.5], [7.8]])
print(arr)
print(arr.ndim)
print(arr.shape)
print(arr.size)
Copy the code

Specify the length of each dimension

Ones live 1 array

import numpy as np
arr=np.ones(shape=(3.4.5))
print(arr)
Copy the code

An array of attributes

Common attributes for array objects are: nDIM, Shape, DType, size, and ItemSize.

Ndim is used to return the dimension of the array

Shape returns the shape of an array

Dtype is used to return the data type of the array

Size is used to return the number of elements in the array

Itemsize returns the size, in bytes, of each element in the array.

import numpy as np
arr=np.array([[1.2], [3.6]])
print(arr.ndim)
print(arr.shape)
print(arr.dtype)
print(arr.size)
print(arr.itemsize)
Copy the code

The data type

When creating an array, you can use dtype to specify the types of the elements in the array.

If no element type is specified, inferences are made based on the element type.

If the type of the element is different, a compatible type is selected.

import numpy as np
a=np.array([1.5.9.0],dtype=np.float32)
b=np.array([1.'a'.2])
print(a.dtype)
print(b.dtype)
Copy the code

Astype () performs type conversion

import numpy as np
a=np.array([1.5.9],dtype=np.string_)
print(a)
a=a.astype(np.uint)
print(a)
print(a.dtype)
Copy the code

The 0 0 method changes the shape of the array.

import numpy as np
a=np.arange(15)
print(a)
b=np.reshape(a,(3.5))
print(b)
Copy the code

Set the multidimensional

Dimension-1 indicates that the size of the dimension is automatically calculated

import numpy as np
a=np.arange(20)
print(a)
b=np.reshape(a,(-1.2.5))
print(b)
Copy the code

Index and slice

Select multiple elements

import numpy as np
a=np.arange(20)
print(a)
print(a[0],a[10])
a=a.reshape((4.5))
print(a)
print(a[2.0])
Copy the code

Slicing returns a view of the original array object

import numpy as np
a=np.arange(20)
b=a[0:5]
a[2] =33
print(a)
print(b)
Copy the code

Copy () returns a copy of the array if you want it to be true

import numpy as np
a=np.arange(20)
print(a)
b=a.copy()
b=b[0:5]
a[2] =33
print(a)
print(b)
Copy the code

Condition index

import numpy as np
a=np.arange(20)
print(a)
b=a[a%2= =0]
print(b)
Copy the code

Array flattening

Ravel () returns a view of the original array

Flatten () returns a copy of the original array

import numpy as np
a=np.arange(15).reshape(3.5)
b=a.ravel()
c=a.flatten()
a[0.0] =1
print(a)
print(b)
print(c)
Copy the code

Store order

The order argument specifies the order in which the array elements are stored

import numpy as np
a=np.array([[1.2.3], [4.5.6]])
b=a.reshape((3.2),order="C")
c=a.reshape((3.2),order="F")
print(a)
print(b)
print(c)
Copy the code

function

statistical

When the array is a two-dimensional array, axis=0 is counted vertically and axis=1 is counted horizontally

import numpy as np
a=np.array([[1.2.3], [4.5.6]])
print(np.sum(a,axis=0))
print(np.sum(a,axis=1))
Copy the code

The commonly used statistical functions are as follows:

① Mean ()/sum()/median(). Mean/sum/median

② Max ()/min()/amax()/amin(). Maximum/minimum/maximum/minimum value

③argmax()/argmin()/ STD ()/var(). Maximum index/minimum index/standard deviation/variance

(4) cumsum (a)/cumprod (). Add up

random

Common random functions are:

(1) np. Random. Rand ()

(2) np. Random. The random ()

(3) np. Random. Randn ()

④ Probability density function of NP.random. Normal () gaussian distribution

⑤ Np.random.randint () the number of random integers

⑥ Np.random. Seed () Random number seed

⑦ NP. Random. Shuffle (

⑧ NP. Random. Uniform () random sampling

import numpy as np
a=np.random.rand(2.2)
b=np.random.random(size=(2.2))
c=np.random.randn(2.2)
print(a)
print(b)
print(c)
Copy the code

The connection

Concatenate () concatenates arrays along the specified axis

import numpy as np
a=np.arange(6).reshape((2.3))
b=np.arange(6).reshape((2.3))
print(a)
print(b)
print(np.concatenate((a,b),axis=0))
print(np.concatenate((a,b),axis=1))
Copy the code

other

Any () : Returns True if any element in the array is True (or can be converted to True), False otherwise.

All () : Returns True if all elements in the array are True (or can be converted to True), False otherwise.

Transpose (T) functions default to matrix transpose when no arguments are specified.

Specify the parameter transpose ((0,1)) to change the sequence according to the original coordinate axis, i.e.

Transpose ((1, 0)) means to swap axis 0 and 1.

import numpy as np
a=np.arange(6).reshape((3.2))
print(a)
print(a.transpose())
Copy the code