[PYTHON] I wrote the basic operation of Numpy in Jupyter Lab.

This article is an article that I actually coded the basic operation of Numpy described in Kame (@usdatascientist)'s blog (https://datawokagaku.com/python_for_ds_summary/) using Jupyter Lab.

** Summary of basic operations of NumPy **

Array

import numpy as np #Import numpy as np
np.array([1,2,3,4]) #list of array
array([1, 2, 3, 4])
python_list = (1,2,3,4) #Python list
python_list
(1, 2, 3, 4)
np.array([[1,2,3], [4,5,6],[7,8,9]]) #Nested list of array
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
[[1,2,3], [4,5,6],[7,8,9]] #Python nested list
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Array calculation

arr1 = np.array([[1,2,3], [4,5,6],[7,8,9]])
arr2 = np.array([[1,2,3], [4,5,6],[7,8,9]])
arr1
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
arr2
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
arr1 + arr2
array([[ 2,  4,  6],
       [ 8, 10, 12],
       [14, 16, 18]])
arr1 - arr2
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]])
arr1 * arr2
array([[ 1,  4,  9],
       [16, 25, 36],
       [49, 64, 81]])
arr1 / arr2
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])
#The missing elements of array[1,2,3]Is automatically complemented with.(Broadcasting)
arr3= np.array([[1,2,3]])
arr4 = np.array([[1,2,3], [4,5,6],[7,8,9]])
arr3 + arr4 
array([[ 2,  4,  6],
       [ 5,  7,  9],
       [ 8, 10, 12]])
arr3 - arr4
array([[ 0,  0,  0],
       [-3, -3, -3],
       [-6, -6, -6]])
arr = np.array([[1,2,3], [4,5,6],[7,8,9]])
#Check the size of the array matrix
arr.shape 
(3, 3)

Indexing

The index starts from 0 and can be fetched from 0 to 0th value.

ndarray = np.array([[1,2], [3,4],[5,6]])
ndarray
array([[1, 2],
       [3, 4],
       [5, 6]])
ndarray[1][0]
3
ndarray[1,0]
3

Slicing

When slicing is [N: M], the element of "N or more and less than M" is fetched.

arr = np.array([[1,2,3,4], [2,4,6,4], [3,5,7,4], [3,5,7,4]])
arr
array([[1, 2, 3, 4],
       [2, 4, 6, 4],
       [3, 5, 7, 4],
       [3, 5, 7, 4]])
arr[0:4, 2:4]
array([[3, 4],
       [6, 4],
       [7, 4],
       [7, 4]])
arr[:,2:] #Fetch all the elements after the second column of all rows.
array([[3, 4],
       [6, 4],
       [7, 4],
       [7, 4]])
arr = np.array([1,2,3,4,2,4,6,4,5,7,4,5,7,4])
arr[1:6:2] #Slicing in two
array([2, 4, 4])

Various Arrays

np.arange[(start,)stop(,step)]

np.arange(0,5,2) #[start, stop, step] #0~List of values where 5 is separated by 2
array([0, 2, 4])
np.arange(5) #start step can be omitted. If omitted, 0 and 1 are entered by default, respectively.
array([0, 1, 2, 3, 4])
np.arange(0,5,0.1) #step is 0.1 step is also possible
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1, 1.2,
       1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5,
       2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8,
       3.9, 4. , 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9])

np.linspace(start, stop, num=50)

np.linspace(1, 2, 20) #1~Divide 2 into 20 equal parts
array([1.        , 1.05263158, 1.10526316, 1.15789474, 1.21052632,
       1.26315789, 1.31578947, 1.36842105, 1.42105263, 1.47368421,
       1.52631579, 1.57894737, 1.63157895, 1.68421053, 1.73684211,
       1.78947368, 1.84210526, 1.89473684, 1.94736842, 2.        ])

.copy()

arr_copy = arr.copy() 
arr_copy
array([0, 1, 2, 3, 4])
ndarray = np.arange(0, 5)
ndarray_copy = ndarray.copy()
print('original array is {}'.format(id(arr)))
print('copied array is {}'.format(id(arr))) #The copied object is a different object from the copy source
original array is 140571396284208
copied array is 140571396284208
ndarray[:] = 100
print('original array:\n', ndarray)    #If you don't copy, the original array will be updated
print('copied array:\n', ndarray_copy) #Copy does not update the original array
original array:
 [100 100 100 100 100]
copied array:
 [0 1 2 3 4]
def add_world(hello):
    hello += ' world'
    return hello

h_str = 'hello'
h_list = ['h', 'e', 'l', 'l', 'o']
output_str = add_world(h_str)
output_list = add_world(h_list)

print('output_str: {}'.format(output_str)) #String is passed by value, so change it directly
print('output_list: {}'.format(output_list)) #list is passed by reference, so don't change it directly

print('h_str: {}'.format(h_str))
print('h_list: {}'.format(h_list))
output_str: hello world
output_list: ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
h_str: hello
h_list: ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
def change_hundred(array):
    
    array[0] = 100 #If you don't copy, the original array will be updated
    return array

def change_hundred_copy(array):
    
    array_copy = array.copy() #Copy does not update the original array
    array_copy[0] = 100
    return array_copy

array_1 = np.arange(0, 4)
array_2 = np.arange(0,4)

output_array = change_hundred(array_1)
output_array_copy = change_hundred_copy(array_2)

print('original_array_1/n', array_1)
print('original_array2/n', array_2)
original_array_1/n [100   1   2   3]
original_array2/n [0 1 2 3]

np.zeros(shape)

shape = (2,3,5) #Zero matrix
zeros = np.zeros(shape)
print(zeros)
[[[0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]]

 [[0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]]]

np.ones(shape)

shape = (2,3,5)
ones_array_1 = np.ones(shape)
ones_array_2 = np.ones(4)

print(ones_array_1)
print(ones_array_2)
[[[1. 1. 1. 1. 1.]
  [1. 1. 1. 1. 1.]
  [1. 1. 1. 1. 1.]]

 [[1. 1. 1. 1. 1.]
  [1. 1. 1. 1. 1.]
  [1. 1. 1. 1. 1.]]]
[1. 1. 1. 1.]

np.eye(N)

np.eye(3) #N *The identity matrix of N. The diagonal components are all 1.
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

np.random.rand()

random_float = np.random.rand() # 0~Returns a random number from 1.
random_1d = np.random.rand(3)
random_2d = np.random.rand(3, 4)

print('random_float:{}'.format(random_float))
print('random_1d:{}'.format(random_1d))
print('random_2d:{}'.format(random_2d))
random_float:0.48703321814513356/n
random_1d:[0.27824053 0.60682459 0.22001138]
random_2d:[[0.52285782 0.87272074 0.03123286 0.7921472 ]
 [0.80209903 0.26478273 0.91139303 0.63077037]
 [0.72151924 0.91234378 0.69355857 0.9969298 ]]

np.random.randn()

random_float = np.random.randn() #Values are returned from the standard normal distribution 0-1.
random_1d = np.random.randn(3)
random_2d = np.random.randn(3, 4)

print('random_float:{}'.format(random_float))
print('random_1d:{}'.format(random_1d))
print('random_2d:{}'.format(random_2d))
random_float:-0.013248078417389888
random_1d:[-1.11606544 -0.24693187  0.17212059]
random_2d:[[-0.85896581  0.53993487  0.09458454  1.3505381 ]
 [-1.83510873  0.09966918 -0.22293729  0.58218476]
 [ 0.37403933  0.83349568  0.73407002 -0.32979339]]

np.random.randint(low[,high][,size])

np.random.randint(10, 50, size=(2,4,3)) #Returns an ndarray of the specified size at random from integers greater than or equal to low and less than high. This time it is 2D 4 rows 3 columns.
array([[[30, 14, 11],
        [33, 24, 48],
        [15, 43, 41],
        [18, 26, 49]],

       [[18, 22, 16],
        [15, 10, 43],
        [43, 48, 10],
        [42, 29, 10]]])

.reshape(shape)

array = np.arange(0, 10)
print('array:{}'.format(array))
new_shape = (2, 5)
reshaped_array = array.reshape(new_shape)

print('reshaped array:{}'.format(reshaped_array))
print("reshaped array's is {}".format(reshaped_array.reshape))
print('original array is NOT changed:{}'.format(array))
array:[0 1 2 3 4 5 6 7 8 9]
reshaped array:[[0 1 2 3 4]
 [5 6 7 8 9]]
reshaped array's is <built-in method reshape of numpy.ndarray object at 0x7fd9420d8030>
original array is NOT changed:[0 1 2 3 4 5 6 7 8 9]

Find the statistic of the element

normal_dist_mat = np.random.randn(5,5)
print(normal_dist_mat) 
[[-1.51861663 -2.09873943 -0.99761607  0.95395101 -0.04577882]
 [-0.81944941  0.54339984  0.45265577 -2.56369775 -1.8300719 ]
 [ 0.63372482 -0.35763135  0.31683655  1.44185989 -1.2110421 ]
 [-1.56200024  1.17061544  1.35721624 -0.46023814  0.3496441 ]
 [ 1.09102475 -0.47551934 -0.75747612  0.34564251  1.62400795]]

.max () maximum value

print('max is')
print(normal_dist_mat.max()) 
max is
1.6240079549859363

.argmax () Find the index of the maximum value.

print('argmax is')
print(normal_dist_mat.argmax()) 
argmax is
24

.flatten () [] Put the values stored in index = 9 in a row.

normal_dist_mat.flatten()[9] 
-1.83007190063398

.min () minimum value

print('min is')
print(normal_dist_mat.min()) 
min is
-2.5636977453276137

.argmin () Find the minimum index.

print('argmin is')
print(normal_dist_mat.argmin()) 
argmax is
8

.mean () average value

print('mean is') 
print(normal_dist_mat.mean())
mean is
-0.1766919366579146

np.median (ndarray) median

print('median is')
print(np.median(normal_dist_mat)) 
median is
-0.04577882007895756

.std () standard deviation

print('standard deviation is')
print(normal_dist_mat.std())
standard deviation is
1.1634432893009636
print(normal_dist_mat.std())  #All the above functions are np.Function name(ndarray)Can also be called by
1.1634432893009636
print(normal_dist_mat) #If you want to find the statistics for a specific row or column, specify the axis argument.
print('axis=0 > {}'.format(normal_dist_mat.max(axis=0))) #axis=If 0 is specified, the statistic for each column,
print('axis=1 > {}'.format(normal_dist_mat.max(axis=1))) #axis=If 1 is specified, the statistic of each row is returned.
[[-1.51861663 -2.09873943 -0.99761607  0.95395101 -0.04577882]
 [-0.81944941  0.54339984  0.45265577 -2.56369775 -1.8300719 ]
 [ 0.63372482 -0.35763135  0.31683655  1.44185989 -1.2110421 ]
 [-1.56200024  1.17061544  1.35721624 -0.46023814  0.3496441 ]
 [ 1.09102475 -0.47551934 -0.75747612  0.34564251  1.62400795]]
axis=0 > [1.09102475 1.17061544 1.35721624 1.44185989 1.62400795]
axis=1 > [0.95395101 0.54339984 1.44185989 1.35721624 1.62400795]

Functions used in mathematics

np.exp(ndarray)

ndarray = np.linspace(-3,3,10)
expndarray = np.exp(ndarray)
print(ndarray)
print(expndarray)
[-3.         -2.33333333 -1.66666667 -1.         -0.33333333  0.33333333
  1.          1.66666667  2.33333333  3.        ]
[ 0.04978707  0.09697197  0.1888756   0.36787944  0.71653131  1.39561243
  2.71828183  5.29449005 10.3122585  20.08553692]

np.log(nd.array)

ndarray = np.linspace(-3,3,10)
logndarray = np.log(ndarray)
print(ndarray)
print(logndarray) #Negative values cannot be logarithmic, so nan.
[-3.         -2.33333333 -1.66666667 -1.         -0.33333333  0.33333333
  1.          1.66666667  2.33333333  3.        ]
[        nan         nan         nan         nan         nan -1.09861229
  0.          0.51082562  0.84729786  1.09861229]


/opt/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in log
print(logndarray[0])
print('nan ==None? {}'.format(logndarray[0] is None))
nan
nan ==None? False
np.isnan(logndarray[0])
True

np.e

print(np.e)
print(np.log(np.e))
2.718281828459045
1.0

ndarray shape operation

ndarray = np.array([[1,2,3], [4,5,6], [7,8,9]])
print('array is :{}'.format(ndarray))
print("ndarray's shape is :{}".format(ndarray.shape)) #The number of dimensions is called rank.
array is :[[1 2 3]
 [4 5 6]
 [7 8 9]]
ndarray's shape is :(3, 3)

np.expand_dims(nd.array, axis)

expanded_ndarray = np.expand_dims(ndarray, axis=0) #Add dimension
expanded_ndarray.shape
(1, 3, 3)

np.squeeze(ndarray)

squeezed_ndarray = np.squeeze(expanded_ndarray) #Eliminate one dimension of shape.
squeezed_ndarray.shape
(3, 3)

.flatten()

flatten_array = ndarray.flatten() #Make one row
print('flatten_array is :{}'.format(flatten_array))
print('ndarray is :{}'.format(ndarray))
flatten_array is :[1 2 3 4 5 6 7 8 9]
ndarray is :[[1 2 3]
 [4 5 6]
 [7 8 9]]

Save and load Numpy Array

np.save ('file path', ndarray)

ndarray = np.array([
    [1,2,3,4],
    [10,20,30,40],
    [100,200,300,400]
])
np.save('saved_numpy', ndarray)

np.load ('file path')

loaded_numpy = np.load('saved_numpy.npy')
loaded_numpy
array([[  1,   2,   3,   4],
       [ 10,  20,  30,  40],
       [100, 200, 300, 400]])

Recommended Posts

I wrote the basic operation of Numpy in Jupyter Lab.
I wrote the basic operation of Seaborn in Jupyter Lab
I wrote the basic operation of matplotlib with Jupyter Lab
I wrote the basic operation of Pandas with Jupyter Lab (Part 1)
I wrote the basic operation of Pandas with Jupyter Lab (Part 2)
I wrote the basic grammar of Python with Jupyter Lab
I wrote the queue in Python
I wrote the stack in Python
I wrote the code to write the code of Brainf * ck in python
Build the execution environment of Jupyter Lab
I wrote the selection sort in C
I wrote the sliding wing in creation.
I checked the list of shortcut keys of Jupyter
I checked the processing speed of numpy one-dimensionalization
I wrote the hexagonal architecture in go language
[Fundamental Information Technology Engineer Examination] I wrote the algorithm of Euclidean algorithm in Python.
Basic operation of pandas
Basic operation of Pandas
[Tips] Save / copy the graph displayed in Jupyter Lab
[Note] I can't call the installed module in jupyter
I wrote GP with numpy
I wrote python in Japanese
I tried the accuracy of three Stirling's approximations in python
About the garbled Japanese part of pandas-profiling in Jupyter notebook
I want to get the operation information of yahoo route
[Scientific / technical calculation by Python] Basic operation of arrays, numpy
I want to judge the authenticity of the elements of numpy array
I wrote it in Go to understand the SOLID principle
Change the theme of Jupyter
I want to align the significant figures in the Numpy array
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I wrote a script that splits the image in two
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
I compared the calculation time of the moving average written in Python
[Python] I wrote the route of the typhoon on the map using folium
I want to know the population of each country in the world.
I got lost in the maze
The story of participating in AtCoder
I investigated the mechanism of flask-login!
I participated in the ISUCON10 qualifying!
I wrote Fizz Buzz in Python
About the basic type of Go
I wrote Gray Scale in Pytorch
The meaning of ".object" in Django
[Fundamental Information Technology Engineer Examination] I wrote an algorithm for the maximum value of an array in Python.
[I touched the Raspberry Pi (1)] I summarized the basic operations of Minecraft Pi Edition (2015.5.23 pre-release)
Comparing the basic grammar of Python and Go in an easy-to-understand manner
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to sort a list in the order of other lists
Memo of pixel position operation for image data in Python (numpy, cv2)
I wrote a corpus reader that reads the results of MeCab analysis
[Python] I forcibly wrote a short Perlin noise generation function in Numpy.
I want to leave an arbitrary command in the command history of Shell
For the first time in Numpy, I will update it from time to time
I made a program to check the size of a file in Python
I made a mistake in fetching the hierarchy with MultiIndex of pandas
I tried to display the altitude value of DTM in a graph
I tried touching touch related methods in the scene module of pythonista
What I investigated in the process of expressing (schematicizing) containers in a nested frame with Jupyter and making it
Part 1 I wrote an example of the answer to the reference problem of how to write offline in real time in Python