一:NumPy简介

  • 官网链接:http://www.numpy.org/

  • NumPy教程链接:https://www.yiibai.com/numpy/

  • NumPy是Python语言的一个扩充程序库。支持高级大量的多维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库

  NumPy – MatLab 的替代之一

  NumPy 通常与 SciPy(Scientific Python)和 Matplotlib(绘图库)一起使用。 这种组合广泛用于替代 MatLab,是一个流行的技术计算平台。

  但是,Python 作为 MatLab 的替代方案,现在被视为一种更加现代和完整的编程语言。

二:NumPy基本功能  

  • 快速高效的多维数组对象ndarray

  • 用于对数组执行元素级计算以及直接对数组执行数学运算的函数

  • 用于读写硬盘上基于数组的数据集的

  • 线性代数运算、傅里叶变换,以及随机数生成

  • 傅立叶变换和用于图形操作的例程。

  • 与线性代数有关的操作。 NumPy 拥有线性代数和随机数生成的内置函数。

三:效率对比

  • 三种数据结构:list / array / numpy.array

  • 三种方法求和:for / sum / numpy.sum

import timeit
common_for = """
for d in data:
s += d
""" common_sum = """
sum(data)
""" common_numpy_sum = """
numpy.sum(data)
""" def timeit_list(n, loops):
list_setup = """
import numpy
data = [1] * {}
s = 0
""".format(n)
print 'list:'
print timeit.timeit(common_for, list_setup, number = loops)
print timeit.timeit(common_sum, list_setup, number = loops)
print timeit.timeit(common_numpy_sum, list_setup, number = loops) def timeit_array(n, loops):
array_setup = """
import numpy
import array
data = array.array('L', [1] * {})
s = 0
""".format(n)
print 'array:'
print timeit.timeit(common_for, array_setup, number = loops)
print timeit.timeit(common_sum, array_setup, number = loops)
print timeit.timeit(common_numpy_sum, array_setup, number = loops) def timeit_numpy(n, loops):
numpy_setup = """
import numpy
data = numpy.array([1] * {})
s = 0
""".format(n)
print 'numpy:'
print timeit.timeit(common_for, numpy_setup, number = loops)
print timeit.timeit(common_sum, numpy_setup, number = loops)
print timeit.timeit(common_numpy_sum, numpy_setup, number = loops) if __name__ == '__main__':
timeit_list(50000, 500)
timeit_array(50000, 500)
timeit_numpy(50000, 500)

运行结果:

list:
0.94836021896
0.126542218145
1.25408217549
array:
2.02870422344
1.4137293358
5.92805967058
numpy:
3.81213793067
2.90964482707
0.0174179931709

四:NumPy的Ndarray对象(N 维数组类型)

  基本的ndarray是使用 NumPy 中的数组函数创建的,如下所示:

   

  它从任何暴露数组接口的对象,或从返回数组的任何方法创建一个ndarray。

numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)

  上面的构造器接受以下参数:

序号 参数及描述
1. object 任何暴露数组接口方法的对象都会返回一个数组或任何(嵌套)序列。
2. dtype 数组的所需数据类型,可选。
3. copy 可选,默认为true,对象是否被复制。
4. order C(按行)、F(按列)或A(任意,默认)。
5. subok 默认情况下,返回的数组被强制为基类数组。 如果为true,则返回子类。
6. ndimin 指定返回数组的最小维数。

五:代码示例:

# -*- coding: utf-8 -*-

import numpy as np

print '使用普通一维数组生成NumPy一维数组'
data = [6, 7.5, 8, 0, 1]
arr = np.array(data)
print arr
print '打印元素类型'
print arr.dtype
print print '使用普通二维数组生成NumPy二维数组'
data = [[1, 2, 3, 4], [5, 6, 7, 8]]
arr = np.array(data)
print arr
print '打印数组维度'
print arr.shape
print print '使用最小维度生成NumPy维度数组'
data = np.array([1, 2, 3,4,5], ndmin = 2)
print data
print print '使用zeros/empty'
print np.zeros(10) # 生成包含10个0的一维数组
print np.zeros((3, 6)) # 生成3*6的二维数组
print np.empty((2, 3, 2)) # 生成2*3*2的三维数组,所有元素未初始化。
print print '使用arrange生成连续元素'
print np.arange(15) # [0, 1, 2, ..., 14]

运行结果:

使用普通一维数组生成NumPy一维数组
[6. 7.5 8. 0. 1. ]
打印元素类型
float64
-------------------------------------
使用普通二维数组生成NumPy二维数组
[[1 2 3 4]
[5 6 7 8]]
打印数组维度
(2L, 4L)
-------------------------------------
使用最小维度生成NumPy维度数组
[[1 2 3 4 5]]
-------------------------------------
使用zeros/empty
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]
[[[1.91684511e-316 1.55481945e-316]
[0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000]] [[0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000]]]
-------------------------------------
使用arrange生成连续元素
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]

六:NumPy - 数据类型

NumPy 支持比 Python 更多种类的数值类型。 下表显示了 NumPy 中定义的不同标量数据类型。

序号 数据类型及描述
1. bool_存储为一个字节的布尔值(真或假)
2. int_默认整数,相当于 C 的long,通常为int32或int64
3. intc相当于 C 的int,通常为int32或int64
4. intp用于索引的整数,相当于 C 的size_t,通常为int32或int64
5. int8字节(-128 ~ 127)
6. int1616 位整数(-32768 ~ 32767)
7. int3232 位整数(-2147483648 ~ 2147483647)
8. int6464 位整数(-9223372036854775808 ~ 9223372036854775807)
9. uint88 位无符号整数(0 ~ 255)
10. uint1616 位无符号整数(0 ~ 65535)
11. uint3232 位无符号整数(0 ~ 4294967295)
12. uint6464 位无符号整数(0 ~ 18446744073709551615)
13. float_float64的简写
14. float16半精度浮点:符号位,5 位指数,10 位尾数
15. float32单精度浮点:符号位,8 位指数,23 位尾数
16. float64双精度浮点:符号位,11 位指数,52 位尾数
17. complex_complex128的简写
18. complex64复数,由两个 32 位浮点表示(实部和虚部)
19. complex128复数,由两个 64 位浮点表示(实部和虚部)

  数据类型对象 (dtype):numpy.dtype(object, align, copy)

# -*- coding: utf-8 -*-

import numpy as np
#定义名为 student 的结构化数据类型,其中包含字符串字段name,整数字段age和浮点字段marks。 此dtype应用于ndarray对象
student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
print student
a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student)
print a
print '---------------------------------' print " 文件名称可用于访问 age 列的内容 "
dt = np.dtype([('age',np.int8)])
print dt
a = np.array([(10,),(20,),(30,)], dtype = dt)
print a
print a['age']

 

  

  

最新文章

  1. ROS学习(一)—— 环境搭建
  2. sscanf_强大的数据读取-转换
  3. 我是如何反编译D-Link路由器固件程序并发现它的后门的
  4. CSS3中box-sizing的理解
  5. Struts2 源码分析——核心机制
  6. 如何在Ubuntu上配置scala教程
  7. 【PHP设计模式 10_ShiPeiQi.php】适配器模式
  8. java读XML文件
  9. hitTest:withEvent:方法(此方法可实现点击穿透、点击下层视图功能)
  10. 段的创建表user_segments
  11. JS常用方法总结
  12. CentOS7 添加端口
  13. ADT SDK Manager启动时一闪而过
  14. Mac 电脑终端上传项目到github上
  15. 3_主流部署方式介绍-Django+mod_wsgi+Apache
  16. linq查询数值为null的问题以及数据表的关联计算问题
  17. 怎样制作爽心的 dashboard ?
  18. SpringSecurity-ConcurrentSessionFilter的作用
  19. linux系统被ddos攻击识别
  20. C++ Error : initial value of reference to non-const must be an lvalue

热门文章

  1. 安装php的sphinx扩展模块
  2. jvm小白
  3. three.js后期之自定义shader通道实现扫光效果
  4. etcd配置参数详解
  5. 【VS开发】uafxcwd.lib(afxmem.obj) : error LNK2005: 已经在 LIBCMTD.lib(new.obj) 中定义错误解决方案
  6. 记录sql中统计近五天数据的口径(While+IF)
  7. Socket与系统调用深层分析
  8. gdb移植(交叉版本)
  9. kafka安装使用配置1.2
  10. PAT A1016 Phone Bills (25)