Numpy Notes

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 : 表示范数类型
    Numpy Notes_第1张图片

x

  • num
  • matrix
  • tensor

ord

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

axis

Numpy Notes_第2张图片

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 Notes_第3张图片

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

Numpy Notes_第4张图片

Return在这里插入图片描述

For example

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

Numpy Notes_第5张图片

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()

Numpy Notes_第6张图片

你可能感兴趣的:(Python,numpy,算法)