numpy的一些用法
安装numpy
windows安装pip即可,具体方法参考pip官网 http://pip-cn.readthedocs.io/en/latest/installing.html
安装方法:pip install numpy-1.14.3-cp27-none-win_amd64.whl
功能介绍:
- 提供数组的矢量化操作,所谓矢量化就是不用循环就能将运算符应用到数组中的每个元素中。
- 提供数学函数应用到每个数组中元素
- 提供线性代数,随机数生成,傅里叶变换等数学模块
- ndarray:numpy库的心脏,多维数组,具有矢量运算能力,快速、节省空间在array中的数据类型是一致的
ndarray:
ndarray具有多维性。ndarray的元素可以通过索引的方式进行访问。在Numpy中,ndarray的维度称为axes。axes的大小称为rank。列如ndarray[1,2,1],它的维度为1,rank的值为1,因为只有一维。索引从0开始。
print np.identity(3,int) #单位矩阵
结果:
[[1 0 0]
[0 1 0]
[0 0 1]]
零矩阵:
print np.zeros((3,4)) #零矩阵
print np.zeros(3) #零矩阵
结果:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[0. 0. 0.]
全一矩阵:
print np.ones((3,3))
print np.ones(4)
结果:
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
[1. 1. 1. 1.]
矩阵乘法:
a=((1,2,3),(4,5,6),(7,8,9))
a=np.array(a)
print np.dot(2,a)
结果:
[[ 2 4 6]
[ 8 10 12]
[14 16 18]]
矩阵大小:
a=((1,2,3),(4,5,6),(7,8,9))
a=np.array(a)
print a.ndim
结果:
2
行求和,列求和
a=((1,2,3),(4,5,6),(7,8,9))
a=np.array(a)
print np.sum(a,axis=1)
print np.sum(a,axis=0)
#axis=1表示矩阵a的行求和,axis=0表示在列求和
结果:
[ 6 15 24]
[12 15 18]
转置矩阵:
a=((1,2,3),(4,5,6),(7,8,9))
a=np.array(a)
print a.T
结果:
[[1 4 7]
[2 5 8]
[3 6 9]]
其他的一些:
a=((1,2,3),(4,5,6),(7,8,9))
a=np.array(a)
print np.random.random((3,3)) #random模块的random函数,生成随机数
print np.mean(a) #求平均数
print np.max(a) #求最大值
print np.min(a)
print np.std(a) #求标准差
print np.arange(0,20,step=2) #arange可以指定起点,终点,步长进行数组创建
print np.linspace(0, 20, 10) #等同于下面的这个
print np.linspace(start=0, stop=20, num=10)
#直接指定开始,结束然后指定个数进行创建。
print np.random.normal(10,100,size=10) #产生服从高斯分布的随机数,三个参数分别是平均值,方差,个数
结果:
[[0.18149469 0.82166642 0.89837593]
[0.07947753 0.65715104 0.23933089]
[0.34254456 0.19185617 0.17856812]]
5.0
9
1
2.581988897471611
[ 0 2 4 6 8 10 12 14 16 18]
[ 0. 2.22222222 4.44444444 6.66666667 8.88888889 11.11111111
13.33333333 15.55555556 17.77777778 20. ]
[ 0. 2.22222222 4.44444444 6.66666667 8.88888889 11.11111111
13.33333333 15.55555556 17.77777778 20. ]
[ 36.25896713 75.73646837 -90.97435221 -87.55378736 192.75253223
-59.32404814 256.30659631 -161.95343956 5.39389542 -62.17649294]
numpy的一些用法的更多相关文章
- Python Numpy shape 基础用法(转自他人的博客,如涉及到侵权,请联系我)
Python Numpy shape 基础用法 shape函数是numpy.core.fromnumeric中的函数,它的功能是读取矩阵的长度,比如shape[0]就是读取矩阵第一维度的长度.它的输入 ...
- Numpy的简单用法
Numpy的简单用法 import numpy as np 一.创建ndarray对象 列表转换成ndarray: >>> a = [1,2,3,4,5] >>> ...
- numpy中线性代数用法
numpy中线性代数用法 矩阵乘法 >>> import numpy as np >>> x=np.array([[1,2,3],[4,5,6]]) >> ...
- numpy.asmatrix的用法
学习的过程中,遇到了asmatrix的用法,看了一下官方文档,明白了. numpy.asmatrix numpy.asmatrix(data, dtype=None)[source] Interpre ...
- 数据科学:numpy.where() 的用法
原文出处:numpy.where() 用法讲解 原创作者:massquantity numpy.where() 有两种用法: 1. np.where(condition, x, y) 满足条件(con ...
- Py修行路 NumPy模块基本用法
NumPy系统是Python的一种开源的数值计算扩展,一个用python实现的科学计算包.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结 ...
- Numpy的基础用法
1.用Numpy创建数组 numpy.array(object):创建数组,与array.array(typecode[, initializer])不同,array.array()只能创建一维数组 ...
- numpy.random模块用法总结
from numpy import random numpy.random.uniform(low=0.0, high=1.0, size=None) 生出size个符合均分布的浮点数,取值范围为[l ...
- anaconda及jupyter notebook的使用之numpy模块的用法(2)
今日内容概要 numpy模块结束 ndarray创建 numpy内置方法 索引与切片(花式索引.布尔索引) 常用函数 统计方法 随机数 numpy的内置方法 import numpy as np 1. ...
- numpy.random模块用法小结
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9751471.html 1.np.random.random()函数参数 np.random.r ...
随机推荐
- Qt类型转换
(转自:http://qimo601.iteye.com/blog/1260479) 1.char * 与 const char *的转换 char *ch1="hello11"; ...
- LeetCode OJ:Remove Element(移除元素)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 如何在eclipse中安装ADT
打开Eclipse,点击 Help -> Install New Software: 点击Add: 然后点击Archive,添加对应的上面的ADT-21.0.1.zip: OK后,再写上一个名字 ...
- Lumen实现用户注册登录认证
Lumen实现用户注册登录认证 前言 Lumen是一个基于Laravel的微框架,号称是以速度为生.截用Lumen官网的一段,号称是比silex和slim还要快. 本文将用Lumen来实现一个完整的用 ...
- vc++ windows获取计算机信息
在软件开发中,我们经常要获当前系统的版本号,判断当前是什么系统,获取获取物理内存和可用内存大小,获取CPU名称.内核数目.主频,获取MAC地址,获取屏幕分辨率,下面的这个c++类将包含所有这些信息. ...
- opencv之图像滤波
均值滤波 均值滤波函数cv2.blur() import cv2 img = cv2.imread('01.jpg') blur = cv2.blur(img,(5,5)) cv2.imshow(&q ...
- LeetCode Path Sum IV
原题链接在这里:https://leetcode.com/problems/path-sum-iv/description/ 题目: If the depth of a tree is smaller ...
- UVA11168 Airport
题意 PDF 分析 首先发现距离最短的直线肯定在凸包上面. 然后考虑直线一般方程\(Ax+By+C=0\),点\((x_0,y_0)\)到该直线的距离为 \[ \frac{|Ax_0+By_0+C|} ...
- openfaas 简单试用
1. 安装 faas-cli 参考以前文章,或者使用官方的shell脚本 2. 简单例子 mkdir rong cd rong faas-cli new rong --lang python / ...
- 一个靠谱的maven仓库镜像地址
<mirror> <id>sprintio</id> <mirrorOf>central</mirrorOf> <name&g ...