Jupyter, numpy, matplotlib notes used in reports

Private notes.

Jupyter

Environment

jupyter_contrib_nbextensions is included for pdf output, which will be described later, and for completion in Jupyter, and is not essential.

pip install jupyter jupyter_contrib_nbextensions
pip install matplotlib numpy #Add other necessary items

Launch Jupyter in your browser

I thought it would be good in VSCode instead of the browser, but I didn't switch after all.

jupyter notebook --ip=127.0.0.1 --allow-root

Change Jupyter theme

You can put in the jupyterthemes module, or you can tweak the ~ / .jupyter / custom / custom.css yourself.

In the former case -Change the background color and font of Jupyter Notebook coolly It will be helpful.

I'm using https://gist.github.com/7ma7X/54f5e0b60e39ae8826bfcc580d524e40 as custom.css.

Output Jupyter as pdf

jupyter nbconvert hoge.ipynb --to pdf

Magic

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

numpy

Correlation coefficient

x = np.array([
  [1, 2, 1, 9, 10, 3, 2, 6, 7],
  [2, 1, 8, 3, 7, 5, 10, 7, 2]]) 

np.corrcoef(x)

Since the return value is a correlation coefficient matrix, check the (1, 2) element (or (2, 1) element).

Index that takes the maximum (small) value

There are argmax and argmin.

np.argmax(arr)

Arithmetic progression

It's okay to make it in list comprehension, but there is np.arange.

np.arange(5)
# array([0, 1, 2, 3, 4])

np.arange(3, 10, 2)
# array([3, 5, 7, 9])

A sequence of specified sections divided into N equal parts

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

Geometric progression

np.logspace(3, 7, num=5, base=2)
# array([8, 16, 32, 64, 128])

Matrix initialization

Note that the argument is a tuple

np.zeros((3, 4))

There is also a np.ones that initializes all elements with 1.

Inner product of vectors, product of matrices

Both are np.dot

np.dot(A, B)

Vector / matrix concatenation

It's confusing, so you should look at https://note.nkmk.me/python-numpy-concatenate-stack-block/. There is a theory that it is better to use numpy.stack.

v1 = np.array([1, 2, 3])
v2 = np.array([4, 5, 6])

np.concatenate([v1, v2])
# [1 2 3 4 5 6]

Note that the entire argument must be a list

Crush a multidimensional array into a one-dimensional array

np.ravel is faster than the built-in flatten.

x = np.array([[1, 2, 3], [4, 5, 6]])
np.ravel(x)
# array([1, 2, 3, 4, 5, 6])

Vector length (norm)

2 norms are usually used

np.linalg.norm(X)
np.linalg.norm(X,ord=1) #1 norm

By the way, np.linalg.norm also corresponds to the matrix norm [^ 2].

Extraction of matrix columns

Write like mat [:, 2]

mat =\
[[ 0  1  2  3  4  5  6  7  8  9]
 [10 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 36 37 38 39]
 [40 41 42 43 44 45 46 47 48 49]
 [50 51 52 53 54 55 56 57 58 59]
 [60 61 62 63 64 65 66 67 68 69]
 [70 71 72 73 74 75 76 77 78 79]
 [80 81 82 83 84 85 86 87 88 89]
 [90 91 92 93 94 95 96 97 98 99]]

mat[:,2]
# array([ 2, 12, 22, 32, 42, 52, 62, 72, 82, 92])

Remove certain elements from the array

test = np.array([1, 2, 3, 4, 5])
np.delete(test, [1, 3])
# array([1, 3, 5])

** Cannot be deleted as a slice! !! ** **

Identity matrix

np.identity(5)
np.eye(5)

Either one is fine.

Inverse matrix

np.linalg.inv(A)

Transpose matrix

A.T

Determinant

np.linalg.det(A)

Eigenvalue / eigenvector

function Description
np.linalg.eig Eigenvalues and eigenvectors of general matrices
np.linalg.eigh Eigenvalues and eigenvectors of Hermitian or real symmetric matrix
np.linalg.eigvals Eigenvalues of the general matrix
Do not calculate eigenvectors
np.linalg.eigvalsh Eigenvalues of Hermitian or real symmetric matrix
Do not calculate eigenvectors

Since the algorithm for finding eigenvalues is different for symmetric matrices than for general matrices, ** use eigh and eigvalsh for symmetric matrices ** for accuracy.

In the method that finds both the eigenvalue and the eigenvector, the return value is returned as a tuple of (eigenvalue, eigenvector).

random number

Random numbers that follow a normal distribution

np.random.normal(size = 1000000)
np.random.normal(loc=1.0, scale=2.0, size=20) 

For multivariate normal distribution, use multivariate_normal

mean = (1, 2)
cov = [[1, 0], [0, 1]]
np.random.multivariate_normal(mean, cov, 100)

Random numbers that follow a uniform distribution

np.random.uniform(size = 1000000)
np.random.uniform(low=1.0, high=2.0, size=20) 

Random numbers that follow a chi-square distribution

np.random.chisquare(2, 10000)

Other miscellaneous material

Solve simultaneous linear equations

a = np.array([[3,1], [1,2]])
b = np.array([9,8])
np.linalg.solve(a, b)
# array([2.,  3.])

Matrix transformation

For example, transforming a matrix of size (1, 12) to size (3, 4).

a = array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
np.reshape(a, (3, 4))
# array([[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [ 8,  9, 10, 11]])

Condition number

In numerical analysis, a value that is an index of susceptibility to error (it can be said that a large value is vulnerable to error)

np.linalg.cond(A, 2)

matplotlib

Basic graph

plt.figure(figsize=(8, 6)) #8 horizontal x 6 vertical graph
plt.title("This is title.") #Give the graph a title

#Name the axis
plt.xlabel("Number of trials")

#Change the range of the axis
plt.ylim(0, 2.5)

# label=In the legend, color=With color, linestyle=Change the line style with
plt.plot(X, Y, label="one") 
plt.plot(X2, Y2, label="two")

plt.legend()
plt.show()

Logarithmic graph

plt.yscale('log')

The y-axis becomes log scale.

histogram

plt.hist(x, bins=100)

bins is the number of bins (number of classes)

Scatter plot

plt.scatter(x, y, marker='x', c='red')

contour

There are times when I want to add contour lines as a supplement, such as when drawing the state of optimization.

x = np.linspace(-1, 1, 100)
y = np.linspace(-1, 1, 100)
 
X, Y = np.meshgrid(x, y)
Z = np.sqrt(X**2 + Y**2)

plt.contour(X, Y, Z)
plt.gca().set_aspect('equal')

Contour lines are quite difficult, so you should read http://ailaby.com/contour/ carefully.

Other Python coding techniques

Loop (zip) multiple lists together

names = ['Alice', 'Bob', 'Charlie']
ages = [24, 50, 18]

for name, age in zip(names, ages):
    print(name, age)
# Alice 24
# Bob 50
# Charlie 18

Indexed loop (enumerate)

l = ['Alice', 'Bob', 'Charlie']

for i, name in enumerate(l):
    print(i, name)
# 0 Alice
# 1 Bob
# 2 Charlie

format statement

print("i={} lambda={} x={}".format(i, l, x))

Mathematical function

Common functions are usually found in the math module, even if they are not from numpy. Check https://docs.python.org/ja/3/library/math.html for details

from math import sin, cos, pi, exp

Recommended Posts

Jupyter, numpy, matplotlib notes used in reports
Settings often used in Jupyter
Summary of frequently used commands in matplotlib
ipython + jupyter + plotly (matplotlib) settings & usage notes
where in numpy
Jupyter study notes_006
Bash in Jupyter
Jupyter study notes_008
Jupyter study notes_004
Jupyter study notes_001
Put matplotlib in Centos7.
Jupyter Official DockerHub Notes
Axis specification in NumPy
View images in Matplotlib
2D plot in matplotlib
matplotlib legend layout notes
Jupyter in Cloud9 IDE
[Numpy / pandas / matplotlib Exercise 01]
I wrote the basic operation of Numpy in Jupyter Lab.
Write charts in real time with Matplotlib on Jupyter notebook
Notes on coloring by value in the matplotlib scatter plot