numpy函数库中一些常用函数的记录
##numpy函数库中一些常用函数的记录
最近才开始接触Python,python中为我们提供了大量的库,不太熟悉,因此在《机器学习实战》的学习中,对遇到的一些函数的用法进行记录。
(1)mat( )
numpy函数库中存在两种不同的数据类型(矩阵matrix和数组array),都可以用于处理行列表示的数字元素。虽然他们看起来很相似,但是在这两个数据类型上执行相同的数学运算可以得到不同的结果,其中numpy函数库中matrix与MATLAB中matrices等价。
调用mat( )函数可以将数组转化为矩阵。例如
random.rand(3,3)#构造一个3*3的随机数组
- 1
- 1
mat(random.rand(3,3))#将3*3的随机数组转化为一个3*3的矩阵
- 1
- 1
(2)shape( )
shape函数是numpy.core.fromnumeric中的函数,它的功能是读取矩阵的长度,比如shape[0]就是读取矩阵第一维度的长度。它的输入参数可以使一个整数表示维度,也可以是一个矩阵。例子如下:
matDemo=mat(random.rand(3,5))
matDemo.shape[0]#获取矩阵第一维的长度,输入参数是一个整数表示维度
matDemo.shape[1]#获取矩阵第二维的长度,
shape(matDemo) #获取矩阵的各个维度的大小,输入参数是一个矩阵
shape(matDemo)[i]#这样写也可以获取矩阵的第i个维度的大小
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
(3)random.uniform( )函数
功能:uniform(x,y) 方法将随机生成下一个实数,它在[x,y]范围内。
参数说明:
x – 随机数的最小值界。
y – 随机数的最大值界。
返回值:返回一个浮点数。
用法如下:
from numpy import * #或者直接 import random也可以
random.uniform(x,y)
- 1
- 2
- 1
- 2
注意:uniform()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。
(4)means( )方法
means( )方法为求平均值的方法
numpy.mean(a, axis=None, dtype=None, out=None, skipna=False, keepdims=False)
axis=None时计算数组中的所有值的平均值
axis=0时以列为单位计算数组的每列的所有值的平均值
axis=1时计算数组的每行为单位的所有事的平均值
dtype为指定数组中元素的类型,默认为float64
numpy.mean
numpy.mean(a, axis=None, dtype=None, out=None, skipna=False, keepdims=False)
Compute the arithmetic mean along the specified axis.
Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis. float64 intermediate and return values are used for integer inputs.
Parameters :
a : array_like
Array containing numbers whose mean is desired. If a is not an array, a conversion is attempted.
axis : int, optional
Axis along which the means are computed. The default is to compute the mean of the flattened array.
dtype : data-type, optional
Type to use in computing the mean. For integer inputs, the default is float64; for floating point inputs, it is the same as the input dtype.
out : ndarray, optional
Alternate output array in which to place the result. The default is None; if provided, it must have the same shape as the expected output, but the type will be cast if necessary. See doc.ufuncs for details.
skipna : bool, optional
If this is set to True, skips any NA values during calculation instead of propagating them.
keepdims : bool, optional
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original arr.
Returns :
m : ndarray, see dtype parameter above
If out=None, returns a new array containing the mean values, otherwise a reference to the output array is returned.
See also
average
Weighted average
Notes
The arithmetic mean is the sum of the elements along the axis divided by the number of elements.
Note that for floating-point input, the mean is computed using the same precision the input has. Depending on the input data, this can cause the results to be inaccurate, especially for float32 (see example below). Specifying a higher-precision accumulator using the dtype keyword can alleviate this issue.
例子如下:
(5)、tile( )方法
tile函数位于python模块 numpy.lib.shape_base中,他的功能是重复某个数组。比如tile(A,reps),功能是将数组A重复n次,构成一个新的数组,
在tile(A,reps)中
A的类型众多,几乎所有类型都可以:array, list, tuple, dict, matrix以及基本数据类型int, string, float以及bool类型。
reps的类型也很多,可以是tuple,list, dict, array, int,bool.但不可以是float, string, matrix类型。
先来看一些例子,然后我们就可以清楚的感受到这个函数到底是干什么的了。
第一类情况:reps为一个整数,A为一个int、tuple、dict等
第二类情况:reps为一个简单的list,A为一个int、tuple、dict等
总结
从上面可以看出,假定A的维度为d,reps的长度为len
A=[[1,2],[2,3]]的维度为2
reps=[2,3]长度为2
reps=2 长度为1
(1)当d小于len时,tile(A,reps)就是将A中所有元素作为单元,变成一个reps.n1*reps.n2 *…. *reps.nd的新数组,其中reps.n2为reps中的第2个数
为便于说明,举一个例子
假设A为一个二维数组
a=array([[1,2],[2,3]]),
reps为一个tuple:reps=[2,3];
tile(A,reps)的含义就是将A中所有元素作为单元,变成一个2*3的新数组。
(2)当d>=len时,将reps长度补足为d,即在reps前面加上d-len个1。tile(A,reps)就是将A中所有元素作为单元,变成一个1*1…reps.n1 reps.n2 …reps.nd
例如
a=array([[1,2],[2,3]])
tile(a,2)与tile(a,(1,2))是一样的。
(6)、argsort( )方法
python中的排序问题
numpy包中的argsort函数是对一个数组进行升序排列。
a=[0,1,3,2]
s=argsort(a)
截图如下:
结果返回的就是a中所有元素排序后各个元素在a中之前的下标,简单来说就是返回的是下标,而不是值,我们需要通过索引才能获取到相应的值.
上面是将数组或者是tuple等按升序排列,那么你肯定会问,降序排序应该用哪个函数来实现呢,或者说怎么实现呢?
- 第一种方法:b=argsort(a) #a为你源数组,这里a也可以是tuple
c=b[::-1] #c就是你想要的降序的索引了,然后你就可以通过c来获取a中的值了- 第二种方法:b=argsort(-a)#a为源数组,不能是tuple
(7)、transpose( )方法
这个方法用于矩阵的转置.
完成矩阵的转置还可以这样做:A.T来完成
假设矩阵为A,则y=transpose(A)或者是y=A.T就可以完成转置,但是有一种情况我们用transpose( )函数进行转置需要注意。
如下: x=linspace(0,4,5) #参数的意义按顺序为:开始值、终值和元素个数
y=transpose(x) #转置
- 1
- 1
从上面两个结果对比可以看出,没有转置成功,那么,这种情况下,怎么能够成功??
原因是因为:x.shape 为(5,),而不是(5,1)导致的
x=linspace(0,4,5)
x.shape=(5,1)
y=transpose(x)
- 1
- 2
- 3
- 1
- 2
- 3
从结果可以看出,这样我们就转置成功了。
numpy函数库中一些常用函数的记录的更多相关文章
- lua的table库中的常用函数总结
table是Lua语言中的一种重要的数据类型, table 的一些特性简单列举如下: (1).table 是一个“关联数组”,数组的索引可以是数字或者是字符串; (2).table 的默认初始索引一般 ...
- numpy函数库中一些经常使用函数的记录
##numpy函数库中一些经常使用函数的记录 近期才開始接触python,python中为我们提供了大量的库,不太熟悉.因此在<机器学习实战>的学习中,对遇到的一些函数的使用方法进行记录. ...
- Mysql中的常用函数:
Mysql中的常用函数: 1.字符串函数: (1).合并字符串 concat():// concat('M','y',"SQL",'5.5');== MySQL5.5//当传入的参 ...
- socket编程中客户端常用函数
1 常用函数 1.1 connect() int connect(int sockfd, const struct sockaddr *servaddr, socklen_taddrlen); 客 ...
- Lua中的常用函数库汇总
lua库函数 这些函数都是Lua编程语言的一部分, 点击这里了解更多. assert(value) - 检查一个值是否为非nil, 若不是则(如果在wow.exe打开调试命令)显示对话框以及输出错误调 ...
- numpy中一些常用函数的用法总结
先简单记录一下,后续补充详细的例子 1. strip()函数 s.strip(rm):s为字符串,rm为要删除的字符序列 只能删除开头或是结尾的字符或者字符串.不能删除中间的字符或是字符串 当rm ...
- C++中string常用函数用法总结
string(s小写)是C++标准库中的类,纯C中没有,使用时需要包含头文件#include<string>,注意不是<string.h>,下面记录一下string中比较常用的 ...
- Python numpy总结(3)——常用函数用法
1,np.ceil(x, y) 限制元素范围,进一法,即向上取整. x 表示输入的数据 y float类型 表示每个元素的上限. a = np.array([-1.7, -1.5, -0.2, 0. ...
- python重要的第三方库pandas模块常用函数解析之DataFrame
pandas模块常用函数解析之DataFrame 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 打开浏览器 ...
随机推荐
- Linux系统下的SSH 使用总结
对于linux运维工作者而言,使用ssh远程远程服务器是再熟悉不过的了!对于ssh的一些严格设置也关系到服务器的安全维护,今天在此,就本人工作中使用ssh的经验而言,做一些总结记录来下. -bash: ...
- Ubuntu下载源码并编译
本文章将介绍如何在Ubunt下进行Linux源码下载,并进行简单的编译步骤. 1.下载linux源码 先查看对应的Ubuntu对应版本源码 $ sudo apt-cache search linux- ...
- Referenced file contains errors (http://www.springframework.org/...解决
今天打开老项目出现如下错误: Referenced file contains errors (http://www.springframework.org/schema/context/spring ...
- RestTemplate相关组件:ClientHttpRequestInterceptor【享学Spring MVC】
每篇一句 做事的人和做梦的人最大的区别就是行动力 前言 本文为深入了解Spring提供的Rest调用客户端RestTemplate开山,对它相关的一些组件做讲解. Tips:请注意区分RestTemp ...
- 进程池和线程池、协程、TCP单线程实现并发
一.进程池和线程池 当被操作对象数目不大时,我们可以手动创建几个进程和线程,十几个几十个还好,但是如果有上百个上千个.手动操作麻烦而且电脑硬件跟不上,可以会崩溃,此时进程池.线程池的功效就能发挥了.我 ...
- 文件包含lfi
CG-CTF web(文件包含漏洞) 参考链接:https://blog.csdn.net/qq_34072526/article/details/89431431 php://filter 的使用: ...
- Spring中的ApplicationListener的使用详解案例
本文链接:https://blog.csdn.net/u010963948/article/details/83507185 1.ApplicationContext Spring的核心,Contex ...
- springcolud 的学习(一),架构的发展史
一.传统架构 传统的SSH架构,分为三层架构 web控制层.业务逻辑层.数据库访问层. 传统架构也就是单点应用,就是大家在刚开始初学JavaEE技术的时候SSH架构或者SSM架构,业务没有进行拆分,都 ...
- Linux系统新手入门学习的四点建议
随着计算机的普及.互联网的发展,原本黑客手中的攻城利器---Linux,渐渐进入到普通群众的视线里,让越来越多的人接触到Linux,并学习Linux进而投身到Linux运维工作中去.如果大家对Linu ...
- JS国际化网站中英文切换(理论支持所有语言)应用于h5版APP
网页框架类APP实现国际化参考文案一 参考:https://blog.csdn.net/CSDN_LQR/article/details/78026254 另外付有自己实现的方法 本人用于H5版的AP ...