[PYTHON] Why what? Deep Learning Scientific Calculation Library Numpy Edition

Introduction

From now on, I will explain the scientific calculation library used in machine learning over several articles. This time is Numpy edition. We will focus on the functions used in machine learning, not the whole.

Also, since I will write the code in Jupyter Notebook, Note that it behaves differently from the command prompt.

table of contents

Version confirmation

version


import numpy as np
np.__version__
#'1.16.4'

Numpy Basics

Numpy is a library that handles multidimensional array data. This is very useful when dealing with multiple arrays and matrices. If you understand the calculation of matrices, it will come in smoothly. (I was immature in the procession, so I was learning while looking at math textbooks. I sometimes calculate on paper.)

Import Numpy


import numpy as np

Numpy often translates to the module name ** np **. (I haven't seen any other module names.)

Array definition


a = np.array([1,2,3,4,5])
# []Don't forget

output


>>>a
array([1, 2, 3, 4, 5])
>>>print(a)
[1 2 3 4 5]
>>>type(a) #Type confirmation
numpy.ndarray

When multiple arrays


>>>b = np.array([[1,2,3,4,5],[6,7,8,9,10]])
>>>b
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10]])
>>>print(b)
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]

Display rows / columns


>>>a.shape
(5,)
>>>b.shape
(2,5)

np.Automatically create elements with arange


>>>np.arange(1,10,2)
array([1, 3, 5, 7, 9])
>>>np.arange(2,11,2)
array([ 2,  4,  6,  8, 10])

np.Convert the matrix with reshape


>>>d = np.arange(1,11,1)
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
>>>d.shape #Display the shape of the array with shape
(10,)
>>>d.reshape(2,5) #2 rows x 5 columns matrix
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10]])
>>>d.reshape(5,2) #5 rows x 2 columns matrix
array([[ 1,  2],
       [ 3,  4],
       [ 5,  6],
       [ 7,  8],
       [ 9, 10]])
>>>d.reshape(10,1) #10 rows x 1 column matrix
array([[ 1],
       [ 2],
       [ 3],
       [ 4],
       [ 5],
       [ 6],
       [ 7],
       [ 8],
       [ 9],
       [10]])

np.You can also do this with arange and reshape


>>>np.arange(1,10).reshape((3,3))
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

Arithmetic calculation

You can also calculate between arrays. However, make sure that the array you are calculating matches the number of rows and columns.

Arithmetic operations


>>>x = np.array([1.0,2.0,3.0])
>>>y = np.array([4.0,5.0,6.0])
>>>x+y
array([5., 7., 9.])
>>>x*y
array([ 4., 10., 18.])
>>>x-y
array([-3., -3., -3.])
>>>x/y
array([0.25, 0.4 , 0.5 ])

broadcast

You can operate directly on the internal data of the array. The inner product always comes out in machine learning, so let's learn how it works. For inner products A and B, match the number of columns in A with the number of rows in B.

Matrix multiplication(Inner product, dot product)


>>>​z = np.array([1,2,3])
>>>z.shape
(3,)
>>>v =np.array([[2],[3],[4]])
>>>v.shape
(3, 1)
>>>np.dot(z,v)
array([20])

inner product


>>>e = np.array([[1,2,3],[4,5,6]])
>>>e
array([[1, 2, 3],
       [4, 5, 6]])
>>>f =np.array([[3,3],[3,3],[3,3]])
>>>f
array([[3, 3],
       [3, 3],
       [3, 3]])
np.dot(e,f)
array([[18, 18],
       [45, 45]])

Finally

This time I wrote a simple part of Numpy. Next, I will write the mechanism of neural network calculation using matrices.

References

Recommended Posts

Why what? Deep Learning Scientific Calculation Library Numpy Edition
Deep Learning Model Lightening Library Distiller
Deep learning from scratch (cost calculation)
Microsoft's Deep Learning Library "CNTK" Tutorial
Deep Reinforcement Learning 3 Practical Edition: Breakout
Introduction to Deep Learning ~ Dropout Edition ~
(python) Deep Learning Library Chainer Basics Basics
Deep learning from scratch (forward propagation edition)
[Scientific / technical calculation by Python] Solving (generalized) eigenvalue problem using numpy / scipy, using library
Deep Learning
[Scientific / technical calculation by Python] Inverse matrix calculation, numpy
[Python / Machine Learning] Why Deep Learning # 1 Perceptron Neural Network