Numpy learning record

Posted by bongbong on Fri, 04 Mar 2022 23:40:13 +0100

Application of time date correlation - datetime64

numpy.busday_offset

(dates, offsets, roll='raise', weekmask='1111100', holidays=None, busdaycal=None, out=None) First adjusts the date to fall on a valid day according to the roll rule, then applies offsets to the given dates counted in valid days.

  • This function is used to determine whether the date after the offset is a working day
  • Parameter offset: indicates the offset. offset=0 directly determines whether dates is a working day. If yes, it directly returns true. Offset is another number num, and then judge whether num days are working days forward or backward.
  • Parameter roll: {raise ',' nat ',' forward ',' following ',' backward ',' preceding ',' modified following ',' modified preceding '}
    'raise' : raise an exception for an invalid day.
    'nat' : return a NaT (not-a-time) for an invalid day.
    'forward' and 'following' : take the first valid day later in time.
    ‘backward’ and ‘preceding’ mean to take the first valid day earlier in time. One is to take the first valid working day forward, and the other is to take the first valid working day backward
a = np.busday_offset('2020-07-11', offsets=1, roll='forward')
b = np.busday_offset('2020-07-11', offsets=1, roll='backward')
print(a)  # 2020-07-14
print(b)  # 2020-07-13

numpy.is_busday

(dates, weekmask='1111100', holidays=None, busdaycal=None, out=None) Calculates which of the given dates are valid days, and which are not.****

  • This function is used to determine whether dates is a working day
  • Parameter weekmask: Custom week mask value, that is, specify which days of the week are working days. The meaning of this function is the same as that of the function above

numpy.busday_count

(begindates, enddates, weekmask='1111100', holidays=[], busdaycal=None, out=None)Counts the number of valid days between begindates and enddates, not including the day of enddates.

  • This function is used to calculate the number of weekday intervals between two dates
import numpy as np

# Friday, July 10, 2020
begindates = np.datetime64('2020-07-10')
enddates = np.datetime64('2020-07-20')
a = np.busday_count(begindates, enddates)
b = np.busday_count(enddates, begindates)
print(a)  # 6
print(b)  # -6

How to create an array

import numpy as np

array()

  • Create a one-dimensional array
# Create a one-dimensional array
a = np.array([0, 1, 2, 3, 4])
b = np.array((0, 1, 2, 3, 4))
print(a, type(a))
# [0 1 2 3 4] <class 'numpy.ndarray'>
print(b, type(b))
# [0 1 2 3 4] <class 'numpy.ndarray'>

The list and tuple in the function are OK, and the last is a one-dimensional array

  • Create a 2D array
# Create a 2D array
c = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
print(c, type(c))
# [[11 12 13 14 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]
#  [26 27 28 29 30]
#  [31 32 33 34 35]] <class 'numpy.ndarray'>

and so on

fromfunction()

def fromfunction(function, shape, **kwargs):
def f(x, y):
    return 10 * x + y
x = np.fromfunction(f, (5, 4), dtype=int)
print(x)
# [[ 0  1  2  3]
#  [10 11 12 13]
#  [20 21 22 23]
#  [30 31 32 33]
#  [40 41 42 43]]

x = np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int)
print(x)
# [[ True False False]
#  [False  True False]
#  [False False  True]]
# Usage of lambda: input ij, output i==j?,
# The input of ij is defined as 0-2 in fromfunction

ones and zeros filling methods

zeros() function: returns an array of zeros for a given shape and type.

def zeros(shape, dtype=None, order='C')
x = np.zeros(5)
print(x)  # [0. 0. 0. 0. 0.]
x = np.zeros([2, 3])
print(x)
# [[0. 0. 0.]
#  [0. 0. 0.]]

The ones() function returns an array of 1 for a given shape and type.

def ones(shape, dtype=None, order='C'):
x = np.ones(5)
print(x)  # [1. 1. 1. 1. 1.]
x = np.ones([2, 3])
print(x)
# [[1. 1. 1.]
#  [1. 1. 1.]]

Empty array or random number

empty() function: returns an empty array whose elements are random numbers.

def empty(shape, dtype=None, order='C'): 
x = np.empty(5)
print(x)
# [1.95821574e-306 1.60219035e-306 1.37961506e-306 
#  9.34609790e-307 1.24610383e-306]

Use numeric ranges to create

At present, two functions are mainly used:

range() function: returns the value of uniform interval within a given interval.

def arange([start,] stop[, step,], dtype=None): 

numpy.random.rand() returns an array of random numbers in [0,1].

def rand(d0, d1, ..., dn): 

other:
numpy.random.randint (low, high, size): returns a random integer in a semi open interval [low, high].
numpy.random.standard_normal(size=None): produce a floating-point number or N-dimensional floating-point array, and the fetching range: standard normal distribution random samples.
More references:
https://blog.csdn.net/ywx1832990/article/details/81455042

x = np.arange(5)
print(x)  # [0 1 2 3 4]
# By default, it starts from 0, the step size is 1, and 5 numbers are created.

x = np.arange(3, 7, 2)
print(x)  # [3 5]
# 3 starts and 7 ends in steps of 2
x = np.random.random(5)
print(x)
# [0.41768753 0.16315577 0.80167915 0.99690199 0.11812291]

x = np.random.random([2, 3])
print(x)
# [[0.41151858 0.93785153 0.57031309]
#  [0.13482333 0.20583516 0.45429181]]

np. random. The random () function indicates the number of elements in parentheses or generates several rows and columns of elements, which are distributed between 0 and 1.

How to convert pictures into np arrays?

import numpy as np
from PIL import Image

img1 = Image.open('test.jpg')
a = np.array(img1)

print(a.shape, a.dtype)
# (959, 959, 3) uint8

Some operations on arrays

Change the shape of the array

  • numpy.ndarray.shape

numpy.ndarray.shape represents the dimension of the array and returns a tuple. The length of this tuple is the number of dimensions, that is, ndim attribute (rank).
Change the shape of the array by modifying the shape property

x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape)  # (8,)
x.shape = [2, 4]
print(x)
# [[1 2 9 4]
#  [5 6 7 8]]
  • numpy.ndarray.flat // numpy.ndarray.flatten([order='C'])
    numpy.ndarray.flat converts the array into a one-dimensional iterator, and each element of the array can be accessed with for.
    (for example, when you want to output by column, you can first convert it into a one-dimensional array by column, and then output it)
x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = x.flat
print(y)

numpy. ndarray. Convert a one-dimensional copy of the array 'C =], and return it.
Parameter order: 'C' - by row, 'F' - by column, 'A' - original order, 'k' - order of elements in memory.

  • numpy.reshape(a, newshape[, order='C'])

numpy.reshape(a, newshape[, order = 'C')) assigns a new shape to the array without changing the data.
reshape() function will automatically determine the number of columns according to the number of rows when the parameter newshape = [rows,-1].

x = np.arange(12)
y = np.reshape(x, [3, 4])
print(y)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

y = np.reshape(x,[-1,3])
print(y)
# [[ 0  1  2]
#  [ 3  4  5]
#  [ 6  7  8]
#  [ 9 10 11]]

reshape() function: when the parameter newshape = -1, it means to reduce the array to one dimension.

Transpose of array / matrix

Two expressions
The first one: after defining an array X, X.T directly represents transpose
The second method: after defining an array X, use the method transfer in np
y = np.transpose(X)

x = np.random.rand(5, 5) * 10
x = np.around(x, 2) #Keep two decimal places
print(x)
# [[6.74 8.46 6.74 5.45 1.25]
#  [3.54 3.49 8.62 1.94 9.92]
#  [5.03 7.22 1.6  8.7  0.43]
#  [7.5  7.31 5.69 9.67 7.65]
#  [1.8  9.52 2.78 5.87 4.14]]
y = x.T
print(y)
# [[6.74 3.54 5.03 7.5  1.8 ]
#  [8.46 3.49 7.22 7.31 9.52]
#  [6.74 8.62 1.6  5.69 2.78]
#  [5.45 1.94 8.7  9.67 5.87]
#  [1.25 9.92 0.43 7.65 4.14]]
y = np.transpose(x)
print(y)
# [[6.74 3.54 5.03 7.5  1.8 ]
#  [8.46 3.49 7.22 7.31 9.52]
#  [6.74 8.62 1.6  5.69 2.78]
#  [5.45 1.94 8.7  9.67 5.87]
#  [1.25 9.92 0.43 7.65 4.14]]

Change dimension

Many toolkits will first judge whether the dimension of the input data meets the requirements during calculation. If the input data cannot reach the specified dimension, you can use the newaxis parameter to add a dimension.
Specific use:

x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape)  # (8,)
print(x)  # [1 2 9 4 5 6 7 8] one dimensional (can also be used as a vector)

y = x[np.newaxis, :]
print(y.shape)  # (1, 8)
print(y)  # [[1 2 9 4 5 6 7 8]] becomes two-dimensional

y = x[:, np.newaxis]
print(y.shape)  # (8, 1)
print(y)
# [[1]
#  [2]
#  [9]
#  [4]
#  [5]
#  [6]
#  [7]
#  [8]]

numpy.squeeze(a, axis=None) deletes a single dimension entry from the shape of the array, that is, the dimension with 1 in the shape is removed.
a represents the input array;
axis is used to specify the dimension to be deleted, but the specified dimension must be a single dimension, otherwise an error will be reported;
In machine learning and deep learning, the result of the algorithm is usually an array that can represent vectors (that is, it contains two or more pairs of square brackets [[]]). If you directly use this array to draw, the display interface may be empty (see the following example). We can use the squeeze() function to convert the array representing the vector into an array with rank 1, so that when drawing with the matplotlib library function, the results can be displayed normally.

x = np.array([[[0], [1], [2]]])
print(x.shape)  # (1, 3, 1)
print(x)
# [[[0]
#   [1]
#   [2]]]

y = np.squeeze(x)
print(y.shape)  # (3,)
print(y)  # [0 1 2]

Splice array

  • numpy.concatenate

numpy.concatenate((a1, a2, ...), axis=0, out=None) Join a sequence of arrays along an existing axis.
The dimension of the array is consistent with that of the original one-dimensional array, which is still one-dimensional after being spliced; It turned out to be a two-dimensional array, which is also two-dimensional when spliced.

x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.concatenate([x, y])
print(z)
# [1 2 3 7 8 9]

x = np.array([1, 2, 3]).reshape(1, 3)
y = np.array([7, 8, 9]).reshape(1, 3)
z = np.concatenate([x, y])
print(z)
# [[ 1  2  3]
#  [ 7  8  9]]
  • numpy.stack

numpy.stack(arrays, axis=0, out=None)Join a sequence of arrays along a new axis. Add a new dimension based on the original one.
Add a series of arrays to the new axis (stack is the splicing of added dimensions)

x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.stack([x, y])
print(z.shape)  # (2, 3)
print(z)
# [[1 2 3]
#  [7 8 9]]

hstack(),vstack() indicates the horizontal and vertical splicing methods respectively. When the data dimension is equal to 1, it is special. When the dimension is greater than or equal to 2, their function is equivalent to concatenate, which is used to operate on the existing axis.

section

copy is a part of array elements. The general format is [start:stop:step],
By default:
Omitting the first number, numpy will think that the first number is 0;
Omitting the second number, numpy will think that the second number is the maximum index value of the array;
Omitting the last number, it will be understood as 1, which is to extract all elements without considering the interval.
If the step size is negative, it means flipping.

The slice of multi-dimensional array has one more parameter than that of one-dimensional array
e.g. x[0, 1:4]
You can slice multidimensional arrays by performing separate slices for each comma separated dimension. Therefore, for two-dimensional arrays, our first slice defines the slice of rows and the second slice defines the slice of columns.

(if you need anything else, please add it again. I don't think it's possible to use it

Topics: Python numpy