mean()

  1. mean() 函数定义:

numpy.``mean(a, axis=None, dtype=None, out=None, keepdims=<class numpy._globals._NoValue at 0x40b6a26c>)[source]

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. float64intermediate 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 : None or int or tuple of ints, optional

    Axis or axes along which the means are computed. The default is to compute the mean of the flattened array.New in version 1.7.0.If this is a tuple of ints, a mean is performed over multiple axes, instead of a single axis or all the axes as before.

  • 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.

  • 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 input array.If the default value is passed, then keepdims will not be passed through to the mean method of sub-classes of ndarray, however any non-default value will be. If the sub-classes sum method does not implement keepdims any exceptions will be raised.-

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.

  1. mean()函数功能:求取均值

    经常操作的参数为axis,以m * n矩阵举例:

    axis 不设置值,对 m*n 个数求均值,返回一个实数

    axis = 0:压缩行,对各列求均值,返回 1* n 矩阵

    axis =1 :压缩列,对各行求均值,返回 m *1 矩阵

    举例:

    >>> import numpy as np

    >>> np.mean(now2) # 对所有元素求均值

    3.5

    >>> np.mean(now2,0) # 压缩行,对各列求均值

    matrix([[ 2.5, 3.5, 4.5]])

array类矩阵

import numpy

建立矩阵

直接建立

a = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
b = numpy.array([[1,2], [3,4]], dtype=complex )

从tuple建立

a = [[1,2,3],[4,5,6],[7,8,9]]
b = numpy.array(a)

建立特殊矩阵

a = numpy.zeros([4,5]) # all zero
a = numpy.ones([7,6]) # all one
a = numpy.eye(4,7) # 4x7 diagonal
a = numpy.diag(range(5)) # 5x5 diagonal
a = numpy.empty((2,3))
a = numpy.arange(10, 30, 5) # array([10, 15, 20, 25]), 1-D
a = numpy.linspace(0, 2, 9) # 9 numbers from 0 to 2
a = numpy.random.random((2,3)) # 随机数矩阵
a = numpy.fromfunction(f,(5,4),dtype=int) # 从函数f(x,y)建立

矩阵变换、变形

a.reshape(-1)
a.reshape(3, 4, -1)
a.T # 转置
a.transpose() # 转置
numpy.linalg.inv(a) # 求逆
a.diagonal([offset, axis1, axis2]) # 对角元
numpy.einsum('iijj->ij',a)
numpy.r_[a,b] # 在a中增加新行b
numpy.c_[a,b] # 新列

一般运算

y = x # 建立引用,修改x会影响y
y = x.copy() # 建立副本,修改x不会影响y
a.dot(b) # 矩阵乘法
numpy.dot(a,b) # 矩阵乘法
numpy.trace(a) #求迹

特殊运算

numpy.einsum('iijj->ij',a)

arange()

类似于range(),

range(1,5,2)   #[1, 3]
arange(1,5,2) #[1 3]

reshape()

np.array(range(1,11)).reshape((-1,1))

[[ 1]
[ 2]
[ 3]
[ 4]
[ 5]
[ 6]
[ 7]
[ 8]
[ 9]
[10]] np.array(range(1,11)).reshape((2,5)) [[ 1 2 3 4 5]
[ 6 7 8 9 10]]

meshgrid()

​ meshgrid函数用两个坐标轴上的点在平面上画格。

用法:

  [X,Y]=meshgrid(x,y)

  [X,Y]=meshgrid(x)与[X,Y]=meshgrid(x,x)是等同的

  [X,Y,Z]=meshgrid(x,y,z)生成三维数组,可用来计算三变量的函数和绘制三维立体图

例:x=-3:1:3;y=-2:1:2;

  [X,Y]= meshgrid(x,y);

  这里meshigrid(x,y)的作用是产生一个以向量x为行,向量y为列的矩阵,而x是从-3开始到3,每间隔1记下一个数据,并把这些数据集成矩阵X;同理y则是从-2到2,每间隔1记下一个数据,并集成矩阵Y。即

  X=

  -3 -2 -1 0 1 2 3

  -3 -2 -1 0 1 2 3

  -3 -2 -1 0 1 2 3

  -3 -2 -1 0 1 2 3

  -3 -2 -1 0 1 2 3

  Y =

  -2 -2 -2 -2 -2 -2 -2

  -1 -1 -1 -1 -1 -1 -1

  0 0 0 0 0 0 0

  1 1 1 1 1 1 1

  2 2 2 2 2 2 2

附注:例题中meshgrid(-3:1:3,-2:1:2);因为-3:1:3产生的是含有7个数字的行向量;-2:1:2产生的是含有5个数字的行向量。所以该命令的结果是产生57的矩阵(X,Y都是57的矩阵;其中X是由第一个含7个元素的行向量产生,Y是由第二个行向量产生)

ravel()

与flatten()一样,都是给array数组降维,flatten()返回的是拷贝,与原array数组的存储位置不同,ravel()与原array数组指向一致

>>> x = np.array([[1, 2], [3, 4]])
>>> x
array([[1, 2],
[3, 4]])
>>> x.flatten()
array([1, 2, 3, 4])
>>> x.ravel()
array([1, 2, 3, 4])
两者默认均是行序优先
>>> x.flatten('F')
array([1, 3, 2, 4])
>>> x.ravel('F')
array([1, 3, 2, 4]) >>> x.reshape(-1)
array([1, 2, 3, 4])
>>> x.T.reshape(-1)
array([1, 3, 2, 4])

c_()

将切片对象沿第二个轴(按列)转换为连接。

np.c_[np.array([1,2,3]), np.array([4,5,6])]
array([[1, 4],
[2, 5],
[3, 6]]) np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]
array([[1, 2, 3, 0, 0, 4, 5, 6]])

python-numpy-1的更多相关文章

  1. Python/Numpy大数据编程经验

    Python/Numpy大数据编程经验 1.边处理边保存数据,不要处理完了一次性保存.不然程序跑了几小时甚至几天后挂了,就啥也没有了.即使部分结果不能实用,也可以分析程序流程的问题或者数据的特点.   ...

  2. 在python&numpy中切片(slice)

     在python&numpy中切片(slice) 上文说到了,词频的统计在数据挖掘中使用的频率很高,而切片的操作同样是如此.在从文本文件或数据库中读取数据后,需要对数据进行预处理的操作.此时就 ...

  3. Python numpy中矩阵的用法总结

    关于Python Numpy库基础知识请参考博文:https://www.cnblogs.com/wj-1314/p/9722794.html Python矩阵的基本用法 mat()函数将目标数据的类 ...

  4. Python NumPy学习总结

    一.NumPy简介 其官网是:http://www.numpy.org/ NumPy是Python语言的一个扩充程序库.支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库.Num ...

  5. Python Numpy shape 基础用法(转自他人的博客,如涉及到侵权,请联系我)

    Python Numpy shape 基础用法 shape函数是numpy.core.fromnumeric中的函数,它的功能是读取矩阵的长度,比如shape[0]就是读取矩阵第一维度的长度.它的输入 ...

  6. [转]Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate()

    Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate() 觉得有用的话,欢迎一起讨论相互学习~Follow Me ...

  7. CS231n课程笔记翻译1:Python Numpy教程

    译者注:本文智能单元首发,翻译自斯坦福CS231n课程笔记Python Numpy Tutorial,由课程教师Andrej Karpathy授权进行翻译.本篇教程由杜客翻译完成,Flood Sung ...

  8. 最实用windows 下python+numpy安装(转载)

    最实用windows 下python+numpy安装 如题,今天兜兜转转找了很多网站帖子,一个个环节击破,最后装好费了不少时间. 希望这个帖子能帮助有需要的人,教你一篇帖子搞定python+numpy ...

  9. python numpy array 与matrix 乘方

    python numpy array 与matrix 乘方 编程语言 waitig 1年前 (2017-04-18) 1272℃ 百度已收录 0评论 数组array 的乘方(**为乘方运算符)是每个元 ...

  10. Python Numpy基础教程

    Python Numpy基础教程 本文是一个关于Python numpy的基础学习教程,其中,Python版本为Python 3.x 什么是Numpy Numpy = Numerical + Pyth ...

随机推荐

  1. C# 获取当前执行DLL 所在路径

    有的时候,当前执行的DLL 和启动的EXE 所在路径并不一致,这时我们想要获得当前执行DLL 所在路径可以使用下面的方法. // Summary: // Gets the path or UNC lo ...

  2. zabbix 3.2.2 server端添加客户端主机配置 (四)

    一.添加主机 主机是Zabbix监控的基本载体,所有的监控项都是基于主机的,那么我们如何来添加一台被监控的主机呢? 1.首先要在被监控的主机上安装好zabbix_agent服务,并可以正常启动zabb ...

  3. 如何在国内跑Kubernetes的minikube

    跑minikube start老是被卡住,得到如以下的结果 minikube start --registry-mirror=https://registry.docker-cn.com miniku ...

  4. PAT Basic 1021 个位数统计 (15 分)

    给定一个 k 位整数 1 (0, ,, d​k−1​​>0),请编写程序统计每种不同的个位数字出现的次数.例如:给定 0,则有 2 个 0,3 个 1,和 1 个 3. 输入格式: 每个输入包含 ...

  5. Elasticsearch中Mapping

    映射(Mapping) 概念:创建索引时,可以预先定义字段的类型以及相关属性.从而使得索引建立得更加细致和完善.如果不预先设置映射,会自动识别输入的字段类型. 官方文档(字段数据类型):https:/ ...

  6. insightface数据裁剪过程

    数据裁剪 我们用lfw数据做实验,你也可以自己找数据. lfw数据 http://vis-www.cs.umass.edu/lfw/ 我下载的是这个原图像https://drive.google.co ...

  7. Python 等分切分数据及规则命名

    将一份一亿多条数据的csv文件等分为10份,代码如下所示: import pandas as pd data = pd.read_csv('C:\\Users\\PycharmProjects\\Sp ...

  8. Easy Populate批量管理下载产品数据为空的解决办法

    把原来的先删除:http://aaaaacom/admin/easypopulate.php?langer=remove

  9. 【SDR】UHD安装教程

    USRP作为软件无线电系统中常用的射频设备,其驱动UHD的安装及稳定运行,是SDR系统稳定的必备条件,该篇博客总结UHD的相关安装方法,主要有三种,分别是apt-get.github clone源码编 ...

  10. 深入理解TransactionTemplate编程式事务

    Spring可以支持编程式事务和声明式事务. Spring提供的最原始的事务管理方式是基于TransactionDefinition.PlatformTransactionManager.Transa ...