針對 array 處理相關操作
Linear Algebra
Image processing
底下接 c / fortran
https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html
import numpy as np
arr = np.array([
[1, 2],
[3.14, 4]
])
arr
array([[ 1. , 2. ], [ 3.14, 4. ]])
arr.dtype
dtype('float64')
np.eye(4)
array([[ 1., 0., 0., 0.], [ 0., 1., 0., 0.], [ 0., 0., 1., 0.], [ 0., 0., 0., 1.]])
np.diag([1, 2, 3, 4])
array([[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]])
np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr = np.arange(20).reshape(4, 5)
arr
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]])
arr[2, 1]
11
arr[2, :]
array([10, 11, 12, 13, 14])
arr[
2,
[0, 3]
]
array([10, 13])
arr = np.array([0, -100, 42, 9])
arr > 0
array([False, False, True, True], dtype=bool)
arr[
arr > 0
]
array([42, 9])
arr = np.arange(15).reshape(3, 5)
arr
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]])
arr[
[1, 2],
[2, 3]
]
array([ 7, 13])
a = np.array([5, 42, 100])
b = a[:]
b
array([ 5, 42, 100])
b[2] = -1
b
array([ 5, 42, -1])
a
array([ 5, 42, -1])
a
array([ 5, 42, -1])
a > 0
array([ True, True, False], dtype=bool)
arr = np.arange(40).reshape(8, 5)
arr
array([[ 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]])
arr.sum()
780
arr.sum(axis=0)
array([140, 148, 156, 164, 172])
arr.sum(axis=1)
array([ 10, 35, 60, 85, 110, 135, 160, 185])
arr
array([[ 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]])
arr + np.array([100, 100, 100, 100, 100])
array([[100, 101, 102, 103, 104], [105, 106, 107, 108, 109], [110, 111, 112, 113, 114], [115, 116, 117, 118, 119], [120, 121, 122, 123, 124], [125, 126, 127, 128, 129], [130, 131, 132, 133, 134], [135, 136, 137, 138, 139]])
%matplotlib notebook
import matplotlib.pyplot as plt
arr = np.random.randn(90)
arr
array([ 6.96515346e-01, 1.04056344e+00, -2.96178541e-01, -1.77355976e+00, -1.14731385e+00, -7.46386654e-01, -1.33980669e+00, -6.99235911e-01, 1.37541970e+00, -1.25327558e+00, -1.06003389e+00, -1.00105016e-01, -5.89305559e-02, 7.91050340e-01, 6.52813592e-01, -9.85387242e-01, -1.15018718e+00, 1.33664097e+00, 1.37403280e-01, -8.29949292e-01, 9.29210780e-01, -1.07313016e+00, 2.24074083e-01, 5.11529823e-01, 2.31299364e-01, -2.09251017e+00, -9.64506545e-01, -7.25388492e-01, 2.28787310e-01, -6.56531683e-01, 1.23332110e+00, 1.08502556e+00, -6.34150669e-01, -3.22031403e-01, -5.22633549e-01, -1.04097756e-01, -6.88593166e-01, -8.79474345e-01, 5.05322090e-01, 1.96629696e+00, 7.24589606e-01, 6.24290125e-01, 1.72282440e-01, -5.12953511e-01, 9.46188077e-02, -5.53293662e-01, -1.64726685e+00, -2.88423616e+00, 2.73958769e-04, -1.95369900e+00, 9.73996415e-01, -5.02292087e-01, 2.13110245e-01, 3.67698000e-01, -4.63988566e-01, 6.95294684e-02, 9.86541954e-01, -1.98491777e+00, -8.46283008e-01, 9.33646028e-01, -3.96486894e-01, -9.96457644e-01, 7.12931355e-01, 1.06370473e+00, -2.44471140e+00, -3.98766171e-01, 5.84776259e-01, 4.10124957e-01, -6.32835861e-01, 2.36304871e-01, -1.52348979e-02, 1.39436163e+00, 1.82724235e+00, -1.80621586e+00, 1.22366313e+00, 1.06431744e+00, 9.34524047e-01, -5.66467019e-01, -2.68894417e-01, 1.71493002e+00, -2.59725697e-01, 1.73614939e-01, -2.22550229e+00, 2.44520275e+00, 1.37707515e+00, 1.98160265e-01, 1.38401746e+00, -2.32806532e-01, 2.51239440e-01, -2.40498274e-01])
plt.plot(arr, np.random.randn(90) + 20, 'o')
[<matplotlib.lines.Line2D at 0x10d390588>]
from mpl_toolkits.mplot3d import axes3d
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
x, y = np.mgrid[0:10:0.5, 0:10:0.5]
z = np.sin(x) - y
ax.plot_wireframe(x, y, z)
<mpl_toolkits.mplot3d.art3d.Line3DCollection at 0x10dbd1390>
import pandas as pd
se = pd.Series([1, 2, 5, 1,2], index=['foo', 'bar', 'a', 'c', 'd'])
df = pd.DataFrame({
'age': [18, 19, 20],
'no': [56, 29, 32]
}, index=['foo', 'Bob', 'Alice'])
df
age | no | |
---|---|---|
foo | 18 | 56 |
Bob | 19 | 29 |
Alice | 20 | 32 |
df['age']['Bob']
19
data = np.random.randn(100, 2)
from sklearn.cluster import KMeans
km = KMeans()
km.fit(data)
KMeans(copy_x=True, init='k-means++', max_iter=300, n_clusters=8, n_init=10, n_jobs=1, precompute_distances='auto', random_state=None, tol=0.0001, verbose=0)
km.cluster_centers_
array([[ 0.52949303, -0.2306427 ], [-0.73272274, 0.50350894], [-0.506402 , -0.6639773 ], [-0.24936256, 1.70200489], [ 0.5943467 , 0.83977173], [-1.49385411, -0.69112534], [ 1.89524255, 0.05002626], [ 0.3024382 , -1.54085445]])