##numpy函数库中一些经常使用函数的记录

近期才開始接触python,python中为我们提供了大量的库,不太熟悉。因此在《机器学习实战》的学习中,对遇到的一些函数的使用方法进行记录。

(1)mat( )

numpy函数库中存在两种不同的数据类型(矩阵matrix和数组array),都能够用于处理行列表示的数字元素。

尽管他们看起来非常类似,可是在这两个数据类型上运行同样的数学运算能够得到不同的结果,当中numpy函数库中matrix与MATLAB中matrices等价。

调用mat( )函数能够将数组转化为矩阵。比如

random.rand(3,3)#构造一个3*3的随机数组

mat(random.rand(3,3))#将3*3的随机数组转化为一个3*3的矩阵

(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个维度的大小

(3)random.uniform( )函数

功能:uniform(x,y) 方法将随机生成下一个实数,它在[x,y]范围内。

參数说明:

x – 随机数的最小值界。

y – 随机数的最大值界。

返回值:返回一个浮点数。

使用方法例如以下:

from numpy import *  #或者直接 import random也能够
random.uniform(x,y)

注意: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)  #转置



从上面两个结果对照能够看出,没有转置成功,那么,这样的情况下,怎么能够成功??

原因是由于:x.shape 为(5,),而不是(5,1)导致的

x=linspace(0,4,5)
x.shape=(5,1)
y=transpose(x)



从结果能够看出。这样我们就转置成功了。

自己在学习的过程中遇到后在慢慢补充。

numpy函数库中一些经常使用函数的记录的更多相关文章

  1. c++函数库中一些实用的函数

    有一些程序,虽然写起来不难,但是可能比较麻烦或容易出错,这时就可以用c++函数库里自带的一些实用的函数. 这里只记录一些不太常见的函数. ------------------------------- ...

  2. numpy函数库中一些常用函数的记录

    ##numpy函数库中一些常用函数的记录 最近才开始接触Python,python中为我们提供了大量的库,不太熟悉,因此在<机器学习实战>的学习中,对遇到的一些函数的用法进行记录. (1) ...

  3. 查看mysql库中所有表的大小和记录数

    查看mysql库中所有表的大小和记录数 ,), 'MB') as total_size FROM information_schema.TABLES WHERE TABLE_SCHEMA='datab ...

  4. 使用Python PIL库中的Image.thumbnail函数裁剪图片

    今天,是我来到博客园的第五天,发现自己还没有头像,想着上传ubuntu系统中我很喜欢的一个背景图片来当头像,但是因为图片过大,上传失败了.那么,我们如何使用python中强大的PIL库来进行图片裁剪呢 ...

  5. Ritchie Lawrence 批处理函数库中英文版

    可以到这个网址去看看,如果你是注册用户,还可以下载到bat的很多函数库,具体地址如下: http://www.bathome.net/viewthread.php?tid=3056&extra ...

  6. 【C++实现python字符串函数库】一:分割函数:split、rsplit

    [C++实现python字符串函数库]split()与rsplit()方法 前言 本系列文章将介绍python提供的字符串函数,并尝试使用C++来实现这些函数.这些C++函数在这里做单独的分析,最后我 ...

  7. javascript 在一个函数参数中包含另一个函数的引用

    javascript函数的参数包含另一个函数的情形: <script> //b函数的参数func为另一个函数 function b(a, func) {  alert(a); //调用参数 ...

  8. numpy 和 pandas 中常用的一些函数及其参数

    numpy中有一些常用的用来产生随机数的函数,randn()和rand()就属于这其中. numpy.random.randn(d0, d1, …, dn)是从标准正态分布中返回一个或多个样本值.  ...

  9. lua的table库中经常使用的函数

    lua提供了一些辅助函数来操作table. 比如,从list中insert和remove元素,对array的元素进行sort.或者concatenate数组中的全部strings.以下就具体地解说这些 ...

随机推荐

  1. tyvj 2020 rainbow 的信号

    期望 被精度坑惨的我 注意:能开 long long 尽量开, 先除后乘, int 转 double 的时候 先转换在做运算 本题与位运算有关,位与位之间互不影响,所以我们可以分开考虑 #includ ...

  2. Session挂起

    异常信息: toString() unavailable - no suspended threads 使用Spring管理 ,在使用hibernate时使用如下语句Session session = ...

  3. 利用jenkins和hockey组织iOS持续交付过程

    1. jenkins可以单独作为web应用部署和启动,但建议将其部署在tomcat上,可以方便的管理其他一些web应用,下面讲解如何在Mac设备上安装tomcat 到tomcat官网下载适用于Mac设 ...

  4. Servlet 2.4 规范之第六篇:响应

    响应对象封装了服务端返回给客户端的所有信息.在HTTP协议中,这些信息通过HTTP头和消息体传送. SRV.5.1    缓冲 出于效率考量,servlet容器可以缓冲输出数据,但这并非强制要求.常见 ...

  5. GitHub 上受欢迎的 Android UI Library 整理(一)

    抽屉菜单 https://github.com/mikepenz/MaterialDrawer ★7337 - 安卓抽屉效果实现方案https://github.com/Yalantis/Side-M ...

  6. Django学习笔记(12)——分页功能

    这一篇博客记录一下自己学习Django中分页功能的笔记.分页功能在每个网站都是必要的,当页面因需要展示的数据条目过多,导致无法全部显示,这时候就需要采用分页的形式进行展示. 分页在网站随处可见,下面展 ...

  7. House Robber(动态规划)

    思路: 代码: class Solution { public: int rob(vector<int> &num) { ; int size=num.size(); ) ]; v ...

  8. Office 各版本下载链接

    Office 2007 链接: https://pan.baidu.com/s/1pNJDlafw6KQSlljRUAQtWw 提取码: xoml 密钥:DBXYD-TF477-46YM4-W74MH ...

  9. IntelliJ IDEA导入包的顺序调整和按包类型分类(保持和Eclipse一致)

    调整的内容如下: 空行 import java.* 空行 import javax.* 空行 import com.* 空行 import all other imports 空行 import st ...

  10. A Good User Interface

    has high conversion rates and is easy to use. In other words, it's nice to both the business side as ...