Numpy

numpy

  • you should learn some basic math,like matrix,integral,probablity and statistics.
  • You can learn https://numpy.org from if you wanna more

get function reference

print(help(numpy.xxxx))

Constructing a multidimensional array

numpy.array()

It must force conversion if the data is not the same type in array:

eg:

# constructing a array
# one dimensional array
vector = numpy.array([5,10,15,20])
# 2D array,action it has two []
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print(vector)
print(matrix)

It’s hard to constructing a array which have dimensional more than three
mumpy/0.ipynb/13*14

Checking the structure of a array,we mostly use it to debug.

print(xxxx.shape)

search

print(xxxx[x,y])
print(xxxxx[,1]) # get the first row
print(xxxx[:,0:2]) #get the first and the second row

split

print(xxxx[n:m])

Is it the value in array ?

xxx == m #

Change the type of all value in array

v = numpy.array(["1","2","3"])
print(v.dtype)
print(v)
v = v.astype(float)
print(v.dtype)
print(v)

Maxima and minima

get value in assignable array

# get sum in the same line(row)
m = numpy.array([
    [5,10,15],
    [20,25,30],
    [35,40,45]
])
m.sum(axis=1)
# answer:
# array([ 30,  75, 120])
#get sum in the same column
m = numpy.array([
    [5,10,15],
    [20,25,30],
    [35,40,45]
])
m.sum(axis=0)#answer:
#array([60, 75, 90])

changing array into matrix

import numpy as np
print(np.arange(15))
a = np.arange(15).reshape(3,5)
a
# [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
# array([[ 0,  1,  2,  3,  4],
#        [ 5,  6,  7,  8,  9],
#        [10, 11, 12, 13, 14]])

get the dimension of a array

print(xxx.ndim)

get the number of one array

print(xxx.size)

the matrix is initialized to zero

import numpy as np
np.zeros((3,4))
# array([[0., 0., 0., 0.],
#        [0., 0., 0., 0.],
#        [0., 0., 0., 0.]])

assign data type

np.ones((2,3,4),dtype=np.int32)

# array([[[1, 1, 1, 1],
#         [1, 1, 1, 1],
#         [1, 1, 1, 1]],

#        [[1, 1, 1, 1],
#         [1, 1, 1, 1],
#         [1, 1, 1, 1]]])

get an sequence

np.arange(10,30,5)#start 10 and end at 30,and the first number and 5 add up to  the next one  
np.arange(10,30,5).reshape(4,3)# you should pay attention to the number of an array
# array([[10, 15],
#        [20, 25]])

Random function

np.random.random((2,3)) # the first random is to get the random model and the second is get to  random function,(2,3) means we can get an 2*3 array
# array([[0.05134094, 0.63073588, 0.14218974],
#        [0.86727903, 0.95890848, 0.39738407]])

get M individual average number between x and y;

np.linspace[x,y,m]

np.linspace(2,3,5)
# array([2.  , 2.25, 2.5 , 2.75, 3.  ])

math

import numpy as np
a = np.array([20,30,40,50])
b = np.arange(4)
print(a)
print(b)
print("a - b " , a - b) # it will subtracted form the same place
print("a - b - 1 :" , a - b - 1) 
print("b**2" , b**2)
print("a < 35" , a < 35)
# [20 30 40 50]
# [0 1 2 3]
# a - b  [20 29 38 47]
# a - b - 1 : [19 28 37 46]
# b**2 [0 1 4 9]
# a < 35 [ True  True False False]

matrix multiplication

A = np.array([
    [1,1],
    [0,1]
])
B = np.array([
    [2,0],
    [3,4]
])
print('------A-------')
print(A)
print('------B-------')
print(B)
print('------A*B-------')
print(A*B) #
print('------A.dot(B)-------')
print(A.dot(B)) # matrix multiplication
print('------np.dot(A,B)-------')
print(np.dot(A,B)) # another way to get matrix multiplication
# ------A-------
# [[1 1]
#  [0 1]]
# ------B-------
# [[2 0]
#  [3 4]]
# ------A*B-------
# [[2 0]
#  [0 4]]
# ------A.dot(B)-------
# [[5 4]
#  [3 4]]
# ------np.dot(A,B)-------
# [[5 4]
#  [3 4]]

math formula

e and square

import numpy as np
B = np.arange(3)
print(B)
print(np.exp(B)) # e**B
print(np.sqrt(B)) # _/`B``
# [0 1 2]
# [1.         2.71828183 7.3890561 ]
# [0.         1.         1.41421356]
import numpy as np
a = np.floor(10*np.random.random((3,4))) # np.floor() //get int value down
print(a)
print('-------------')
print(a.ravel()) # transform matrix into vector
print('-------------')
a.shape = (3,4) # tranform vector into matrix
#
# a.shape = (3,-1)
# it will automatically get another dimension if the second is -1;
#
print(a)
print('-------------')
print(a.T) # matrix transpose
# [[3. 5. 8. 6.]
#  [5. 6. 6. 7.]
#  [1. 6. 2. 5.]]
# -------------
# [3. 5. 8. 6. 5. 6. 6. 7. 1. 6. 2. 5.]
# -------------
# [[3. 5. 8. 6.]
#  [5. 6. 6. 7.]
#  [1. 6. 2. 5.]]
# -------------
# [[3. 5. 1.]
#  [5. 6. 6.]
#  [8. 6. 2.]
#  [6. 7. 5.]]

links matrix

# links matrix
import numpy as np
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print('----------a-----------')
print(a)
print('----------b-----------')
print(b)
print('----------------------')
print(np.hstack((a,b))) # links matrix by row
print('----------------------')
print(np.vstack((a,b))) # links matrix by column
# ----------a-----------
# [[2. 0.]
#  [9. 7.]]
# ----------b-----------
# [[2. 0.]
#  [6. 9.]]
# ----------------------
# [[2. 0. 2. 0.]
#  [9. 7. 6. 9.]]
# ----------------------
# [[2. 0.]
#  [9. 7.]
#  [2. 0.]
#  [6. 9.]]

split data in matrix

#split data in matrix
a = np.floor(10*np.random.random((2,12)))
print(a)
print('------------')
print(np.hsplit(a,3))  # it will get 3 matrix splitting by row
print('------------')
print(np.hsplit(a,(3,4))) 
# split a after  the third and the fourth cloumn
# 在第三行和第四行后进行切割
print('------------')
a = np.floor(10*np.random.random((12,2)))
print(a)
print('-------------')
np.vsplit(a,3)  # splitting by column
# [[4. 3. 3. 3. 7. 5. 7. 4. 6. 4. 6. 8.]
#  [9. 9. 4. 8. 0. 4. 3. 5. 1. 9. 4. 4.]]
# ------------
# [array([[4., 3., 3., 3.],
#        [9., 9., 4., 8.]]), array([[7., 5., 7., 4.],
#        [0., 4., 3., 5.]]), array([[6., 4., 6., 8.],
#        [1., 9., 4., 4.]])]
# ------------
# [array([[4., 3., 3.],
#        [9., 9., 4.]]), array([[3.],
#        [8.]]), array([[7., 5., 7., 4., 6., 4., 6., 8.],
#        [0., 4., 3., 5., 1., 9., 4., 4.]])]
# ------------
# [[8. 2.]
#  [3. 9.]
#  [3. 5.]
#  [5. 0.]
#  [4. 3.]
#  [2. 3.]
#  [0. 2.]
#  [5. 7.]
#  [5. 5.]
#  [7. 9.]
#  [3. 8.]
#  [0. 0.]]
# -------------
# [array([[8., 2.],
#         [3., 9.],
#         [3., 5.],
#         [5., 0.]]), array([[4., 3.],
#         [2., 3.],
#         [0., 2.],
#         [5., 7.]]), array([[5., 5.],
#         [7., 9.],
#         [3., 8.],
#         [0., 0.]])]

data copy

# There are two way to get data copy
# shallow copy
c = a.view() # share the same value in shallow copy
print(c is a)
c.shape = (2,6)
print('a.shape: ' ,a.shape)
print('c.shape: ' ,c.shape)
c[0,4] = 1234    # the value of a will change after c changes,for it share the same value
print(a)
print(id(a))
print(id(c))
# False
# a.shape:  (3, 4)
# c.shape:  (2, 6)
# [[   0    1    2    3]
#  [1234    5    6    7]
#  [   8    9   10   11]]
# 2540538182992
# 2540538442256
#
#
#
# deep copy
d = a.copy()
print(d is a)
d[0,0] = 9999
print('------d-------')
print(d)
print('------a-------')
print(a)
# False
# ------d-------
# [[9999    1    2    3]
#  [1234    5    6    7]
#  [   8    9   10   11]]
# ------a-------
# [[   0    1    2    3]
#  [1234    5    6    7]
#  [   8    9   10   11]]

data sort

#data sort
import numpy as np
data = np.sin(np.arange(20).reshape(5,4))
print(data)
ind = data.argmax(axis = 0) # it will calculate by column
print(ind) # print the biggest one in every column which start from zero by default
data_max = data[ind,range(data.shape[1])] 
print(data_max)
# [[ 0.          0.84147098  0.90929743  0.14112001]
#  [-0.7568025  -0.95892427 -0.2794155   0.6569866 ]
#  [ 0.98935825  0.41211849 -0.54402111 -0.99999021]
#  [-0.53657292  0.42016704  0.99060736  0.65028784]
#  [-0.28790332 -0.96139749 -0.75098725  0.14987721]]
# [2 0 3 1]
# [0.98935825 0.84147098 0.99060736 0.6569866 ]

expand between column and row

# expand
import numpy as np
a = np.arange(0,40,10)
print(a)
b = np.tile(a,(3,5)) #construct one two dimensional array which has 3 rows and 5 columns and the data value is a by default.
print(b)
# [ 0 10 20 30]
# [[ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]
#  [ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]
#  [ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]]

sort

#sort
import numpy as np
a = np.array([[4,3,5],
             [1,6,1],
             [0,2,3]])
print(a)
print('------sorting by column-------')
b = np.sort(a,axis = 0) #get an sorted array,it will be sorted by column if the axis is zero and it will be sorted by row if the axis is one.
print(b)
#b
a.sort(axis = 1)
print('--------sorting by row-----') 
print(a)

print('################')
a = np.array([5,3,1,2])
j = np.argsort(a)   # get the index of least one in an array

print('-------the index of least one in an array------')
print(j)
print('-------the result ------')
print(a[j])   
# [[4 3 5]
#  [1 6 1]
#  [0 2 3]]
# ------sortting by row-------
# [[0 2 1]
#  [1 3 3]
#  [4 6 5]]
# --------sortting by column-----
# [[3 4 5]
#  [1 1 6]
#  [0 2 3]]
# ################
# -------the least one in an array------
# [2 3 1 0]
# -------the result ------
# [1 2 3 5]

More in this website !

All posts is made in Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0),If you repost please mark it is from -https://blog.emperinter.info/2020/04/27/numpy.html

Leave a Comment