Numpy练习

参考链接

创建数组

TestArray.py

import numpy as np

# 一维数组
def test1DArray():
    one = np.array([1, 2, 3])
    print("1D Array:\n", one)

# 二维数组
def test2DArray():
    two = np.array([[1, 2, 3], [4, 5, 6]])
    print("2D Array:\n", two)

# 创建全0数组
def testZeroArray():
    # shape代表形状,这里是5行3列的二维数组
    zero = np.zeros(shape=(5, 3))
    print("zero Array:\n", zero)

# 创建全1数组
def testOneArray():
    one = np.ones(shape=(5, 3))
    print("One Array:\n", one)

# 创建全空数组,与全0不一样,表示的是无限小,
# 单独运行时结果确实是无限小,不知道为什么上面有其他函数运行后,他的结果就不是无限小了
def testEmptyArray():
    empty = np.empty(shape=(5, 3))
    print("Empty Array:\n", empty)

# 创建有连续序列的数组arange
def testArangeArray():
    # 10-16的数据,步长为2
    arange = np.arange(10, 16, 2)
    print("Range Array:\n", arange)

# 创建有连续间隔的数组linspace
def testLinspaceArray():
    # 创建线段型数据,从1开始,到10结束,且分割成20个数据,生成线段
    linspace = np.linspace(1, 10, 20)
    print("Linspace Array:\n", linspace)


# 创建随机数组
def testRandomArray():
    # 创建一个3行4列,值在0-1之间的数组
    random = np.random.rand(3, 4)
    print("Random Array:\n", random)
    # 这种方式创建出来的random数组是4行5列,值在2-5之间
    random1 = np.random.randint(2, 5, size=(4, 5))
    print("Random1 Array:\n", random1)

# 改变数组形状,下面是2行5列改成5行2列
def testReshapeArray():
    data1 = [1, 2, 3, 4, 5]
    data2 = [6, 7, 8, 9, 10]
    data = np.array([data1, data2])
    print("before reshape:")
    print(data.shape)
    data = data.reshape(5, 2)
    print("after reshape:")
    print(data.shape)

# 数组转置
def testTranspositionArray():
    data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    data_array = np.array(data)
    print("before transposition:")
    print(data)
    print("after transposition:")
    print(data_array.T)

main.py

import TestArray as testArray


# 按装订区域中的绿色按钮以运行脚本。
if __name__ == '__main__':
    testArray.test1DArray()
    testArray.test2DArray()
    testArray.testZeroArray()
    testArray.testOneArray()
    testArray.testEmptyArray()
    testArray.testArangeArray()
    testArray.testLinspaceArray()
    testArray.testRandomArray()
    testArray.testReshapeArray()
    testArray.testTranspositionArray()

运行结果

1D Array:
 [1 2 3]
2D Array:
 [[1 2 3]
 [4 5 6]]
zero Array:
 [[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
One Array:
 [[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
Empty Array:             (这个结果有问题)
 [[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
Range Array:
 [10 12 14]
Linspace Array:
 [ 1.          1.47368421  1.94736842  2.42105263  2.89473684  3.36842105
  3.84210526  4.31578947  4.78947368  5.26315789  5.73684211  6.21052632
  6.68421053  7.15789474  7.63157895  8.10526316  8.57894737  9.05263158
  9.52631579 10.        ]
Random Array:
 [[0.63689452 0.42798231 0.19218566 0.87573321]
 [0.93669444 0.0808807  0.41104911 0.18225544]
 [0.37329302 0.88089557 0.30469678 0.03274842]]
Random1 Array:
 [[4 2 2 2 3]
 [2 2 2 2 3]
 [3 3 3 2 2]
 [4 3 2 3 4]]
before reshape:
(2, 5)
after reshape:
(5, 2)
before transposition:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
after transposition:
[[1 4 7]
 [2 5 8]
 [3 6 9]]

数组显示

# 打印数组的维度、形状、元素个数、数据类型
def testArrayDetail():
    data = np.array([[1, 2, 3], [4, 5, 6]])
    print("data dim: ", data.ndim)
    print("data shape: ", data.shape)
    print("data size: ", data.size)
    print("data type: ", data.dtype)
data dim:  2
data shape:  (2, 3)
data size:  6
data type:  int32

数组操作

# 数组运算
def testArrayCalculation():
    # 同一个形状才可以加减乘除操作,比如都是1行3列
    array1 = np.array([1, 2, 3])
    array2 = np.array([4, 5, 6])
    result = array1 + array2
    print("add result: ", result)
    result = array1 * array2
    print("multiply result: ", result)

    data = np.array([1, 5, 6, 9])
    # 平均值
    print("mean result: ", np.mean(data))
    # 中位数
    print("median result: ", np.median(data))
    # 标准差
    print("std result: ", np.std(data))
    # 方差
    print("variance result: ", np.var(data))
    # 最小值
    print("min result: ", np.min(data))
    # 最大值
    print("max result: ", np.max(data))
    # 元素之和
    print("sum result: ", np.sum(data))
    # 元素乘积
    print("prod result: ", np.prod(data))
    # 前缀和
    print("cumsum result: ", np.cumsum(data))
add result:  [5 7 9]
multiply result:  [ 4 10 18]
mean result:  5.25
median result:  5.5
std result:  2.8613807855648994
variance result:  8.1875
min result:  1
max result:  9
sum result:  21
prod result:  270
cumsum result:  [ 1  6 12 21]

数组切片

切片操作是左闭右开的,即包括起始位置,但不包括结束位置

# 一维数组
def testArraySection():
    arr = np.array([1, 2, 3, 4, 5])
    print("1D Array[1:4]:\n", arr[1:4])

多维数组看大佬参考链接解释
总体思路就是一维一维地分解,并不是什么高深的东西

数组堆叠

def testArrayStack():
    arr = np.array([1, 2, 3, 4, 5])
    arr2 = np.array([6, 7, 8, 9, 10])
    # 垂直堆叠
    print("vstack: ", np.vstack((arr, arr2)))
    # 水平堆叠
    print("hstack: ", np.hstack((arr, arr2)))
vstack:  [[ 1  2  3  4  5] [ 6  7  8  9 10]]
hstack:  [ 1  2  3  4  5  6  7  8  9 10]

数组保存


def testArraySave():
    # arr = np.array([1, 2, 3, 4, 5])
    # np.save('my_array.npy', arr)
    arr = np.load('my_array.npy')
    print("array: ", arr)

执行save时会在当前项目路径下生产一个my_array.npy文件,等到实际用的时候再通过load把数组拿出来

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值