-----------------------------------------------------------------------------------------------------------------------------------------------
Numpy(Numerical Python) note:
(always use with scipy/scientific and matplotlib)-----------------------------------------------------------------------------------------------------------------------------------------------
1.numpy.arange(1,10,2)2.numpy.linspace(1,10,50) #second parameter can be minus number!
linealSpace:线性空间->等差数列,默认两个参数,第三个参数缺省时数列元素和取50个3.numpy.sin(np.pi * x)
x = numpy.arange(0,1.0,0.01) #generate 100 folat numbers
y = numpy.sin(2 * np.pi * x)
plt.plot(x,y,color='red')4.a = np.arange(15).reshape(row,column)
a = np.arange(150).reshape(layer,row,column)
type(a): numpy.ndarray(n dimensions array多维数组)
type(a.shape): tuple
a.shape: (3, 10, 5)
a.size: 3*10*5
type(a.size): inta.ndim(n dimensions维数/轴的个数)
a.dtype.name #数据类型
a.dtype
-> int8->int16->int32->int64
-> float16->float32->float64
可在使用array创建多维数组时指定:a = np.array(([1, 2, 3], [4, 5, 6], [7, 8, 9]), dtype='int8')5.multiply of matrix
A@B :叉乘
A.dot(B) :叉乘
A*B :点乘
import numpy as np
import matplotlib.pyplot as plt
def test_numpy_00():
"""
test linspace
draw sin() figure
"""
a = np.linspace(1, -10, 3) # linealspace线性空间->等差数列
print(a)
print(type(a))
x = np.arange(0.0, 1.0, 0.01)
y = np.sin(2 * np.pi * x)
plt.plot(x, y, color='red')
plt.show()
def test_numpy_01():
"""
test reshape
test type(reshape)、type(a.shape)、type(a.size)
"""
a = np.arange(150).reshape(3, 10, 5) #层数+行数+列数 <- 行数+列数
print(a)
print(type(a))
print(a.shape)
print(type(a.shape))
print(a.size)
print(type(a.size))
print(a.ndim)
print(type(a.ndim))
def test_numpy_02():
"""
create a array
parameter can be tuple/list
"""
a = np.array(([1, 2, 3], [4, 5, 6], [7, 8, 9]), dtype='int8')
print(a)
print(type(a))
print(a.ndim)
print(type(a.ndim))
print(a.dtype)
print(type(a.dtype))
def test_numpy_03():
"""
test zeros()
test ones()
test empty() # random numbers
"""
a = np.zeros((3, 4), dtype='int8')
print(a)
b = np.ones((3, 4, 5), dtype='int8')
print(b)
c = np.empty((3, 4), dtype='int8')
print(c)
def test_numpy_04():
"""
test multiply of matrix
"""
a = np.array([[1, 2], [4, 5]])
b = np.array([[0, 1], [7, 8]])
c = a @ b # cross multiply
d = a.dot(b) # cross multiply
e = a * b # point multiply
print(c)
print(d)
print(e)
if __name__ == "__main__":
test_numpy_04()