Private notes.
Jupyter
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
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
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.
jupyter nbconvert hoge.ipynb --to pdf
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
numpy
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).
There are argmax and argmin.
np.argmax(arr)
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])
np.linspace(2.0, 3.0, num=5)
# array([2. , 2.25, 2.5 , 2.75, 3. ])
np.logspace(3, 7, num=5, base=2)
# array([8, 16, 32, 64, 128])
Note that the argument is a tuple
np.zeros((3, 4))
There is also a np.ones that initializes all elements with 1.
Both are np.dot
np.dot(A, B)
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
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])
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].
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])
test = np.array([1, 2, 3, 4, 5])
np.delete(test, [1, 3])
# array([1, 3, 5])
** Cannot be deleted as a slice! !! ** **
np.identity(5)
np.eye(5)
Either one is fine.
np.linalg.inv(A)
A.T
np.linalg.det(A)
| 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).
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)
np.random.uniform(size = 1000000)
np.random.uniform(low=1.0, high=2.0, size=20)
np.random.chisquare(2, 10000)
a = np.array([[3,1], [1,2]])
b = np.array([9,8])
np.linalg.solve(a, b)
# array([2., 3.])
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]])
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
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()
plt.yscale('log')
The y-axis becomes log scale.
plt.hist(x, bins=100)
bins is the number of bins (number of classes)
plt.scatter(x, y, marker='x', c='red')
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.
names = ['Alice', 'Bob', 'Charlie']
ages = [24, 50, 18]
for name, age in zip(names, ages):
print(name, age)
# Alice 24
# Bob 50
# Charlie 18
l = ['Alice', 'Bob', 'Charlie']
for i, name in enumerate(l):
print(i, name)
# 0 Alice
# 1 Bob
# 2 Charlie
print("i={} lambda={} x={}".format(i, l, x))
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