Numpy - Functions
|
Numpy importation |
|
Returns sine of 0 (0) |
|
Returns cosine of 0 (1) |
|
Returns hiperbolic sine of 0 (0) |
|
Returns hiperbolic cosine of 0 (1) |
|
Returns the value of pi (3.1415...) |
|
Returns a 1-dimensional array |
np.array([[1, 2],[3, 4]])
|
Returns a 2-dimensional array |
|
Returns a matrix |
|
Returns a 1-dimensional array from 0 to 1 with 11 elements |
|
Returns a 1-dimensional array from 101 to 103 with 11 elements |
|
Returns a 1-dimensional array from 0 to 1 with step 0.1 |
|
Returns a random 1-dimensional array with 2 elements |
np.random.random((2, 2))
|
Returns a random 2-dimensional array with 2 rows and 2 columns |
np.random.random((2, 2, 2))
|
Returns a random 3-dimensional array with 2 elements in each dimension |
|
Returns a 5x5 identity matrix |
|
Returns a null vector with 5 elements |
|
Returns a null matrix with 5 rows and 2 columns |
|
Returns a 5 elements vector filled with 1 |
|
Returns a 5x2 matrix filled with 1 |
1Numpy has all functions and constants in the library Math.
2Numpy arrays can be used to do all sorts of linear algebra calculations since they are treated as mathematical tensors (vectors and matrices) rather than Python lists.
3Numpy matrices are a special type of array that is easier to use in common linear algebra problems. Matrices are always 2D
Numpy - Linalg
|
Returns the dot product between arrays a and b |
|
Returns the matrix product between arrays a and b |
|
Returns the eigenvalues of the square array a |
|
Returns the eigenvalues and eigenvectors of the square array a |
|
Returns the a array norm |
|
Returns the determinant of a |
|
Returns the inverse of a |
|
Returns the pseudo-inverse of a |
|
Returns the solution of aX = b |
Numpy - Slicing
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
a[1:, 1:] = [[50, 60], [80, 90]]
print(a)
|
[[ 1 2 3]
[ 4 50 60]
[ 7 80 90]]
Matplotlib - Example
from matplotlib import pyplot as plt
import numpy as np
x = np.linspace(0, 10, 200)
y1 = 3*np.sin(x) + np.cos(x)
y2 = 2*np.cos(x) - np.sin(x)
plt.plot(x, y1, '-b', label="y1")
plt.plot(x, y2, '-r', label="y2")
plt.xlabel("x")
plt.ylabel("y")
plt.title("Y Vs. X")
plt.legend()
plt.grid()
plt.show()
|
Matplotlib - Example (Output)
Matplotlib - Pyplot Functions
from matplotlib import pyplot as plt
|
Pyplot importation |
|
Plots x Vs. y on current figure |
|
Sets x-axis label to x |
|
Sets y-axis label to y |
|
Sets figure title to Y Vs. X |
|
Shows legends in figure |
|
Shows grid in figure |
|
Shows figures |
|
Starts a new figure |
1 Many args can be set on plt.plot(). Some are shown on the next block.
Pyplot - Lines, Markers and Colors
'-' |
solid line style |
'--' |
dashed line style |
'-.' |
dash-dot line style |
':' |
dotted line style |
'.' |
point marker |
',' |
pixel marker |
'o' |
circle marker |
'v' |
triangle_down marker |
'^' |
triangle_up marker |
'<' |
triangle_left marker |
'>' |
triangle_right marker |
's' |
square marker |
'p' |
pentagon marker |
'*' |
star marker |
'h' |
hexagon1 marker |
'H' |
hexagon2 marker |
'+' |
plus marker |
'x' |
x marker |
'D' |
diamond marker |
'd' |
thin_diamond marker |
'|' |
vline marker |
'_' |
hline marker |
'b' |
blue |
'g' |
green |
'r' |
red |
'c' |
cyan |
'm' |
magenta |
'y' |
yellow |
'k' |
black |
'w' |
white |
You can use a line style, a marker and a color directly at plt.plot() like this:
plt.plot(x, y, '-+b')
|
|
|