Numpy Notes

本文介绍了NumPy的几个常用函数。np.linalg.norm()用于求矩阵或向量范数,可根据ord参数返回不同结果;np.clip()用于限定数组值的范围;numpy.arange()可在给定区间内生成均匀间隔的值;numpy.arccos()是余弦的反函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

np.linalg.norm()

Matrix or vector norm
return one of eight different matrix norms, or one of an infinite number of vector norms (described below), depending on the value of the ord parameter.

  • np.linalg.norm() 用于求范数
  • linalg : linear(线性) + algebra(代数)
  • norm表示范数
np.linalg.norm(x, ord=None, axis=None, keepdims=False)

Param

  • x : 表示矩阵(一维数据也是可以的~)
  • ord : 表示范数类型
    在这里插入图片描述

x

  • num
  • matrix
  • tensor

ord

  • ord=1:表示求列和的最大值
  • ord=2:|λE-ATA|=0,求特征值,然后求最大特征值得算术平方根
  • ord=∞:表示求行和的最大值
  • ord=None:表示求整体的矩阵元素平方和,再开根号

axis

![在这里插入图片描述](https://img-blog.csdnimg.cn/e7e55088a74643f9bf5105c5b08b9589.png

keepdims

表示是否保持矩阵的二位特性

  • 默认为False
  • True : 保持
  • False : 不保持

np.clip()

Clip (limit) the values in an array.

numpy.clip(a, a_min, a_max, out=None)

Param

  • a : 输入的数组
  • a_min: 限定的最小值 也可以是数组 如果为数组时 shape必须和a一样
  • a_max:限定的最大值 也可以是数组 shape和a一样
  • out:剪裁后的数组存入的数组

if a [ ] < a_min ----> a_min
if a [ ] > a_max----->a_max
between [a_min, a_max] ------> a [ ]

For example

>>> a = np.arange(10) # 0 1 2 3 4 5 6 7 8 9
>>> np.clip(a, 1, 8)
array([1, 1, 2, 3, 4, 5, 6, 7, 8, 8]) # a被限制在1-8之间
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # 没改变a的原值

>>> np.clip(a, 3, 6, out=a) # 修剪后的数组存入到a中
array([3, 3, 3, 3, 4, 5, 6, 6, 6, 6])

>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.clip(a, [3,4,1,1,1,4,4,4,4,4], 8)
# 当a_min为数组时, a中每个元素和都和a_min中对应元素比较
# 0 < 3 -->小于最小值 则等于3
# 3 > 2 -->大于最小值 则等于本身 再和最大值比 没超过最大值 所以为3
array([3, 4, 2, 3, 4, 5, 6, 7, 8, 8])

numpy.arange()

Return evenly spaced values within a given interval

numpy.arange([start, ]stop, [step, ]dtype=None, *, like=None)

Param

  • arange(stop) : Values are generated within the half-open interval [0, stop) (in other words, the interval including start but excluding stop).
  • arange(start, stop) : Values are generated within the half-open interval [start, stop).
  • arange(start, stop, step) : Values are generated within the half-open interval [start, stop), with spacing between values given by step.

在这里插入图片描述

numpy.arccos()

The inverse of cos
在这里插入图片描述

numpy.arccos(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'arccos'>

Param

在这里插入图片描述

Return在这里插入图片描述

For example

import numpy as np
print('数组的反余弦值:{}'.format(np.arccos([1, -1])))

在这里插入图片描述

import matplotlib.pyplot as plt
x = np.linspace(-1, 1, num=100)
plt.plot(x, np.arccos(x), c='b')
plt.axis('tight')
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cmy_CTO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值