numpy库是python的一个著名的科学计算库,本文是一个quickstart。

引入:计算BMI

BMI = 体重(kg)/身高(m)^2

假如有如下几组体重和身高数据,让求每组数据的BMI值:

weight = [65.4,59.2,63.6,88.4,68.7]
height = [1.73,1.68,1.71,1.89,1.79]
print weight / height ** 2

执行上面代码,报错:TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'

这是因为普通的除法是元素级的而非向量级的,并不能应用到一组数据上。

解决方案:使用numpy.ndarray数据结构(N维数组),运算是面向矩阵的:

import numpy as np
np_weight = np.array(weight)
np_height = np.array(height)
print type(np_weight)
print type(np_height)
<type 'numpy.ndarray'>
<type 'numpy.ndarray'>
print np_weight
print np_height
[ 65.4  59.2  63.6  88.4  68.7]
[ 1.73 1.68 1.71 1.89 1.79]

注:和python的列表不同的是,numpy.ndarray数据结构的元素之间是没有逗号分隔的。

np_bmi = np_weight / np_height ** 2
print type(np_bmi)
print np_bmi
<type 'numpy.ndarray'>
[ 21.85171573 20.97505669 21.75028214 24.7473475 21.44127836]

numpy数组:numpy.ndarray

numpy.ndarray是numpy最基本的数据结构,即N维数组,且数组中的元素需要是同一种类型,如果不是,则会自动转换成同一种类型,如:

print np.array([1.0,'hi',True])
['1.0' 'hi' 'True']

可以看到都被转成了字符串类型。

不同数据类型的不同行为

# 普通的python列表
py_list = [1,2,3]
# numpy数组
np_array = np.array(py_list)
print py_list + py_list  # 这是列表的拼接
[1, 2, 3, 1, 2, 3]
print np_array + np_array  # 这是每两个对应元素之间的运算
[2 4 6]

子集

print np_bmi[0]
21.8517157272
print np_bmi > 23
[False False False  True False]
print np_bmi[np_bmi > 23]
[ 24.7473475]

二维numpy数组

二维numpy数组是以list作为元素的数组,比如:

np_2d = np.array([height,weight])
print type(np_2d)
<type 'numpy.ndarray'>
print np_2d
[[  1.73   1.68   1.71   1.89   1.79]
[ 65.4 59.2 63.6 88.4 68.7 ]]
print np_2d.shape
(2, 5)

通过shape属性值可以看出,np_2d是一个2行5列的二维数组。

single type原则

print np.array([[1,2],[3,'4']])
[['1' '2']
['3' '4']]

二维numpy数组的子集

np_2d = np.array([height,weight])
print np_2d
[[  1.73   1.68   1.71   1.89   1.79]
[ 65.4 59.2 63.6 88.4 68.7 ]]
print np_2d[0][2]
1.71
print np_2d[0,2]
1.71

还可以在两个轴向上分别切片:

print np_2d[:,1:3]
[[  1.68   1.71]
[ 59.2 63.6 ]]

选取第1行:

print np_2d[1,:]
[ 65.4  59.2  63.6  88.4  68.7]

求对应的BMI值:

print np_2d[1,:] / np_2d[0,:] ** 2
[ 21.85171573  20.97505669  21.75028214  24.7473475   21.44127836]

应用

用numpy生成呈正太分布的随机测试数据,并求各项基本的统计数据。

比如生成10000条数据集,记录的是某个镇上所有居民的身高(m)、体重(kg)数据,所用到的函数:

np.random.normal(均值,标准差,取样数)

height = np.random.normal(1.75,0.20,10000)
weight = np.random.normal(60.32,15,10000)

下面将若干个(这里是2个)一维数组拼成一个二维数组(有点像zip()函数的作用):

np_info = np.column_stack((height,weight))
print np_info
[[  1.88474198  76.24957048]
[ 1.85353302 64.62674488]
[ 1.74999035 67.5831439 ]
...,
[ 1.78187257 50.11001273]
[ 1.90415778 50.65985964]
[ 1.51573081 41.00493358]]

求np_info身高平均值:

print np.mean(np_info[:,0])
1.75460102053

求身高的中位数:

print np.median(np_info[:,0])
1.75385473036

求身高和体重的相关系数:

print np.corrcoef(np_info[:,0],np_info[:,1])
[[  1.00000000e+00  -1.50825116e-04]
[ -1.50825116e-04 1.00000000e+00]]

求身高的标准差:

print np.std(np_info[:,0])
0.201152169706

排序(不会影响源数组):

print np.sort(np_info[0:10,0])
[ 1.46053123  1.59268772  1.74939538  1.74999035  1.78229515  1.85353302
1.88474198 1.99755291 2.12384833 2.3727505 ]

求和:

print np.sum(np_info[0:10,0])
18.5673265584

随机推荐

  1. 隐式意图调用系统自带组件的各种Uri总结

    调用系统应用解析(必需要加各自使用的权限)  android intent 隐式意图和显示意图(activity跳转) 显示意图要求必须知道被激活组件的包和class 隐式意图仅仅须要知道跳转acti ...

  2. ConfigurationSection类使用心得

    ConfigurationSection类主要是方便我们用于扩展自定义webcongfig中的节点信息.我们可以方便的通过以下方式获取[自定义节点对象] [你自定义的对象] config = ([你自 ...

  3. cocos2dx lua 3.10 使用cjson

    本篇介绍如何在lua中使用cjson对数据进行json的encode与decode,首先简单介绍下cjson: Lua CJSON 是 Lua 语言提供高性能的 JSON 解析器和编码器,其性能比纯 ...

  4. Eclipse UML插件

    Green UML http://green.sourceforge.net/ AmaterasUML http://amateras.sourceforge.jp/cgi-bin/fswiki_en ...

  5. 第七篇:两个经典的文件IO程序示例

    前言 本文分析两个经典的C++文件IO程序,提炼出其中文件IO的基本套路,留待日后查阅. 程序功能 程序一打印用户指定的所有文本文件,程序二向用户指定的所有文本文件中写入数据. 程序一代码及其注释 # ...

  6. SpringMVC JSONP JSON支持

    1.ajax端 $.ajax({ type: "post", dataType: "jsonp", //传递给请求处理程序,用以获得jsonp回调函数名的参数名 ...

  7. 接口测试工具 — jmeter(数据库操作)

    1.导入jdbc jar包 2.配置MySQL连接 3.执行sql语句

  8. 3*0.1 == 0.3 将会返回什么?true 还是 false?

    false,因为有些浮点数不能完全精确的表示出来 public static void main(String[] args) { System.out.println(3 * 0.1); Syste ...

  9. quartz集群 定时任务 改成可配置

    前面的博文中提到的quartz集群方式会有以下缺点: 1.假设配置了3个定时任务,job1,job2,job3,这时数据库里会有3条job相关的记录,如果下次上线要停掉一个定时任务job1,那即使定时 ...

  10. 微信js分享朋友圈(一)

    1.绑定域名 先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”. 备注:登录后可在“开发者中心”查看对应的接口权限. 2.引入js文件 <script type=&q ...