Python - Numpy_Test

-----------------------------------------------------------------------------------------------------------------------------------------------

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): int

    a.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()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值