%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
Numpy基础是数组对象ndarray
np.dtype
字符编码
np.dtype('i'), np.dtype(float)
t = np.dtype('Float64')
t.char, t.type
a = np.array([1,2,3,4,5,6,7,8,9,10,11,32,12])
a.dtype
b = np.arange(10, dtype=np.int8)
b
val = np.array([5, 12, 2, 32, 19, 71, 48])
val[1:5:2]
var[lower:upper:step, lower:upper:step]
# fancy indexing
a = np.arange(1,16).reshape(3,5)
a
a[[1,2]]
a[[1,2], [3, 4]]
a[a>5]
# 生成函数
x = np.arange(-10,10,2)
x
x = np.linspace(-10, 10, 20)
x
y = np.logspace(0, 10, 20, base=np.e)
y
# similar to meshgrid in MATLAB
x, y = np.mgrid[0:5, 0:5]
x
# uniform random
np.random.rand(5)
# 正态分布
np.random.randn(5,5)
a.shape = (3,4)
a
a.reshape(5,3)
# 展平
b = a.ravel()
a
b
b = a.flatten()
a
b
ravel
返回的是视图,而flatten
返回的是重新分配内存的新结果
a
a.transpose()
a
a.T
a.resize((5,3))
a
与reshape
不同,它就地更新结构
a.ndim
a.size
a.itemsize
a.nbytes
a.dtype
a.T
a.tolist()
a.dot(a.T)
concatenate((a0,a1,...,aN),axis=0)
x = np.array([[0, 1, 2],[10, 11, 12]])
y = np.array([[50, 51, 52],[60, 61, 62]])
x,y
y
np.concatenate((x, y))
np.concatenate((x, y), 1)
np.array((x,y))
np.vstack((x,y))
np.hstack((x,y))
np.dstack((x,y))
A = np.mat('1 2 3; 4 5 6; 7 8 9')
A
A = np.matrix('1 2 3; 4 5 6; 7 8 9')
A
A = np.mat(np.arange(1,10).reshape(3,3))
A
# 转置矩阵
A.T
# 逆矩阵
A.I
# Hermitian
C = np.matrix([[1j, 2j], [3j, 4j]])
C
C.H
# 转化成一维
A.A1
np.linalg.det(A)
# 单位矩阵
A = np.eye(3)
A
B = A*10
B
np.bmat("A B; B A")
# 零矩阵
A = np.zeros((3,3))
A
A = np.mat(np.arange(1,10).reshape(3,3))
B = np.zeros_like(A)
B
# 对角阵
np.diag([1,2,3,4,5])
np.diag([1,2,3,4,5], k=1)
A = np.matrix('1 2 3; 4 5 6; 7 8 9')
A * 2
A + 2
A * A
# 求模
np.mod(A, 2)
A % 2
$A \times B$
$A / B $
$A^2 \times B$
A = np.array([1.0,6.0,2.0,5.0,8.0,9.0])
B = np.array([6.0,2.0,4.0,7.0,9.0,2.0])
A*B, A/B, A**2*B
a = np.array([-3,-2,-1,0,1,2,3])
def Theta(x):
if x >= 0:
return 1
else:
return 0
Theta(a)
Theta_V = np.vectorize(Theta)
Theta_V(a)
$3x^2+2x-1$
p = np.poly1d([3, 2, -1])
p(1)
p.roots
p.order
x = np.linspace(0, 1, 20)
y = np.sin(x) + 0.3*np.random.rand(20)
p = np.poly1d(np.polyfit(x, y, 3))
t = np.linspace(0,1,200)
plt.plot(x, y, 'o', t, p(t), '-')
data = []
with open('data/mat.txt') as file:
for line in file:
fields = line.split()
row_data = [float(x) for x in fields]
data.append(row_data)
data = np.array(data)
data
data = np.loadtxt("data/mat.txt")
data
M = np.random.rand(10, 4)
np.savetxt("data/mat2.txt", M)
M
np.savetxt("data/mat.csv", M, fmt="%.3f")
!cat data/mat.csv
dr1 = np.loadtxt('data/sample.txt')
dr1
help(np.loadtxt)
!head data/sample.txt
dr1 = np.loadtxt('data/sample.txt', delimiter="|", skiprows=1)
dr1
!cat data/sample2.txt
dtype = np.dtype([('obsid', 'S6'), ('designation', 'S19'), ('obsdate', 'S10'), ('lmjd', int)])
dr1 = np.loadtxt('data/sample2.txt', dtype=dtype, delimiter="|", skiprows=1)
dr1
dr1['lmjd']
# 一元ufunc
arr = np.arange(10)
np.cos(arr)
# 二元ufunc
x = np.random.randn(10)
y = np.random.randn(10)
x, y
np.add(x, y)
## 自定义
def sqrt2(x,y):
return np.sqrt(x**2 + y**2)
# input: 2, output: 1
sqrt2_uf = np.frompyfunc(sqrt2, 2, 1)
sqrt2_uf(x,y)
sqrt2_uf2 = np.vectorize(sqrt2, otypes=[np.float64])
sqrt2_uf2(x,y)
各列的数据类型可能不一致
dtype=[('RA', np.float64), ('Dec', np.float64), ('Type', np.int16)]
sarr = np.array([(1.23293, 23.231234, 12), (12.3242, 332.47876, 34)], dtype=dtype)
sarr
sarr['RA']