当前位置: 首页 > web >正文

np的一些函数用法

总结了一些用到的np函数,希望可以帮到大家

np.arange()

     函数返回一个有终点和起点的固定步长的排列,如[1,2,3,4,5],起点是1,终点是6,步长为1。
参数个数情况: np.arange()函数分为一个参数,两个参数,三个参数三种情况
1)一个参数时,参数值为终点,起点取默认值0,步长取默认值1。
2)两个参数时,第一个参数为起点,第二个参数为终点,步长取默认值1。
3)三个参数时,第一个参数为起点,第二个参数为终点,第三个参数为步长。其中步长支持小数


np.max()和np.maximum()的区别

1.np.max(a, axis=None, out=None, keepdims=False)

  求序列的最值

最少接受一个参数

axis默认为axis=0即列向,如果axis=1即横向

ex:

np.max([-2, -1, 0, 1, 2])

2.np.maximum(X, Y, out=None)

X和Y逐位进行比较,选择最大值.

最少接受两个参数

ex:

np.maximum([-3, -2, 0, 1, 2], 0)
  • array([0, 0, 0, 1, 2])


np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

    在规定的时间内,返回固定间隔的数据。他将返回“num”个等间距的样本,在区间[start, stop]中。其中,区间的结束端点可以被排除在外。也就是生成等间距数组。

import numpy as np
print(np.linspace(1,100,num=10,endpoint=True,retstep=True,dtype=np.bool))
#结果是(array([ True,  True,  True,  True,  True,  True,  True,  True,  True,True]), 11.0)

np.random.uniform(0,5)函数

  • 函数原型: numpy.random.uniform(low,high,size)

    功能:从一个均匀分布[low,high)中随机采样,注意定义域是左闭右开,即包含low,不包含high.

  • 参数介绍:

    low: 采样下界,float类型,默认值为0;
    high: 采样上界,float类型,默认值为1;
    size: 输出样本数目,为int或元组(tuple)类型,例如,size=(m,n,k), 则输出 m * n * k 个样本,缺省时输出1个值。

    返回值:ndarray类型,其形状和参数size中描述一致


np.concatenate()函数

是numpy中对array进行拼接的函数,使用方法如下所示:

import numpy as npx1 = np.random.normal(1,1,(5,4))
x2 = np.random.normal(1,1,(3,4))print(x1)
print(x1.shape)
print(x2)
print(x2.shape)con = np.concatenate([x1,x2],axis=0)
print(con)
print(con.shape)输出结果为:
[[ 2.22806658  0.15277615  2.21245262  1.63831116][ 1.30131232 -1.09226289 -0.65959394  1.16066688][ 1.52737722  0.84587186  1.53041503  0.4584277 ][ 1.56096219  1.29506244  3.08048523  2.06008988][ 1.79964236  0.95087117  1.30845477 -0.2644263 ]]
(5, 4)
[[0.89383392 1.49502055 2.90571116 1.71943997][1.44451535 1.87838383 1.4763242  0.82597179][0.72629108 1.42406398 1.35519112 0.58121617]]
(3, 4)
[[ 2.22806658  0.15277615  2.21245262  1.63831116][ 1.30131232 -1.09226289 -0.65959394  1.16066688][ 1.52737722  0.84587186  1.53041503  0.4584277 ][ 1.56096219  1.29506244  3.08048523  2.06008988][ 1.79964236  0.95087117  1.30845477 -0.2644263 ][ 0.89383392  1.49502055  2.90571116  1.71943997][ 1.44451535  1.87838383  1.4763242   0.82597179][ 0.72629108  1.42406398  1.35519112  0.58121617]]
(8, 4)

np.zeros()函数

作用:np.zeros()函数返回一个元素全为0且给定形状和类型的数组:
zeros(shape, dtype=float, order=‘C’)
1.shape:形状
2.dtype:数据类型,可选参数,默认numpy.float64
3.order:可选参数,c代表与c语言类似,行优先;F代表列优先
 

print(np.zeros((2,5),dtype= np.int))

运行结果:
一个2行5列的矩阵


np.min()和np.argmin()函数用法

np.min()函数用于返回列表中的最小值
np.argmin()函数用于返回一维列表最小值索引或多维列表展平之后的最小值索引

import numpy as np
lst1=[1,100,56,78,0]
lst2=[[100,4,5],[3,5,7],[5,0,6]]
print("lst列表中的最小值是:")
print(np.min(lst1))
print("lst1列表中最小值的索引是:")
print(np.argmin(lst1))
print("lst2列表中最小值的索引是:")
print(np.argmin(lst2))

输出:

lst列表中的最小值是:
0
lst1列表中最小值的索引是:
4
lst2列表中最小值的索引是:
7

np.sum()函数

原文链接:https://blog.csdn.net/rifengxxc/article/details/75008427


np.exp(B) : 求e的幂次方

np.sqrt(B):求B的开方(算数平方根)


np.arctan()函数

求反三角函数


np.rad2deg()函数和np.deg2rad()函数

np.rad2deg()把弧度值转换为角度值

np.deg2rad()把角度值转换为弧度值


np.random.randn()函数

    语法:np.random.randn(d0,d1,d2……dn)
    1. 当函数括号内没有参数时,则返回一个浮点数;
    2. 当函数括号内有一个参数时,则返回秩为1的数组,不能表示向量和矩阵;
    3. 当函数括号内有两个及以上参数时,则返回对应维度的数组,能表示向量或矩阵;
    4. np.random.standard_normal()函数与np.random.randn()类似,但是     np.random.standard_normal()的输入参数为元组(tuple)。
    5. np.random.randn()的输入通常为整数,但是如果为浮点数,则会自动直接截断转换为整数。
    作用:通过本函数可以返回一个或一组服从标准正态分布的随机样本值。
    特点:标准正态分布是以0为均数、以1为标准差的正态分布,记为N(0,1)。
 


numpy.floor()函数

np.floor()返回不大于输入参数的最大整数。(向下取整)


np.array()函数

转换为数组


np.random.seed()函数

用于生成指定随机数。

seed()被设置了之后,np,random.random()可以按顺序产生一组固定的数组,如果使用相同的seed()值,则每次生成的随机数都相同,如果不设置这个值,那么每次生成的随机数不同。但是,只在调用的时候seed()一下并不能使生成的随机数相同,需要每次调用都seed()一下,表示种子相同,从而生成的随机数相同。


np.tile(A,B)函数

重复A,B次,这里的B可以时int类型也可以是远组类型。

>>> import numpy
>>> numpy.tile([0,0],5)#在列方向上重复[0,0]5次,默认行1次
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> numpy.tile([0,0],(1,1))#在列方向上重复[0,0]1次,行1次
array([[0, 0]])
>>> numpy.tile([0,0],(2,1))#在列方向上重复[0,0]1次,行2次
array([[0, 0],[0, 0]])
>>> numpy.tile([0,0],(3,1))
array([[0, 0],[0, 0],[0, 0]])
>>> numpy.tile([0,0],(1,3))#在列方向上重复[0,0]3次,行1次
array([[0, 0, 0, 0, 0, 0]])
>>> numpy.tile([0,0],(2,3))<span style="font-family: Arial, Helvetica, sans-serif;">#在列方向上重复[0,0]3次,行2次</span>
array([[0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0]])

np.repeat()函数

重复相关的词义

np.repeat(1, 5)
array([1, 1, 1, 1, 1])
# 1 重复5次
x = np.array([[1,2],[3,4]])
np.repeat(x, 2)
array([1, 1, 2, 2, 3, 3, 4, 4])
# 合并维度
np.repeat(x, 3, axis=1)
array([[1, 1, 1, 2, 2, 2],
[3, 3, 3, 4, 4, 4]])
# 将重复操作施加到 维度‘axis=1’上,相当于‘增加列数’
np.repeat(x, 3, axis=1)
array([[1, 1, 1, 2, 2, 2],
[3, 3, 3, 4, 4, 4]])
# 将重复操作施加到 维度‘axis=1’上,相当于‘增加列数’

np.squeeze()

作用:从数组的形状中删除单维条目,即把shape中为1的维度去掉

例子:

import numpy as npa = np.array([[1], [2], [3]])
print(a)
print(a.shape)

输出:

[[1]
 [2]
 [3]]

应用squeeze()后:

a1 = np.squeeze(a)
print(a1)
print(a1.shape)

[1 2 3]
shape:(3,)

应用:

在预测分析中用于处理预测数组和真实值数组以方便计算预测值与真实值之间的误差:

predictions = np.array(predictions).squeeze() 
labels = np.array(labels).squeeze() 
rmse = np.sqrt(((predictions - labels) ** 2).mean(axis=O)) 

np.load()

       在读取文件时,我们有时候会需要读取npy文件,有时候这样一个文件特别大读取起来非常慢,在读取文件的时候只需要添加上读取方式,那么就会使用给定模式对文件进行内存映射,内存映射的矩阵保留在磁盘上,并不是直接全部读取到内存里。

def load_data(filename,filepath):x=np.load(filepath+filename,mmap_mode = 'r')return x

http://www.xdnf.cn/news/11230.html

相关文章:

  • 软件开发管理规范(制度)
  • 电商新趋势来临!?解析Dtop 环球嘉年华电商是否值得加入!
  • 懒人网址导航源码v3.9源码及教程
  • SSM-OA自动化办公系统wljr4(程序+源码+数据库+调试部署+开发环境)
  • 指针与引用
  • superior哥AI系列第6期:Transformer注意力机制:AI界的“注意力革命“
  • 百度网盘国际版 Dubox 体验:干净好用,却与国内用户无缘
  • ubuntu安装rabbitvcs
  • 如何快速的学习单片机,学习单片机的路线是怎样的??末尾附赠教程+项目资料包
  • 软件研发管理经验总结 - 技术管理
  • IIS 服务器,下载APK 文件,用于发布更新最新的APK包
  • layui框架——弹出层layer
  • JavaScriptAPI编程_History对象(获取浏览历史)
  • 插件与控件的区别
  • Linux多线程(2)-线程间同步的5种方式,一次性说清楚!
  • VMware虚拟机Windows 10安装使用教程(非常详细)从零基础入门到精通,看完这一篇就够了
  • 【Maven入门篇】(3)依赖配置,依赖传递,依赖范围,生命周期
  • 软件版本号扫盲——Beta RC Preview release等
  • latex如何输入正确的 双引号
  • WinForm中常用控件
  • C#中CheckListBox的用法
  • 搭建Serv-U FTP服务器共享文件外网远程访问「无公网IP」
  • 使用CImage类
  • Linux系统 虚拟机安装教程_虚拟机安装linux系统
  • 镜头选型——景深计算
  • 86年版五笔和98年版五笔区别
  • C语言从入门到精通保姆级教程(2021版上)
  • Response.AddHeader使用实例
  • functionexists php,PHP 检测函数是否被定义 function_exists 函数
  • [转载] Rss 与 Feed 的概念区别