Numpy用法总结

import numpy as np
import pandas as pd
from pandas import Series, DataFrame

ndarray

一、创建ndarray

# 使用np.array()由Python list 创建
data = [1,2,3,4]
n1 = np.array(data)
n1
array([1, 2, 3, 4])
# 使用np的routines函数创建
# 创建多个都是1
np.ones(5,dtype=int)
array([1, 1, 1, 1, 1])
# 创建多个 都是0
np.zeros(5,dtype=int)
array([0, 0, 0, 0, 0])
# 生成指定shape形状的,值都为fill_value
np.full(shape=(3,4),fill_value=1024)
array([[1024, 1024, 1024, 1024],
       [1024, 1024, 1024, 1024],
       [1024, 1024, 1024, 1024]])
# 生成对角线都为1,其余都为0,N为rows,M为columns,不指定,默认相等N=M
np.eye(N=4,M=3)
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.],
       [ 0.,  0.,  0.]])
# 线性生成矩阵,计算start和stop,均分
np.linspace(1,100,num=50,dtype=int)
array([  1,   3,   5,   7,   9,  11,  13,  15,  17,  19,  21,  23,  25,
        27,  29,  31,  33,  35,  37,  39,  41,  43,  45,  47,  49,  51,
        53,  55,  57,  59,  61,  63,  65,  67,  69,  71,  73,  75,  77,
        79,  81,  83,  85,  87,  89,  91,  93,  95,  97, 100])
# 生成0,100之间,每次步数为5,左闭右开
np.arange(0,100,step=5)
array([ 0,  5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80,
       85, 90, 95])
# 生成随机整数,左闭右开,size为数量,可以指定形状
np.random.randint(0,100,size=(4,3))
array([[70, 19, 93],
       [24, 86, 92],
       [77, 99, 88],
       [18, 55, 29]])
# 生成标准正太分布
np.random.randn(100)
array([-0.07352457, -1.3136685 ,  1.51471995, -0.08136916, -1.11574765,
        0.17386269, -1.31727833, -0.41155414,  1.30430032, -0.86782806,
       -0.76215743, -0.50927113,  1.52628811, -0.18189482, -0.02033947,
        1.97168795,  0.24356417,  0.98391052, -0.41973603,  0.32262384,
       -1.26531272,  0.51957893, -0.53221152, -0.18077624, -0.04793903,
       -1.07987469, -0.14964406, -1.50501509,  0.22443113,  1.02252398,
        1.8288228 ,  0.14265383, -1.07763155, -1.45768047,  0.57438741,
       -0.02607326,  0.76216751, -0.21004165, -1.45035988, -2.21018297,
        0.18629033,  0.51804587,  0.6108886 , -1.79317464, -0.01333462,
       -1.02223808,  1.02241954, -0.21770695, -0.13238381, -2.43526884,
        1.77094614,  1.58023618,  0.86634068,  0.63893764, -0.09904435,
        0.19179536,  0.70664661,  0.18497984,  1.06407283,  0.08213135,
        0.1189964 , -0.05590335, -0.73709569, -0.14316891, -0.90192259,
        0.53208371,  1.52754737, -0.06674151,  1.07920869,  0.42858941,
       -1.15588987, -1.10701065,  0.01149264, -1.36904717, -0.0157708 ,
        0.81281272, -0.87613834, -0.05256806,  0.36815293, -1.19499011,
        1.28316201,  0.68097169, -0.02886232, -0.45725581,  2.17369421,
        0.67138163,  1.7039416 ,  0.16232207, -0.3319481 ,  0.8932495 ,
       -1.67827068, -1.61199462,  0.60000361, -1.52476205, -0.28960245,
        0.98590361, -0.67797509, -1.03187526, -0.84967747,  0.11290939])
# 生成以10为中心,偏移5,的10个数
np.random.normal(10,5,10)
array([  8.41062812,   7.48295308,  17.81455213,  15.14675061,
         3.74643964,  10.79360893,   5.2854533 ,  16.02048099,
         3.22449646,   3.80641927])
# 生成0-1的随机数,左闭右开
np.random.random(10)
array([ 0.68289218,  0.80123252,  0.91247595,  0.73536506,  0.867282  ,
        0.89442711,  0.06441671,  0.80846567,  0.77142565,  0.19021568])

二、ndarray的属性

# ndim:维度;shape:形状;size:总长度;dtype:元素类型
n = np.random.randint(100,size=(4,4))
display(n.ndim, n.shape, n.size, n.dtype)
2



(4, 4)



16



dtype('int64')

三、ndarray的基本操作

n = np.random.randint(100,size=(4,4,4))
n
array([[[16, 66, 17, 90],
        [97, 29, 56, 95],
        [10, 97, 63, 22],
        [39, 83, 25, 89]],

       [[52, 88, 36, 20],
        [64, 25, 51, 78],
        [48, 95, 87, 63],
        [97, 73, 83, 55]],

       [[16, 16,  7, 51],
        [10, 99, 83, 10],
        [45, 35,  5, 20],
        [68, 71, 23,  7]],

       [[81, 20, 55, 63],
        [ 8, 39,  9, 40],
        [66,  6, 79, 92],
        [10, 79, 94, 36]]])
# 索引
# 取出特定的值,还可以进行修改
n[0,0,0]
16
# 切片
n[:]
array([[[16, 66, 17, 90],
        [97, 29, 56, 95],
        [10, 97, 63, 22],
        [39, 83, 25, 89]],

       [[52, 88, 36, 20],
        [64, 25, 51, 78],
        [48, 95, 87, 63],
        [97, 73, 83, 55]],

       [[16, 16,  7, 51],
        [10, 99, 83, 10],
        [45, 35,  5, 20],
        [68, 71, 23,  7]],

       [[81, 20, 55, 63],
        [ 8, 39,  9, 40],
        [66,  6, 79, 92],
        [10, 79, 94, 36]]])
# 多维切片
n[:,0:2]
array([[[16, 66, 17, 90],
        [97, 29, 56, 95]],

       [[52, 88, 36, 20],
        [64, 25, 51, 78]],

       [[16, 16,  7, 51],
        [10, 99, 83, 10]],

       [[81, 20, 55, 63],
        [ 8, 39,  9, 40]]])
# 数据反转
n[::-1]
array([[[81, 20, 55, 63],
        [ 8, 39,  9, 40],
        [66,  6, 79, 92],
        [10, 79, 94, 36]],

       [[16, 16,  7, 51],
        [10, 99, 83, 10],
        [45, 35,  5, 20],
        [68, 71, 23,  7]],

       [[52, 88, 36, 20],
        [64, 25, 51, 78],
        [48, 95, 87, 63],
        [97, 73, 83, 55]],

       [[16, 66, 17, 90],
        [97, 29, 56, 95],
        [10, 97, 63, 22],
        [39, 83, 25, 89]]])
# 变形
n = np.random.randint(100,size=16).reshape((4,4))
n
array([[67, 11,  6, 41],
       [51,  9, 30, 73],
       [98, 75, 52, 84],
       [34, 17,  2, 19]])
# 级联
# 级联的参数是列表,要加中括号或者小括号
# 维度必须相同
# 级联的方向,默认是shape这个touple的第一个值所代表的方向的维度
# 可通过axis参数改变级联的方向
n1 = np.random.randint(100, size=(4,4))
n2 = np.random.randint(100, size=(4,4))
display(n1,n2)
array([[78,  0,  3,  0],
       [28, 72, 92, 59],
       [46, 14, 97, 63],
       [15, 45,  2, 63]])



array([[85, 61, 30, 21],
       [59, 66, 66, 36],
       [87, 72, 15, 72],
       [11, 33, 12, 16]])
np.concatenate((n1,n2))
array([[78,  0,  3,  0],
       [28, 72, 92, 59],
       [46, 14, 97, 63],
       [15, 45,  2, 63],
       [85, 61, 30, 21],
       [59, 66, 66, 36],
       [87, 72, 15, 72],
       [11, 33, 12, 16]])
np.concatenate((n1,n2),axis=1)
array([[78,  0,  3,  0, 85, 61, 30, 21],
       [28, 72, 92, 59, 59, 66, 66, 36],
       [46, 14, 97, 63, 87, 72, 15, 72],
       [15, 45,  2, 63, 11, 33, 12, 16]])
# 水平级联与竖直级联
# 处理自己,进行维度的改变
# hstack水平级联,horizon
n_stack = np.hstack(n1)
n_stack
array([78,  0,  3,  0, 28, 72, 92, 59, 46, 14, 97, 63, 15, 45,  2, 63])
# vstack数值级联,vertical
np.vstack(n_stack)
array([[78],
       [ 0],
       [ 3],
       [ 0],
       [28],
       [72],
       [92],
       [59],
       [46],
       [14],
       [97],
       [63],
       [15],
       [45],
       [ 2],
       [63]])
# 与级联类似,三个函数完成切分的工作
# 按照索引,下标3(不包括)切一下,下标5(不包括)切一下
>>>a = np.random.randint(0, 10, size=20)
>>>np.split(a, [3,5])
[array([3, 4, 4]),
    array([4, 8]),
    array([4, 6, 4, 9, 8, 7, 2, 1, 9, 6, 0, 4, 2, 8, 6]]
>>>b = np.random.randint(0,10,size = 20).reshape((4,5))
>>>b
array([[6, 0, 1, 0, 7],
        [3, 3, 5, 9, 4],
        [4, 9, 5, 4, 5],
        [5, 6, 9, 5, 3]])
# 下标1(不包括1,其实是下标0)切一下,下标3(其实是2)切一下
>>>np.vsplit(b,(1,3))
[array([[6, 0, 1, 0, 7]]), array([[3, 3, 5, 9, 4],
            [4, 9, 5, 4, 5]]), array([[5, 6, 9, 5, 3]])]
>>>np.hsplit(b, (2,3))
[array([[6, 0],
        [3, 3],
        [4, 9],
        [5, 6]]), array([[1],
        [5],
        [5],
        [9]]), array([[0, 7],
        [9, 4],
        [4, 5],
        [5, 3]])]
Function Name    NaN-safe Version    Description
np.sum              np.nansum     Compute sum of elements
np.prod             np.nanprod    Compute product of elements
np.mean             np.nanmean    Compute mean of elements
np.std              np.nanstd     Compute standard deviation
np.var              np.nanvar     Compute variance
np.min              np.nanmin     Find minimum value
np.max              np.nanmax     Find maximum value
np.argmin           np.nanargmin  Find index of minimum value
np.argmax           np.nanargmax  Find index of maximum value
np.median           np.nanmedian  Compute median of elements
np.percentile       np.nanpercentile    Compute rank-based statistics of elements
np.any                  N/A    Evaluate whether any elements are true
np.all                  N/A    Evaluate whether all elements are true
np.power               幂运算
# np.sum 和 np.nansum 的区别。nan: not a number
>>>b = np.array([1, 2, None])
>>>b
array([1, 2, None], dtype=object)
# np.nansum(b)会报错
# np.nan == not a number
>>>c = np.array([1,10,np.nan])
>>>np.nansum()
11.0
# 矩阵积
>>>a = np.array([[-3,0],[5,0.5]])
>>>b = np.array([[-7,2],[4,6]])
>>>display(np.dot(a,b),a*b)
array([[ 21.,  -6.],
       [-33.,  13.]])
array([[ 21.,   0.],
       [ 20.,   3.]])
# np.sort()与ndarray.sort()都可以,但有区别:
# np.sort()不改变输入
# ndarray.sort()本地处理,不占用空间,但改变输入
Python中的NumPy是一个用于科学计算和数据分析的基础包,提供了大量的矩阵和数组的计算函数。安装NumPy可以使用pip命令:pip install numpy。要查看NumPy的版本,可以使用numpy.__version__。在代码中导入NumPy时,通常使用import numpy as npNumPy的主要功能之一是创建ndarray(N-dimensional array)。我们可以使用np.array()函数来创建多维数组。例如,使用np.array([1, 2, 3])可以创建一个一维数组,使用np.array([[1, 2], [3, 4]])可以创建一个二维数组。 除了np.array()函数外,NumPy还提供了其他创建数组的方法,比如使用np.zeros()创建全零数组,np.ones()创建全一数组,np.random.randn()创建随机数组等。 另外,如果想对矩阵进行转置,可以使用numpy的transpose()函数来实现。例如,对于一个二维数组a,可以使用np.transpose(a)来实现转置操作。 总结起来,Python中的NumPy是一个功能强大的科学计算和数据分析的基础包,可以用来创建和操作多维数组,以及进行矩阵的转置操作等。这些功能可以帮助我们进行高效的数值计算和数据处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [numpy 用法总结](https://blog.csdn.net/Dear_learner/article/details/125018228)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [python-numpy的详细使用](https://blog.csdn.net/wander_ing/article/details/105763309)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Python 使用Numpy对矩阵进行转置的方法](https://download.csdn.net/download/weixin_38693192/12864784)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值