Numpy学习记录:ndarray介绍、生成数组、数组切片、随机数、正态分布、转置、去重、类型转换、逻辑运算、统计运算、矩阵运算

本文详细介绍了NumPy库中ndarray对象的属性,如shape、ndim、size和dtype等,并展示了如何生成不同类型的数组,包括0或1数组、从现有数组生成、固定范围数组以及随机数组。接着,讨论了数组的基本操作,如索引切片、形状修改、转置以及类型转换。此外,还涵盖了数组去重、逻辑运算和统计运算,包括比较运算、通用判断函数和三元运算符。最后,演示了数组与数及数组间的运算,包括矩阵乘法和广播机制。
摘要由CSDN通过智能技术生成

ndarray的属性

在这里插入图片描述

import numpy as np

score = np.array([[1, 2, 3, 4, 5, 6],
                   [2, 3, 4, 5, 6, 7],
                   [3, 4, 5, 6, 7, 8],
                   [4, 5, 6, 7, 8, 9]])
score
array([[1, 2, 3, 4, 5, 6],
       [2, 3, 4, 5, 6, 7],
       [3, 4, 5, 6, 7, 8],
       [4, 5, 6, 7, 8, 9]])
score.shape
(4, 6)
score.ndim
2
score.size
24
score.itemsize
4
score.dtype
dtype('int32')

ndarray的基本操作

生成数组的方法

生成0或1数组

import numpy as np

ones = np.ones([3, 4])

ones
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
temp = np.array([[1, 2, 3, 4, 5, 6],
                 [2, 3, 4, 5, 6, 7],
                 [3, 4, 5, 6, 7, 8],
                 [4, 5, 6, 7, 8, 9]])
ones = np.ones_like(temp)
ones
array([[1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1]])
# 类似的有np.zeros()和np.zeros_like()

从现有数组生成

import numpy as np

a = np.array([[1, 2, 3],
             [2, 3, 4]])
# 创建一个新数组
a1 = np.array(a)

# 相当于索引的形式,并没有真正拷贝
a2 = np.asarray(a)

a[1][2] = 100

print("a:" + (",".join(str(i) for i in a)) + "\na1:" + (",".join(str(i) for i in a1)) + "\na2:" + (",".join(str(i) for i in a)) + "\n")
a:[1 2 3],[  2   3 100]
a1:[1 2 3],[2 3 4]
a2:[1 2 3],[  2   3 100]

生成固定范围的数组

# 生成等间隔的数组(有小数最好用这个而不是arange) start end num

np.linspace(0, 10, 101)

array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ,
        1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8,  1.9,  2. ,  2.1,
        2.2,  2.3,  2.4,  2.5,  2.6,  2.7,  2.8,  2.9,  3. ,  3.1,  3.2,
        3.3,  3.4,  3.5,  3.6,  3.7,  3.8,  3.9,  4. ,  4.1,  4.2,  4.3,
        4.4,  4.5,  4.6,  4.7,  4.8,  4.9,  5. ,  5.1,  5.2,  5.3,  5.4,
        5.5,  5.6,  5.7,  5.8,  5.9,  6. ,  6.1,  6.2,  6.3,  6.4,  6.5,
        6.6,  6.7,  6.8,  6.9,  7. ,  7.1,  7.2,  7.3,  7.4,  7.5,  7.6,
        7.7,  7.8,  7.9,  8. ,  8.1,  8.2,  8.3,  8.4,  8.5,  8.6,  8.7,
        8.8,  8.9,  9. ,  9.1,  9.2,  9.3,  9.4,  9.5,  9.6,  9.7,  9.8,
        9.9, 10. ])
# arange(通常用于整数)start end step

np.arange(1, 20, 2)
array([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19])
# 等比数列 start end num base(默认为10)

np.logspace(1, 4, 4)
array([   10.,   100.,  1000., 10000.])

生成随机数租

import numpy as np

# 生成m*n的数组,每个值在[0-1)之间

np.random.rand(2, 3)# m, n
array([[0.19135166, 0.52947236, 0.76666105],
       [0.51849476, 0.06500935, 0.50160038]])
np.random.uniform(1, 20, size=(2, 3))# low high(左闭右开), size. size是数组的大小,可以一维也可以多维
array([[ 3.00045022, 16.51641481, 10.87714811],
       [19.01537556, 16.99783209, 17.30736074]])
np.random.randint(low = 2, high = 10, size=(2, 3))
# 函数的作用是,返回一个随机整型数,范围从低(包括)到高(不包括),即[low, high)。
# 如果没有写参数high的值,则返回[0,low)的值。
array([[5, 4, 4],
       [6, 2, 4]])

生成正态分布数组

# 标准正态分布
print("传入n维的大小")
print(np.random.randn(4, 2))

print("\n同上")
print(np.random.standard_normal(size=(3, 4)))
print("\n都一样,都是标准的正态分布")
传入n维的大小
[[-0.10247791 -0.302461  ]
 [ 0.43151273 -0.03009678]
 [ 0.22533906 -0.50855367]
 [ 0.4363346  -1.41143221]]

同上
[[ 0.16257913  0.83891324 -0.2343641  -1.94905029]
 [-0.53955478 -1.29111421 -1.31547997  0.64587412]
 [ 0.5742727  -0.28350871  0.66280201 -1.52803032]]

都一样,都是标准的正态分布
# 正态分布
np.random.normal(loc=1.75, scale=2.5, size=(2, 3))# 均值。标准差,大小
array([[ 1.2951935 ,  2.89116038, -0.06128184],
       [ 4.3991733 ,  3.32387725,  0.52387362]])

生成几个分布小案例

import numpy as np
import matplotlib.pyplot as plt

# 生成随机分布的均匀数
data =  np.random.uniform(-1, 1, 10000000)

# 画图看分布状况

# 创建画布
plt.figure(figsize=(20, 8), dpi=100)

# 绘制直方图
plt.hist(x=data, bins=1000)# bins代表有多少组

# 绘制图像
plt.show()

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt

# 生成正态分布的均匀数
data =  np.random.normal(1.75, 2.5, 10000000)

# 画图看分布状况

# 创建画布
plt.figure(figsize=(20, 8), dpi=100)

# 绘制直方图
plt.hist(x=data, bins=1000)# bins代表有多少组

# 绘制图像
plt.show()

在这里插入图片描述

数组基本操作

数组索引切片

import numpy as np

stock_change = np.random.normal(0, 1, (4, 5))
stock_change
array([[ 0.76801499, -1.33287166, -0.59273324, -0.06655721, -0.29520877],
       [-0.60462515,  1.25515367, -1.88420884,  0.62095113,  1.90918972],
       [ 0.25226408, -0.65847108,  1.29888936, -0.76053287,  0.73216724],
       [ 0.3186477 ,  0.72990549, -1.1674782 , -0.23469572, -1.34058423]])
stock_change[0:1, 0:3] # 先切第一维再切第二维
array([[-0.95873687, -0.48295261, -0.35280297]])

形状修改

reshape()产生新的数组,原来数组不变

import numpy as np

stock_change = np.random.normal(0, 1, (4, 5))
print(stock_change)

stock_change.reshape([5, 4])
[[-0.87870891 -0.34540774 -0.79158385 -1.98982512 -0.73392165]
 [ 0.15196991 -1.41316382 -1.25018898  0.06092648 -0.17250281]
 [ 1.13816396  0.72506707  0.0936578   1.98472725 -0.50617628]
 [ 0.22088737  1.84383482  0.49270367 -0.0186252   0.70370198]]





array([[-0.87870891, -0.34540774, -0.79158385, -1.98982512],
       [-0.73392165,  0.15196991, -1.41316382, -1.25018898],
       [ 0.06092648, -0.17250281,  1.13816396,  0.72506707],
       [ 0.0936578 ,  1.98472725, -0.50617628,  0.22088737],
       [ 1.84383482,  0.49270367, -0.0186252 ,  0.70370198]])
# 知道一维的大小即可另一维可以省略为-1
stock_change.reshape([-1, 10])
array([[-0.87870891, -0.34540774, -0.79158385, -1.98982512, -0.73392165,
         0.15196991, -1.41316382, -1.25018898,  0.06092648, -0.17250281],
       [ 1.13816396,  0.72506707,  0.0936578 ,  1.98472725, -0.50617628,
         0.22088737,  1.84383482,  0.49270367, -0.0186252 ,  0.70370198]])
# 必须整除不然报错
stock_change.reshape([-1, 3])
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

Input In [13], in <cell line: 2>()
      1 # 必须整除不然报错
----> 2 stock_change.reshape([-1, 3])


ValueError: cannot reshape array of size 20 into shape (3)

resize()直接改变当前数组的值

import numpy as np

stock_change = np.random.normal(0, 1, (4, 5))
print(stock_change)

print("\n")

stock_change.resize([5, 4])
print(stock_change)
[[-0.4705347  -0.03172707  1.20034808  0.34953259 -0.18518757]
 [ 0.66582051  0.2342336  -0.3283656  -0.19566417  1.91230817]
 [-0.37197274 -0.82963941  0.34968941  1.58491501 -0.7666581 ]
 [-0.57245321  0.38757326  0.84230422  0.29047579  0.31681754]]


[[-0.4705347  -0.03172707  1.20034808  0.34953259]
 [-0.18518757  0.66582051  0.2342336  -0.3283656 ]
 [-0.19566417  1.91230817 -0.37197274 -0.82963941]
 [ 0.34968941  1.58491501 -0.7666581  -0.57245321]
 [ 0.38757326  0.84230422  0.29047579  0.31681754]]

T转置不会改变原来的数组

import numpy as np

stock_change = np.random.normal(0, 1, (3, 5))
print(stock_change)

print("\n")

print(stock_change.T)

print("\n")

print(stock_change)

[[-1.06005484  1.15345569  1.71982981 -0.71750619 -0.58880189]
 [ 0.50318628  0.17658125 -0.03911683 -0.65436872 -0.2555744 ]
 [ 0.72744857  1.20266405  1.13856298  1.06341566  0.75381428]]


[[-1.06005484  0.50318628  0.72744857]
 [ 1.15345569  0.17658125  1.20266405]
 [ 1.71982981 -0.03911683  1.13856298]
 [-0.71750619 -0.65436872  1.06341566]
 [-0.58880189 -0.2555744   0.75381428]]


[[-1.06005484  1.15345569  1.71982981 -0.71750619 -0.58880189]
 [ 0.50318628  0.17658125 -0.03911683 -0.65436872 -0.2555744 ]
 [ 0.72744857  1.20266405  1.13856298  1.06341566  0.75381428]]

类型修改

例子

import numpy as np

stock_change = np.random.normal(0, 1, (4, 5))

stock_change.astype(np.int32)
array([[ 0, -1,  0,  0,  0],
       [ 0, -1, -1,  0,  0],
       [ 0,  0,  1,  0,  0],
       [ 0,  1,  0,  0,  0]])

转化为bytes

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[12, 3, 34], [5, 6, 7]]])
arr.tobytes()
b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00\x0c\x00\x00\x00\x03\x00\x00\x00"\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00\x07\x00\x00\x00'

数组去重

arr = np.array([[1, 2, 3, 4, 5], [4, 5, 6, 7, 8]])

# 不改变原来的数组
np.unique(arr)
array([1, 2, 3, 4, 5, 6, 7, 8])

数组运算

逻辑运算

import numpy as np

stock_change = np.random.normal(0, 1, (8, 10))

stock_change
array([[ 0.48726938,  1.41029693, -1.74478236,  2.59006553,  1.01267122,
        -1.42721614,  0.79795668,  1.48830645,  1.32591467, -0.343406  ],
       [ 0.20751247,  1.03113678,  0.90999482, -0.93109363, -2.20728299,
         1.55877724,  0.43970143, -0.22270693,  0.41944351,  1.67221201],
       [-1.04241982,  0.66476944, -0.20849201,  0.48027243,  0.99194936,
        -0.73396597,  0.461289  ,  0.16602602,  1.25863874, -1.10716444],
       [ 0.06754715,  0.79882888, -0.43428688, -0.03912004,  1.42440581,
        -1.07315856,  1.53475397, -0.24560464, -2.33046679, -0.09257724],
       [-0.72306501, -0.38233658,  1.01153807, -0.55146991,  0.22015818,
        -0.55540457,  0.13352199, -1.01723782, -0.32738677,  1.59193651],
       [ 0.07677289, -0.20106256, -0.09749797,  1.63645891, -0.52249891,
        -0.16865819, -1.32453895, -0.02542151, -1.54402959, -2.15037454],
       [-0.08704302, -0.3064378 ,  0.21584889,  0.45547149,  2.16270059,
         0.20217298, -0.18771071, -0.98187427,  2.24541396,  0.26691165],
       [ 1.37549845,  0.34830687,  0.22041938, -1.28352399, -1.02851443,
         0.27487662, -2.67331784,  0.77372558, -0.95653509,  0.42517371]])
stock_c = stock_change[0:5, 0:5]

stock_c
array([[ 0.48726938,  1.41029693, -1.74478236,  2.59006553,  1.01267122],
       [ 0.20751247,  1.03113678,  0.90999482, -0.93109363, -2.20728299],
       [-1.04241982,  0.66476944, -0.20849201,  0.48027243,  0.99194936],
       [ 0.06754715,  0.79882888, -0.43428688, -0.03912004,  1.42440581],
       [-0.72306501, -0.38233658,  1.01153807, -0.55146991,  0.22015818]])

比较运算符

只使用比较运算符
stock_c > 1
array([[False,  True, False,  True,  True],
       [False,  True, False, False, False],
       [False, False, False, False, False],
       [False, False, False, False,  True],
       [False, False,  True, False, False]])
比较运算符加赋值
stock_c[stock_c > 1] = 2

stock_c
array([[ 0.48726938,  2.        , -1.74478236,  2.        ,  2.        ],
       [ 0.20751247,  2.        ,  0.90999482, -0.93109363, -2.20728299],
       [-1.04241982,  0.66476944, -0.20849201,  0.48027243,  0.99194936],
       [ 0.06754715,  0.79882888, -0.43428688, -0.03912004,  2.        ],
       [-0.72306501, -0.38233658,  2.        , -0.55146991,  0.22015818]])

通用判断函数

import numpy as np

stock_change = np.random.normal(0, 1, (8, 10))

stock_change
array([[ 0.98192818, -0.7423644 ,  0.83856622, -1.38288695,  1.58403207,
         0.13193475, -0.25999827,  1.7841822 ,  1.30519564, -0.96306291],
       [-0.31951504,  0.32116463, -0.32130427,  0.13737013,  0.33685948,
         0.53060045,  0.65126372,  0.13254858,  0.4952142 ,  0.16590108],
       [ 0.54935166,  1.03379519,  1.91459765,  0.2129722 ,  0.60982324,
        -2.18983737,  0.3695911 ,  0.31565123, -0.1881202 ,  1.33497577],
       [ 0.49871806,  0.64799038,  0.45480588,  0.98934396, -1.83629375,
        -0.3899517 ,  0.07775308,  0.3858998 ,  0.65899643, -2.13548272],
       [-1.15089312, -0.41468495, -0.29641972, -0.06418452,  1.83018199,
         1.09597832, -1.02162633,  1.33509145,  0.13632115,  1.63047904],
       [-0.33200145, -2.14452499, -1.87558523,  1.64391717, -0.92365358,
        -0.35480242,  1.44446196,  1.80457897,  0.66642436, -1.34582758],
       [ 0.61174781, -0.68371711,  0.02142601, -0.42038404, -1.90784219,
         0.17519257, -0.93224004, -0.09624388, -0.11561394,  1.59702833],
       [-1.09257017, -1.18410096, -0.84730629,  1.10644004,  1.02496394,
         0.92711254, -0.03559252,  0.56931272,  0.21141447,  1.07765783]])

all()

stock_d = stock_change[0:2, 0:5]

stock_d
array([[ 0.98192818, -0.7423644 ,  0.83856622, -1.38288695,  1.58403207],
       [-0.31951504,  0.32116463, -0.32130427,  0.13737013,  0.33685948]])
np.all(stock_d > 1)
False

any()

np.any(stock_d > 1)
True

np.where()三元运算符

import numpy as np

stock_change = np.random.normal(0, 1, (8, 10))

stock_change
array([[-0.00738898, -1.52686441,  1.08290361, -1.03663786,  0.39182115,
         1.34726045,  0.24912741,  0.04008401,  0.14051921,  0.57549206],
       [ 0.1458114 , -0.97656353, -0.26536879, -0.05386619,  0.08092551,
        -0.15171341, -0.51185827,  0.00923498,  0.57592761, -1.90525554],
       [-0.17032273,  1.55207936,  0.5175043 , -0.7154653 , -0.5229542 ,
        -0.40910814,  0.84623971,  0.05425052,  0.33717293, -1.91621428],
       [-0.45509579, -0.30229342, -2.01412473,  0.70763634,  0.15047674,
        -0.26415425,  0.90636651, -0.94660084, -0.32585857, -1.38341451],
       [ 0.19685245, -2.08524275, -1.98350852, -2.61353146,  0.66184551,
         2.04057251,  0.224485  , -0.14955347,  0.30138992, -1.05298851],
       [ 0.97219025,  0.60025636, -0.53889732,  0.54928153, -0.26499179,
        -0.32344942,  0.03399285,  1.19792913,  0.21370443,  1.03626431],
       [ 0.73258952, -1.03036255, -0.07775774, -1.52229579,  0.93892208,
         1.25872243, -0.26356544, -0.69322137,  1.23721931, -0.09300135],
       [ 0.80852665, -0.32230339,  1.19136761,  0.08683314,  0.792351  ,
         0.85248659,  0.81597551, -0.0154996 ,  1.4619294 , -0.59732485]])
stock_e = stock_change[0:2, 0:5]

stock_e
array([[-0.00738898, -1.52686441,  1.08290361, -1.03663786,  0.39182115],
       [ 0.1458114 , -0.97656353, -0.26536879, -0.05386619,  0.08092551]])

简单条件

temp = stock_e.copy()

np.where(temp > 0, 1, -1)
array([[-1, -1,  1, -1,  1],
       [ 1, -1, -1, -1,  1]])

复合条件

temp = stock_e.copy()

# 借助np.logical_and()实现逻辑与
np.where(np.logical_and(temp > 0.1, temp < 1), 1, -1)
array([[-1, -1, -1, -1,  1],
       [ 1, -1, -1, -1, -1]])
temp = stock_e.copy()

# 借助np.logical_or()实现逻辑与
np.where(np.logical_or(temp < -1, temp > 1), 1, -1)
array([[-1,  1,  1,  1, -1],
       [-1, -1, -1, -1, -1]])

统计运算

import numpy as np

stock_change = np.random.normal(0, 1, (8, 10))

stock_change
array([[-1.40664111,  2.53898609, -0.71332096,  0.77131387,  0.25064588,
        -1.69003702, -2.28315036, -1.03668485, -0.56831104, -0.68103245],
       [-0.86180553, -1.0177156 ,  0.607172  ,  0.03113487, -0.58259596,
         0.00878276, -1.06227643,  0.90856394, -0.73536542,  1.86090758],
       [ 0.17425504,  0.77046619, -0.26241965, -0.81811201, -1.50948498,
        -0.22741503, -1.05610638, -0.57417931, -0.79330999,  0.39906742],
       [ 0.46367558,  1.63662751,  1.85419601,  1.61959579,  0.56524124,
        -1.15652665, -2.11979141,  0.99864112,  1.00307604, -0.41367357],
       [ 0.41387701, -2.67105107, -0.58281472,  0.08118211,  1.14831002,
        -1.84941568,  0.38750209,  0.06922559,  0.65160754, -1.12143123],
       [-0.93708386,  0.57935251, -1.99391439, -0.5994678 , -0.90515329,
        -0.66804239,  0.07250328,  0.08935587, -1.14373627, -2.3963548 ],
       [-0.37794642, -0.54585281, -0.39236249, -1.32356628, -1.62452409,
        -0.13771112, -0.79339711, -1.05047013,  1.35623853, -0.49894184],
       [ 1.10786105, -1.08252792,  0.25773715, -0.45512644, -2.40380837,
         0.74376359,  0.72246425, -0.63946866, -0.93127191,  0.79521714]])

求出具体的统计值

# 直接写求的是所有数据中的最大值,加上轴就求每一列或每一行的最大值
print("最小值{}".format(np.min(stock_change)))
print("最大值{}".format(np.max(stock_change, axis=0)))
print("中位数{}".format(np.median(stock_change)))
print("均值{}".format(np.mean(stock_change)))a
print("标准差{}".format(np.std(stock_change)))
print("方差{}".format(np.var(stock_change)))
最小值-2.67105106527336
最大值[1.10786105 2.53898609 1.85419601 1.61959579 1.14831002 0.74376359
 0.72246425 0.99864112 1.35623853 1.86090758]
中位数-0.47703414078907447
均值-0.2969606267867909
标准差1.0752069288955253
方差1.1560699399449472
### 求出具体值的下标
print("最小值所在的下标{}".format(np.argmin(stock_change)))
最小值所在的下标41

数组与数的运算

import numpy as np

arr = np.array([[1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8]])

print(arr)
print("\n")
print(arr + 1)
print("\n")
print(arr / 2)
[[1 2 3 4 5 6 7]
 [2 3 4 5 6 7 8]]


[[2 3 4 5 6 7 8]
 [3 4 5 6 7 8 9]]


[[0.5 1.  1.5 2.  2.5 3.  3.5]
 [1.  1.5 2.  2.5 3.  3.5 4. ]]

数组与数组的运算(加法)广播机制

import numpy as np

# 对应维度相等或维度有一个为一
arr1 = np.array([[1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8]])
arr2 = np.array([[2], [4]])
arr1+arr2
array([[ 3,  4,  5,  6,  7,  8,  9],
       [ 6,  7,  8,  9, 10, 11, 12]])

矩阵的运算

矩阵乘法

a = np.array([[1, 2, 3], [2, 3, 4]])
b = np.array([[1, 2], [2, 3], [3, 4]])

# 必须是两个矩阵不能是常数
np.matmul(a, b)
array([[14, 20],
       [20, 29]])
c = np.array([[1, 2, 3], [2, 3, 4]])
num1 = 2
num2 = 3
# 可以有一个是常数
print(np.dot(c, num1))
# 也可以都是常数
print(np.dot(num1, num2))
[[2 4 6]
 [4 6 8]]
6
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值