numpy详细笔记

本文介绍了Numpy数组的创建方法,包括一维和二维数组,以及使用索引、布尔运算和广播机制。此外,还详细讲解了numpy的随机数生成函数,如rand(), randn(), randint()和choice(),并演示了如何设置随机数种子以保证可预测性。
摘要由CSDN通过智能技术生成

数组创建总结

在这里插入图片描述

数组属性总结

在这里插入图片描述

Numpy数组对象

在这里插入图片描述

Numpy和Python循环处理速度对比

在这里插入图片描述

创建 ndarray 数组

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

数组属性

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

作业练习链接:
link性能测试
创建数组与属性

在这里插入图片描述

整数数组


import numpy as np
a=np.arange(10)

b=a[2:8:2]
b
out :array([2, 4, 6])
#索引+1=平常说的第几个元素

二维数组索引

c=np.arange(20).reshape(4,5)
c
out:
array([[ 0,  1,  2,  3,  4],
         [ 5,  6,  7,  8,  9],
         [10, 11, 12, 13, 14],
         [15, 16, 17, 18, 19]])
c.ndim
out:
2
c[2]
out:
array([10, 11, 12, 13, 14])
c[2][2]
out:
12
c[...][2]
out:
array([10, 11, 12, 13, 14])
c[...][2:]
array([[10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
       #意味先取出所有行再取第三行
#行的位置用...先取出所有行
#c[...][2]意味先取出所有行再取第三行
c[2:]
array([[10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])


b=np.array([
    [0,1,2],
    [3,4,5],
    [6,7,8],
    [9,10,11]
])
a=b[[0,0,3,3],[0,2,0,2]]
#[0,0,3,3]代表行索引
#[0,2,0,2]代表列索引
#对应[0,0],[0,2],[3,0],[3,2]
a
array([ 0,  2,  9, 11])

r=np.array([[0,0],[3,3]]).reshape(4)
l=np.array([[0,2],[0,2]]).reshape(4)
r
​array([0, 0, 3, 3])
s=b[r,l].reshape((2,2))
s
out:
array([[ 0,  2],
       [ 9, 11]])
#逗号前表示行索引,逗号后表示列索引
#b[r,l]=b[[0,0,3,3],[0,2,0,2]]
#逗号前表示行索引,逗号后表示列索引
b=np.array([
    [0,1,2],
    [3,4,5],
    [6,7,8],
    [9,10,11]
])
a=b[1:3,1:3]#  等同于a=b[[1,2],[1,2]]
print(a)
[[4 5]
 [7 8]]
#创建8*8数组,并全部赋值为0
d=np.arange(64).reshape(8,8)
​
d.flat=0
print(d)
[[0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]]

布尔数组 输出的结果可以经过布尔运算

a=np.arange(12).reshape(3,4)
a[a>6]   #输出大于6的数
​
array([ 7,  8,  9, 10, 11])

a[a%2==1] #输出奇数
​a

array([[ 0, -1,  2, -1],
       [ 4, -1,  6, -1],
       [ 8, -1, 10, -1]])
a[a%2==1]=-1 #输出奇数,修改不可逆,除非建副本修改
a
array([[ 0, -1,  2, -1],
       [ 4, -1,  6, -1],
       [ 8, -1, 10, -1]])

a=np.arange(12)
a[(a>2)&(a<10)]   # 要加括号,&的用法

array([3, 4, 5, 6, 7, 8, 9])


True False的形式 表示需要和不需要的数据

a=np.arange(9).reshape(3,3)
a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

a[[0,2],[0,2]]
array([0, 8])

广播机制 (满足运算条件,小的数组会在行或列重复以和大的数组相等)

a=np.arange(12).reshape(4,3)  #43列
b=np.array([1,2,3]) #一行三列
a
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
b
array([1, 2, 3])
print(a+b)
[[ 1  3  5]
 [ 4  6  8]
 [ 7  9 11]
 [10 12 14]]
print(a*b)
[[ 0  2  6]
 [ 3  8 15]
 [ 6 14 24]
 [ 9 20 33]]

运算条件

将两个数组维度数值右对齐,如果数值有1或为空或相等,则可以进行运算

numpy的random函数

import numpy as np

numpy.random.rand()

  • numpy.random.rand(d0,d1,…,dn)
  • rand 函数根据给定维度生成 [0,1) 之间的数据,包含 0,不包含 1
  • dn 表格每个维度
  • 返回值为指定维度的 array
np.random.rand(4,2,3)#创建423列的数据
out:
array([[[0.98328983, 0.89976007, 0.45187069],
        [0.7636354 , 0.1266366 , 0.59737245]],

       [[0.23233784, 0.72466596, 0.62792478],
        [0.65601457, 0.39634565, 0.67872993]],

       [[0.94510396, 0.1209695 , 0.51040255],
        [0.48820327, 0.25108958, 0.39326264]],

       [[0.93581577, 0.7946682 , 0.22548727],
        [0.46295792, 0.62925333, 0.93611731]]])

numpy.random.randn()

  • numpy.random.rand(d0,d1,…,dn)
- randn 函数返回一个或一组具有标准正态分布样本,。
- dn 表格每个维度
- 返回值为指定维度的 array
np.random.randn() #当没有参数,返回单个数据
out:
0.9978221054194687
np.random.randn(2,4)
out:
array([[-0.91464903, -1.34407418,  0.27328322,  1.48655677],
       [-0.73953291,  1.14596789, -0.37372058,  0.87601889]])
np.random.randn(4,2,4)
out:
array([[[ 0.49249151, -0.18689611, -0.7061262 ,  0.45240466],
        [-0.83259504,  2.71097971, -1.39491662,  2.08274104]],

       [[ 1.45643729, -1.47484262,  0.05107497, -1.06490762],
        [ 0.59100238, -0.01911552, -0.19523674, -0.26440994]],

       [[ 0.78185713,  0.67089298,  1.0351736 ,  0.65612953],
        [ 0.47037743, -0.7347524 ,  0.73128454, -0.26023019]],

       [[-0.27894992,  0.03703866, -0.32102327,  0.47836213],
        [ 1.3069379 , -0.24945773, -2.07441152, -0.72807191]]])
  • 标准正态分布介绍
  • 标准正态分布—standard normal distribution
  • 标准正态分布又称为 u 分布,是以 0 为均值、以 1 为标准差的正态分布,记为 N(0,1)。

numpy.random.randint()

  • numpy.random.randint(low, high=None, size=None, dtype=‘l’)
  • 返回随机整数,范围区间为 [low,high),包含 low,不包含 high
  • 参数:low 为最小值,high 为最大值,size 为数组维度大小,dtype 为数据类型,默认的数据类型是 np.int
  • high 没有填写时,默认生成随机数的范围是 [0,low)
np.random.randint(1,size=6)#返回[0,1)的整数,所以只有0
out:
array([0, 0, 0, 0, 0, 0])
np.random.randint(2,9,size=7)
out:
array([5, 2, 4, 3, 2, 8, 2])
np.random.randint(1,5) # 返 回 1 个 [1,5) 时 间 的 随 机 整 数
out:
4
np.random.randint(-5,5,size=(2,2))   
array([[ 2, -4],
       [-4,  0]])

numpy.random.choice()

  • numpy.random.choice(a, size=None, replace=True, p=None)
  • 从给定的一维数组中生成随机数
  • 参数:a 为一维数组类似数据或整数;size 为数组维度;p 为数组中的数据出现的概率
  • a 为整数时,对应的一维数组为 np.arange(a)
  • replace = False 随机数不能重复
np.random.choice(5,size=3)
out:
array([4, 1, 1])
a=np.array([1,3,5,7])
np.random.choice(a,size=4)
array([3, 5, 1, 1])
np.random.choice(5,size=(2,3))
array([[4, 4, 0],
       [4, 3, 2]])
demo_list=['iphone','huawei','xiaomi','sansumg']
np.random.choice(demo_list,size=(2,2))
array([['iphone', 'xiaomi'],
       ['sansumg', 'huawei']], dtype='<U7')
  • 参数 p 的长度与参数 a 的长度需要一致;
  • 参数 p 为概率,p 里的数据之和应为 1
np.random.choice(demo_list,size=(2,2),p=[0.2,0.3,0.3,0.2])
array([['sansumg', 'sansumg'],
       ['huawei', 'huawei']], dtype='<U7')

np.random.seed()

  • np.random.seed() 的作用:使得随机数据可预测。
  • 当我们设置相同的 seed,每次生成的随机数相同。如果不设置 seed,则每次会生成不同的随机数

```c
 np.random.seed(0)   #这个数没有要求,一个种子生成的随机数是一样的
np.random.rand(2)
array([0.5488135 , 0.71518937])
np.random.seed(0)   #一个种子生成的随机数是一样的
np.random.rand(2)
array([0.5488135 , 0.71518937])

np.random.seed(1555)
np.random.rand(4)
array([0.06316912, 0.76071872, 0.59230514, 0.8572013 ])
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值