专业编程基础技术教程

网站首页 > 基础教程 正文

python数据分析numpy基础之sqrt用法和示例

ccvgpt 2024-11-04 12:23:19 基础教程 8 ℃

1 python数据分析numpy基础之sqrt用法和示例

python的numpy库的sqrt()函数用于计算数组各元素的平方根,相当于arr**0.5。

用法

python数据分析numpy基础之sqrt用法和示例

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

描述

numpy.sqrt()对数组的每个元素计算平方根,返回每个元素的非负平方根,即如果是负数和复数,则返回元素的复数。

入参

x:必选,array-like

需计算平方根的数组,可以是ndarray或类ndarray(比如元组、列表等)或标量;

out:可选,ndarray

存储平方根结果的数组。若提供,则需为ndarray,out元素类型必须与返回值一致,若未提供,返回新的ndarray;

where:可选,bool

表示是取哪的结果作为出参,默认为True,表示sqrt平方根运算结果作为出参;若为False,若传了out则结果取out的值,若未传out则结果取最近一次的out的值,最近一次out若为out=None则取随机值;

出参

返回x的浮点型绝对值,x是array-lie则返回ndarray,若为标量则返回标量。

1.1 入参x

np.sqrt()的入参x为必选入参,表示需计算平方根的ndarray或元组或列表或标量。标量或元素值为非负数,返回浮动数;为负数返回nan;含复数则全部返回复数。

 >>> import numpy as np
 # np.sqrt()计算正整数的平方根,标量,返回浮点数
 >>> np.sqrt(9)
 3.0
 # np.sqrt()计算负整数的平方根,标量,返回nan,第一次会警告
 >>> np.sqrt(-9)
 Warning (from warnings module):
   File "<pyshell#3>", line 1
 RuntimeWarning: invalid value encountered in sqrt
 nan
 # np.sqrt()计算负数,第二次不会警告
 >>> np.sqrt(-10)
 nan
 # np.sqrt()计算元组的平方根,返回ndarray
 >>> np.sqrt((0,1,2))
 array([0.        , 1.        , 1.41421356])
 # np.sqrt()计算列表的平方根,返回ndarray
 >>> np.sqrt([0,1,2])
 array([0.        , 1.        , 1.41421356])
 # np.sqrt()计算列表的平方根,返回ndarray
 >>> np.sqrt(np.array([0,1,2]))
 array([0.        , 1.        , 1.41421356])
 # np.sqrt()计算负数列表的平方根,返回值为nan的ndarray
 >>> np.sqrt([-1,-2,-4])
 array([nan, nan, nan])
 # 元素有复数,则全部元素的平方根转为复数
 >>> np.sqrt([9,-9,-3+4j])
 array([3.+0.j, 0.+3.j, 1.+2.j])

1.2 入参out

np.sqrt()的入参out为可选入参,表示存放平方根结果的ndarray,若提供,则需为ndarray,out元素类型必须与返回值类型一致,若未提供,返回新的ndarray;如果是标量用sqrt(),则out的每个元素都赋值为标量的平方根。

 >>> import numpy as np
 >>> ar1=np.array([1,2,3])
 >>> ar2=np.zeros(3)
 >>> ar3=np.array([1,2,3],dtype=complex)
 >>> a=1
 # np.sqrt()的out(ar2)存放平方根
 >>> np.sqrt(ar1,out=ar2)
 array([1.        , 1.41421356, 1.73205081])
 >>> ar2
 array([1.        , 1.41421356, 1.73205081])
 # ar2的每个元素被赋值为标量的平方根
 >>> np.sqrt(9,ar2)
 array([3., 3., 3.])
 # np.sqrt()的out和x都为复数
 >>> np.sqrt(ar3,out=ar3)
 array([1.        +0.j, 1.41421356+0.j, 1.73205081+0.j])
 # np.sqrt()的out必须为ndarray,否则报错
 >>> np.sqrt(ar1,a)
 Traceback (most recent call last):
   File "<pyshell#18>", line 1, in <module>
     np.sqrt(ar1,a)
 TypeError: return arrays must be of ArrayType
 # np.sqrt()的out必须与x相同类型,否则报错
 >>> np.sqrt(ar3,out=ar2)
 Traceback (most recent call last):
   File "<pyshell#13>", line 1, in <module>
     np.sqrt(ar3,out=ar2)
 numpy.core._exceptions._UFuncOutputCastingError: Cannot cast ufunc 'sqrt' output from dtype('complex128') to dtype('float64') with casting rule 'same_kind'

1.3 入参where

np.sqrt()的入参where为可选入参,表示是取哪的结果作为出参,默认为True表示取绝对值结果为出参,若为False却指定out则取out为出参,未指定out则取随机值。

 >>> import numpy as np
 >>> list1=[1,2,3]
 >>> ar1=np.array([4,5,6.8])
 # 未指定out,where=False取随机值
 >>> np.sqrt(list1,where=False)
 array([0., 0., 0.])
 # 未指定out,where=True取平方根作为出参
 >>> np.sqrt(list1,where=True)
 array([1.        , 1.41421356, 1.73205081])
 # 指定out,where=False取out作为出参
 >>> np.sqrt(list1,out=ar1,where=False)
 array([4. , 5. , 6.8])

2 END

本文首发微信公众号:梯阅线条

更多内容参考python知识分享或软件测试开发目录。

Tags:

最近发表
标签列表