Numpy笔记:安装Numpy+ndarray基本属性+常用方法+索引和切片+广播+轴+范数

Numpy
Python库,用于数组快速操作的各种API

  • 支持常见的数组和矩阵操作
  • ndarray处理多维数组

安装Numpy

  1. 检查PyCharm的Python运行环境
    • File–>Settings–>Project–>Python Interpreter
    • 检查Python Interpreter环境,例如base
  2. 点击Anaconda Prompt
  3. 切换运行环境到PyCharm设置的运行环境
    • 默认是在base环境,无需切换环境
    • 可输入命令行conda env list检查当前环境
    • 输入命令行conda activate 环境名切换运行环境
  4. 输入以下命令行安装numpy
    • pip install numpy==1.21.6 -i https://mirrors.aliyun.com/pypi/simple/

numpy==1.21.6 需要 Requires-Python >=3.7,❤️.11

  1. 安装成功后,可输入命令行pip list检查安装包

N维数组-ndarray

内存分配

  1. ndarray存储数据,数据与数据的地址是连续的,操作更快
  2. python原生列表是通过寻址查询元素

效率对比

"""
原生数组与numpy数组操作效率对比
"""
import time

import numpy as np

a = [num for num in range(10000000)]

t1 = time.time()
sum_a = sum(a)
t2 = time.time()
print(t2 - t1)
# 4.231211423873901

b = np.array(a)
t3 = time.time()
sum_b = np.sum(b)
t4 = time.time()
print(t4 - t3)
# 0.06466150283813477

ndarray属性

  • 数组维度的元组
    • ndarray.shape
  • 数组元素的类型
    • ndarray.dtype
      • 默认是int32
  • 数组中元素数量
    • ndarray.size
  • 数组维数
    • ndarray.ndim

ndarray.shape

可修改shape属性
shape(一维元素个数, 二维元素个数, 三维元素个数, …)
注意:修改的属性需要根据元素个数设定,如果不计算元素个数,可设定-1,会自动计算当前维度的元素个数(只能设定一个-1)
直接赋值修改

"""
np.array([...]).shape

"""
import numpy as np

a = np.array([
    [1, 2, 3],
    [4, 5, 6]
])
# print(a)
# [[1 2 3]
#  [4 5 6]]
a.shape = (3, 2)
# print(a)
# [[1 2]
#  [3 4]
#  [5 6]]

ndarray.arange()

自动生成数组,参数和python内置函数range类似
arange(起始值, 结束值, 步长)

import numpy as np

# print(np.arange(0, 9))
# [0 1 2 3 4 5 6 7 8]
print(np.arange(0, 10, 2))
# [0 2 4 6 8]

ndarray.reshape()

参数为维度元组
reshape((一维元素个数, 二维元素个数, 三维元素个数, …))

import numpy as np


# print(np.arange(0, 9).reshape((3, -1)))
# [[0 1 2]
#  [3 4 5]
#  [6 7 8]]
# print(np.arange(8).reshape(2, 4))
# [[0 1 2 3]
#  [4 5 6 7]]
# print(np.arange(1000).reshape(5, 5, 5, -1))
# [[[[  0   1   2   3   4   5   6   7]
#    [  8   9  10  11  12  13  14  15]
#    [ 16  17  18  19  20  21  22  23]
#    [ 24  25  26  27  28  29  30  31]
#    [ 32  33  34  35  36  37  38  39]]
#    ...]]
print(np.arange(9).reshape(3, -1, 3))
# [[[0 1 2]]
#
#  [[3 4 5]]
#
#  [[6 7 8]]]

ndarray.dtype

np.int32
np.uint8 无符号整数 0-255
np.object python对象

import numpy as np

# a = np.arange(1, 7).reshape(2, 3)
# a = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float_)
# print(a.dtype)
# float64

# a = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.str_)
# print(a)
# print(a.dtype)
# [['1' '2' '3']
#  ['4' '5' '6']]
# 

# a = np.array([[1, 2, 3], [4, 5, '100']], dtype=np.uint8)
# print(a)
# print(a.dtype)
# [[  1   2   3]
#  [  4   5 100]]
# uint8

# arr = np.array(['python', 'tensorflow', 'scikit-learn', 'numpy'], dtype=np.string_)
# print(arr)
# print(arr.dtype)
# [b'python' b'tensorflow' b'scikit-learn' b'numpy']
# |S12
# 12表示最长字符串元素的个数

a = np.array([[1, 2, 3], [4, 5, 'Anna']], dtype=np.str_)
print(a)
print(a.dtype)
# [['1' '2' '3']
#  ['4' '5' 'Anna']]
# 

ndarray常用方法

  1. array
  2. asarray
  3. zeros
  4. zeros_like
  5. ones
  6. ones_like
  7. full
  8. full_like
  9. reshape
  10. argmax
  11. argmin
  12. argsort
  13. where
  14. where(bool)
  15. where(bool, x, y)
  16. mean
  17. squeeze
  18. expand_dims
  19. copy
  20. nonzero
  21. count_nonzero
  22. concatenate
import numpy as np

# print(np.array([1, 2, 3], ndmin=3))
# print(np.array([1, 2, 3, 4], ndmin=3))
# print(np.array([1, 2, 3, 4, 5], ndmin=3))
# [[[1 2 3]]]
# [[[1 2 3 4]]]
# [[[1 2 3 4 5]]]

# print(np.array([1, 2, 3], dtype="f4"))
# [1. 2. 3.]

# print(np.zeros(shape=[5, 5], dtype="i4"))
# [[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]]

# print(np.ones(shape=[5, 5], dtype="i4"))
# [[1 1 1 1 1]
#  [1 1 1 1 1]
#  [1 1 1 1 1]
#  [1 1 1 1 1]
#  [1 1 1 1 1]]

# print(np.nonzero(np.array([1, 0, 2, 3, 0, 4])))
# (array([0, 2, 3, 5], dtype=int64),)

# print(np.full(shape=[5, 5], fill_value=1.5, dtype=np.float_))
# [[1.5 1.5 1.5 1.5 1.5]
#  [1.5 1.5 1.5 1.5 1.5]
#  [1.5 1.5 1.5 1.5 1.5]
#  [1.5 1.5 1.5 1.5 1.5]
#  [1.5 1.5 1.5 1.5 1.5]]

# print(np.eye(3))
# [[1. 0. 0.]
#  [0. 1. 0.]
#  [0. 0. 1.]]

# print(np.empty(shape=(3, 3)))
# [[0.00000000e+000 0.00000000e+000 0.00000000e+000]
#  [0.00000000e+000 0.00000000e+000 1.48219694e-321]
#  [1.11261027e-306 7.23149004e-308 9.34531021e-307]]

# print(np.linspace(1, 10, 5))
# [ 1.    3.25  5.5   7.75 10.  ]

# print(np.random.random((3, 3)))
# [[0.96535887 0.84381189 0.00743776]
#  [0.12660224 0.13720431 0.61864365]
#  [0.7851438  0.0894568  0.3641283 ]]

# print(np.random.randint(0, 10, (3, 3)))
# [[2 1 2]
#  [6 2 3]
#  [6 6 9]]

# print(np.random.normal(0, 1, (3, 3)))
# [[ 0.35513043  0.6897101  -0.27432646]
#  [-0.44496008  0.401572   -0.41364899]
#  [-0.78072351 -0.9713653   0.49036246]]

# numpy数组中元素的字节大小
# print(np.array([1, 2, 3]).itemsize)
# 4
# print(np.array([1., 2., 3.]).itemsize)
# 8
import numpy as np

a = np.arange(6).reshape(2, 3)
# print(a)
# [[0 1 2]
#  [3 4 5]]
# print(a.T)
# [[0 3]
#  [1 4]
#  [2 5]]

# nums = np.arange(20, 30)
# idxs = np.where(nums > 25)
# print(nums)
# [20 21 22 23 24 25 26 27 28 29]
# print(idxs)
# (array([6, 7, 8, 9], dtype=int64),)
# print(nums[idxs])
# [26 27 28 29]

# idxs = np.where(a < 3, 1, -1)
# print(idxs)

data = np.ones_like(a)
print(data)
# [[1 1 1]
#  [1 1 1]]
import numpy as np


# a = np.arange(1, 7).reshape(3, 2)

# a = np.array([1, 2, 3, 4])
# b = np.array([5, 6, 7, 8])
# print(np.dot(a, b))
# 1*5 + 2*6 + 3*7 + 4*8
# 70

# a = np.array([
#     [1, 2, 3],
#     [0, 5, 6],
#     [7, 8, 9]
# ])
# # a数组,输出0轴对比得出的最小值
# print(np.amin(a, axis=0))
# # [0 2 3]
# print(np.amax(a, axis=1))
# # [3 6 9]

# x = np.arange(9).reshape(3, 3)
# # 按主对角线转换元素
# print(x.transpose())

# a = np.arange(1, 10, dtype=np.int8).reshape(3, 3)
# b = np.array([1, 1, 1])
# print(a)
# [[1 2 3]
#  [4 5 6]
#  [7 8 9]]
# print(b)
# [1 1 1]

# print(np.add(a, b))
# print(np.subtract(a, b))
# print(np.multiply(a, b))
# print(np.divide(a, b))
# print(np.mod(a, b))
# print(np.power(a, b))
# e的1次方
# print(np.exp(1))

# print(np.min([1, 2, 3]))
# 1
# print(np.max([[1, 7, 3], [4, 5, 2]], axis=0))
# [4 7 3]
# print(np.mean([1, 2, 3]))
# 2.0

# 返回元素最大值的索引
# print(np.argmax([1, 2, 3, 0, 1]))
# 2
# 返回元素最小值的索引
# print(np.argmin([1, 2, 3, 0, 1]))
# 3

# 减少维度为1的维度
# print(np.squeeze([[1], [2], [3]]))
# [1 2 3]

# 拼接数组元素为一维数组
# print(np.hstack(([1, 2, 3], [4, 5, 6])))
# [1 2 3 4 5 6]
# print(np.hstack(([1, 2, 3], [4, 5, 6], [4, 5, 6])))
# [1 2 3 4 5 6 4 5 6]

# 拼接数组为二维数组
# print(np.vstack(([1, 2, 3], [4, 5, 6])))
# [[1 2 3]
#  [4 5 6]]
# print(np.vstack(([1, 2, 3], [4, 5, 6], [4, 5, 6])))
# [[1 2 3]
#  [4 5 6]
#  [4 5 6]]

# 获得非零元素的索引
# print(np.nonzero([1, 2, 3, 0, 1]))
# (array([0, 1, 2, 4], dtype=int64),)
# 非零元素计数
# print(np.count_nonzero([1, 2, 3, 0, 1]))
# 4

# print(np.abs([-1, -2, -3, 0, -1]))
# [1 2 3 0 1]

# a = np.array([1, 2, 3])
# # 使用 expand_dims 在第一个维度插入尺寸为1的新维度
# b = np.expand_dims(a, axis=0)
# print("原数组形状:", a.shape)
# print(a)
# # [1 2 3]
# print("插入尺寸为1的新维度后的形状:", b.shape)
# print(b)
# # [[1 2 3]]

迭代数组

nditer

  • order属性,F为优先列遍历,K为优先行遍历,默认为K
import numpy as np

a = np.arange(1, 10).reshape(3, 3)
# print(a)
for num in np.nditer(a, order='F'):
    print(num, end=' ')
print()
# 1 4 7 2 5 8 3 6 9

列表拷贝

拷贝数组

  • 深拷贝

    • copy.deepcopy
    • np.copy
  • 浅拷贝

    • copy.copy
    • 拷贝对象是简单子对象,则拷贝数据不影响原数据

复杂子对象:列表、对象等

import copy

import numpy as np

nums = [
    [1, 2, 3],
    [4, 5, 6]
]

# copyNums = []
# for line in nums:
#     subNums = []
#     for num in line:
#         subNums.append(num)
#     copyNums.append(subNums)
# # print(copyNums)
# # [[1, 2, 3], [4, 5, 6]]
# copyNums[0][0] = 99
# print(nums)
# # [[1, 2, 3], [4, 5, 6]]
# print(copyNums)
# # [[99, 2, 3], [4, 5, 6]]

copyNums = copy.deepcopy(nums)
print(copyNums)
# [[1, 2, 3], [4, 5, 6]]
print(id(copyNums))
# 1908804890048
print(id(nums))
# 1908804935552

data = np.array([1, 2, 3])
data_copy = data.copy()
data[1] = 99
print(id(data))
print(id(data_copy))
# 1404926945392
# 1404924359760
print(data)
print(data_copy)
# [ 1 99  3]
# [1 2 3]

复制数组

view

  • 新开内存存储原数组数据,但修改原数据后,view复制的数据同样改变
"""
复制数组
"""
import copy

import numpy as np

nums = np.array([1, 2, 3])
copyNums = nums.view()
nums[0] = 99
print(id(nums))
print(id(copyNums))
print(nums)
print(copyNums)
# 1828126145680
# 1828126143952
# [99  2  3]
# [99  2  3]

创建数组的方式

  1. array
  2. asarray

array和asarray的区别

  • array新开辟一个内存赋值数组
  • asarray增加一个内存引用,指向数组数据
    • 如果使用asarray创建数组时,修改dtype,则会变为深拷贝
import numpy as np

nums = np.array([
    [1, 2, 3, 1],
    [4, 5, 6, 1],
    [7, 8, 9, 1]
])
a = np.array(nums)
b = np.asarray(nums)
c = np.asarray(nums, dtype=np.float_)
print(id(nums))
# 2233696150416
print(id(a))
# 2233696113072
print(id(b))
# 2233696150416
print(id(c))
# 2234239804592

Numpy索引和切片

  1. 切片
    • 切片[起始值, 结束值, 步长]
  2. 索引
    • a[0, 1, 2]获得单个元素
    • a[0, [0, 2], [1, 0]]获得两个元素
    • b为数字类型数组,a[b]表示b数组元素为索引,获得a的对应数组元素
    • b为布尔类型数组,a[b]表示b数组元素为真时,获得a的对应数组元素
import numpy as np

a1 = np.array([1, 2, 3, 4])
# print(a1)
# print(a1[0])
# print(a1[2])
a2 = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

# print(a2[2])
# print(a2[2, 1])
# print(a2[:, 2])

a3 = np.array([
    [[10, 11, 12], [13, 14, 15], [16, 17, 18]],
    [[20, 21, 22], [23, 24, 25], [26, 27, 28]],
    [[30, 31, 32], [33, 34, 35], [36, 37, 38]]
])
# print(a3.shape)
# print(a3[2])
# print(a3[2, 0])
# print(a3[2, 0, 1])
# print(a3[:, 1, 2])
# print(a3[1, 2])
# print(a3[0, :])
# print(a3[0, :, 1])
# print(a3[:, 1])
# print(a3[:, 1, 0])
# print(a3[:, 1:3, 0])
# print(a3[:, :2, :2])
# print(a3[1:2, 1:3, 1])
# print(a3[:, ::2, 1])
# print(a3[:, [0, 2], [1, 1]])
# print(a3[:, [0, 2], [1, 0]])
import numpy as np

arr = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
# 使用整数数组索引选择元素
# indices = np.array([0, 2])
# result = arr[:, indices]
# # result = arr[:, [0, 2]]
# print(result)
# [[1 3]
#  [4 6]
#  [7 9]]

# row_indices = np.array([0, 1, 2])
# col_indices = np.array([1, 2, 0])
# result = arr[row_indices, col_indices]
# print(result)
# [2 6 7]

data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# mask1 = np.array([True, False, True, False, True, False, True, False, True], dtype=np.bool_)
# print(data[mask1])
# [1 3 5 7 9]
# index = np.array([1, 0, 1, 0, 1, 0, 1, 0, 1], dtype="i1")
# print(data[index])
# [2 1 2 1 2 1 2 1 2]
index = np.array([2, 4, 1, 0], dtype="i1")
print(data[index])
# [3 5 2 1]

Numpy广播

两个数组计算时,如果形状不同,广播机制会自动扩展为相同形状进行运算
扩展的数组必须一个维度为1,另一个维度和目标数组的一个维度相同
形状为(1,)的数组可以适应任何数组进行广播机制

import numpy as np

a = np.array([
    [1, 2, 3, 1],
    [4, 5, 6, 1],
    [7, 8, 9, 1]
])
# b = np.array([0, 1, 2, 3])
# print(a + b)
# [[ 1  3  5  4]
#  [ 4  6  8  4]
#  [ 7  9 11  4]]

# b = np.array([
#     [0],
#     [1],
#     [2]
# ])
# print(a + b)
# [[ 1  2  3  1]
#  [ 5  6  7  2]
#  [ 9 10 11  3]]

# b = np.array([
#     [2, 2, 2, 2],
# ])
# print(a * b)
# [[ 2  4  6  2]
#  [ 8 10 12  2]
#  [14 16 18  2]]

b = np.array([1000])
c = a + b
print(c)
# [[1001 1002 1003 1001]
#  [1004 1005 1006 1001]
#  [1007 1008 1009 1001]]
print(b.shape)
# (1,)
print(c.shape)
# (3, 4)

轴即数组的维度
按轴处理,即按轴的方向折叠,如数组(a, b, c)

  • 按0轴处理,即axis=0,处理后(a, b, c)–>(b, c)
  • 按1轴处理,即axis=1,处理后(a, b, c)–>(a, c)
  • 按2轴处理,即axis=2,处理后(a, b, c)–>(a, b)
import numpy as np

a = np.array([
    [1, 2, 3, 1],
    [4, 5, 6, 1],
    [7, 8, 9, 1]
])
print(np.sum(a, axis=0))
# [12 15 18  3]
print(np.sum(a, axis=1))
# [ 7 16 25]

范数

衡量向量或矩阵的长度、大小或距离的方法,例如np.linalg.norm(v, ord=2)

  • ord=2,表示对平方和开根
  • ord=1,表示每个元素的绝对值求和
  • ord=np.inf,表示所有元素绝对值的最大值
import numpy as np

v = np.array([3, 4])

l2_norm = np.linalg.norm(v, ord=2)
print('L2范数:', l2_norm)
# L2范数: 5.0

l1_norm = np.linalg.norm(v, ord=1)
print('L1范数:', l1_norm)
# L1范数: 7.0

linf_norm = np.linalg.norm(v, ord=np.inf)
print('Linf范数:', linf_norm)
# Linf范数: 4.0

你可能感兴趣的:(Python,numpy,笔记,python,学习)