Numpy简单介绍
1.Numpy是什么

很简单,Numpy是Python的一个科学计算的库,提供了矩阵运算的功能,其一般与Scipy、matplotlib一起使用。其实,list已经提供了类似于矩阵的表示形式,不过numpy为我们提供了更多的函数。如果接触过matlab、scilab,那么numpy很好入手。 在以下的代码示例中,总是先导入了numpy:(通用做法import numpu as np 简单输入)

>>> import numpy as np
>>> print np.version.version
1.6.2
  1. 多维数组

多维数组的类型是:numpy.ndarray。

使用numpy.array方法

以list或tuple变量为参数产生一维数组:

>>> print np.array([1,2,3,4])
[1 2 3 4]
>>> print np.array((1.2,2,3,4))
[ 1.2  2.   3.   4. ]
>>> print type(np.array((1.2,2,3,4)))
<type 'numpy.ndarray'>

以list或tuple变量为元素产生二维数组或者多维数组:

>>> x = np.array(((1,2,3),(4,5,6)))
>>> x
array([[1, 2, 3],[4, 5, 6]])
>>> y = np.array([[1,2,3],[4,5,6]])
>>> y
array([[1, 2, 3],[4, 5, 6]])

numpy数据类型设定与转换
numpy ndarray数据类型可以通过参数dtype 设定,而且可以使用astype转换类型,在处理文件时候这个会很实用,注意astype 调用会返回一个新的数组,也就是原始数据的一份拷贝。

numeric_strings2 = np.array([‘1.23’,‘2.34’,‘3.45’],dtype=np.string_)

numeric_strings2
Out[32]:
array([‘1.23’, ‘2.34’, ‘3.45’],
dtype=’|S4’)

numeric_strings2.astype(float)
Out[33]: array([ 1.23, 2.34, 3.45])

numpy索引与切片
index 和slicing :第一数值类似数组横坐标,第二个为纵坐标

>>> x[1,2]
6
>>> y=x[:,1]
>>> y
array([2, 5])

涉及改变相关问题,我们改变上面y是否会改变x?这是特别需要关注的!

>>> y
array([2, 5])
>>> y[0] = 10
>>> y
array([10,  5])
>>> x
array([[ 1, 10,  3],[ 4,  5,  6]])

通过上面可以发现改变y会改变x ,因而我们可以推断,y和x指向是同一块内存空间值,系统没有为y 新开辟空间把x值赋值过去。

arr = np.arange(10)

arr
Out[45]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

arr[4]
Out[46]: 4

arr[3:6]
Out[47]: array([3, 4, 5])

arr[3:6] = 12

arr
Out[49]: array([ 0, 1, 2, 12, 12, 12, 6, 7, 8, 9])

如上所示:当将一个标量赋值给切片时,该值会自动传播整个切片区域,这个跟列表最重要本质区别,数组切片是原始数组的视图,视图上任何修改直接反映到源数据上面。
思考为什么这么设计? Numpy 设计是为了处理大数据,如果切片采用数据复制话会产生极大的性能和内存消耗问题。

假如说需要对数组是一份副本而不是视图可以如下操作:

arr_copy = arr[3:6].copy()

arr_copy[:]=24

arr_copy
Out[54]: array([24, 24, 24])

arr
Out[55]: array([ 0, 1, 2, 12, 12, 12, 6, 7, 8, 9])

再看下对list 切片修改

l=range(10)

l
Out[35]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

l[5:8] = 12
Traceback (most recent call last):

File “”, line 1, in
l[5:8] = 12

TypeError: can only assign an iterable

l1= l[5:8]

l1
Out[38]: [5, 6, 7]

l1[0]=12

l1
Out[40]: [12, 6, 7]

l
Out[41]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
这里设计到python 中深浅拷贝,其中切片属于浅拷贝,具体参考:python深浅拷贝

多维数组索引、切片
arr2d = np.arange(1,10).reshape(3,3)

arr2d
Out[57]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

arr2d[2]
Out[58]: array([7, 8, 9])

arr2d[0][2]
Out[59]: 3

arr2d[0,2]
Out[60]: 3

布尔型索引
这种类型在实际代码中出现比较多,关注下。

names = np.array([‘Bob’,‘joe’,‘Bob’,‘will’])

names == ‘Bob’
Out[70]: array([ True, False, True, False], dtype=bool)

data
Out[73]:
array([[ 0.36762706, -1.55668952, 0.84316735, -0.116842 ],
[ 1.34023966, 1.12766186, 1.12507441, -0.68689309],
[ 1.27392366, -0.43399617, -0.80444728, 1.60731881],
[ 0.23361565, 1.38772715, 0.69129479, -1.19228023],
[ 0.51353082, 0.17696698, -0.06753478, 0.80448168],
[ 0.21773096, 0.60582802, -0.46446071, 0.83131122],
[ 0.50569072, 0.04431685, -0.69358155, -0.9629124 ]])

data[data < 0] = 0

data
Out[75]:
array([[ 0.36762706, 0. , 0.84316735, 0. ],
[ 1.34023966, 1.12766186, 1.12507441, 0. ],
[ 1.27392366, 0. , 0. , 1.60731881],
[ 0.23361565, 1.38772715, 0.69129479, 0. ],
[ 0.51353082, 0.17696698, 0. , 0.80448168],
[ 0.21773096, 0.60582802, 0. , 0.83131122],
[ 0.50569072, 0.04431685, 0. , 0. ]])

上面展示通过布尔值来设置值的手段。

数组文件输入输出
在跑实验时经常需要用到读取文件中的数据,其实在numpy中已经有成熟函数封装好了可以使用

将数组以二进制形式格式保存到磁盘,np.save 、np.load 函数是读写磁盘的两个主要函数,默认情况下,数组以未压缩的原始二进制格式保存在扩展名为.npy的文件中

arr = np.arange(10)
np.save(‘some_array’,arr)

np.load(‘some_array.npy’)
Out[80]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
存取文本文件:
文本中存放是聚类需要数据,直接可以方便读取到numpy array中,省去一行行读文件繁琐。

arr = np.loadtxt(‘dataMatrix.txt’,delimiter=’ ')

arr
Out[82]:
array([[ 1. , 1. , 1. , 1. , 1. ,
0.8125 ],
[ 0.52882353, 0.56271186, 0.48220588, 0.53384615, 0.61651376,
0.58285714],
[ 0. , 0. , 0. , 1. , 1. ,
1. ],
[ 1. , 0.92857143, 0.91857143, 1. , 1. ,
1. ],
[ 1. , 1. , 1. , 1. , 1. ,
1. ],
[ 0.05285714, 0.10304348, 0.068 , 0.06512821, 0.05492308,
0.05244898],
[ 0.04803279, 0.08203125, 0.05516667, 0.05517241, 0.04953488,
0.05591549],
[ 0.04803279, 0.08203125, 0.05516667, 0.05517241, 0.04953488,
0.05591549]])

np.savetxt 执行相反的操作,这两个函数在跑实验加载数据时可以提供很多便利!!!

使用numpy.arange方法

>>> print np.arange(15)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
>>> print type(np.arange(15))
<type 'numpy.ndarray'>
>>> print np.arange(15).reshape(3,5)
[[ 0  1  2  3  4][ 5  6  7  8  9][10 11 12 13 14]]
>>> print type(np.arange(15).reshape(3,5))
<type 'numpy.ndarray'>使用numpy.linspace方法例如,在从1到10中产生20个数:>>> print np.linspace(1,10,20)
[  1.           1.47368421   1.94736842   2.42105263   2.894736843.36842105   3.84210526   4.31578947   4.78947368   5.263157895.73684211   6.21052632   6.68421053   7.15789474   7.631578958.10526316   8.57894737   9.05263158   9.52631579  10.        ]

使用numpy.zeros,numpy.ones,numpy.eye等方法可以构造特定的矩阵

>>> print np.zeros((3,4))
[[ 0.  0.  0.  0.][ 0.  0.  0.  0.][ 0.  0.  0.  0.]]
>>> print np.ones((3,4))
[[ 1.  1.  1.  1.][ 1.  1.  1.  1.][ 1.  1.  1.  1.]]
>>> print np.eye(3)
[[ 1.  0.  0.][ 0.  1.  0.][ 0.  0.  1.]]

获取数组的属性:

>>> a = np.zeros((2,2,2))
>>> print a.ndim   #数组的维数
3
>>> print a.shape  #数组每一维的大小
(2, 2, 2)
>>> print a.size   #数组的元素数
8
>>> print a.dtype  #元素类型
float64
>>> print a.itemsize  #每个元素所占的字节数
8
Memory layout
The following attributes contain information about the memory layout of the array:

ndarray.flags Information about the memory layout of the array.
ndarray.shape Tuple of array dimensions.
ndarray.strides Tuple of bytes to step in each dimension when traversing an array.
ndarray.ndim Number of array dimensions.
ndarray.data Python buffer object pointing to the start of the array’s data.
ndarray.size Number of elements in the array.
ndarray.itemsize Length of one array element in bytes.
ndarray.nbytes Total bytes consumed by the elements of the array.
ndarray.base Base object if memory is from some other object.
Array methods
An ndarray object has many methods which operate on or with the array in some fashion, typically returning an array result. These methods are briefly explained below. (Each method’s docstring has a more complete description.)

For the following methods there are also corresponding functions in numpy: all, any, argmax, argmin, argpartition, argsort, choose, clip,compress, copy, cumprod, cumsum, diagonal, imag, max, mean, min, nonzero, partition, prod, ptp, put, ravel, real, repeat, reshape, round,searchsorted, sort, squeeze, std, sum, swapaxes, take, trace, transpose, var.

更多Array的相关方法见:http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html

用到比较多函数示例:

x
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],

   [[ 9, 10, 11],[12, 13, 14],[15, 16, 17]],[[18, 19, 20],[21, 22, 23],[24, 25, 26]]])

x.sum(axis=1)
array([[ 9, 12, 15],
[36, 39, 42],
[63, 66, 69]])

x.sum(axis=2)
array([[ 3, 12, 21],
[30, 39, 48],
[57, 66, 75]])

np.sum([[0, 1], [0, 5]])
6

np.sum([[0, 1], [0, 5]], axis=0)
array([0, 6])

np.sum([[0, 1], [0, 5]], axis=1)
array([1, 5])

合并数组

使用numpy下的vstack(垂直方向)和hstack(水平方向)函数:

a = np.ones((2,2))
b = np.eye(2)
print np.vstack((a,b))
[[ 1. 1.]
[ 1. 1.]
[ 1. 0.]
[ 0. 1.]]

print np.hstack((a,b))
[[ 1. 1. 1. 0.]
[ 1. 1. 0. 1.]]

看一下这两个函数有没有涉及到浅拷贝这种问题:

c = np.hstack((a,b))
print c
[[ 1. 1. 1. 0.]
[ 1. 1. 0. 1.]]

a[1,1] = 5
b[1,1] = 5
print c
[[ 1. 1. 1. 0.]
[ 1. 1. 0. 1.]]

通过上面可以知道,这里进行是深拷贝,而不是引用指向同一位置的浅拷贝。

深拷贝数组

数组对象自带了浅拷贝和深拷贝的方法,但是一般用深拷贝多一些:

a = np.ones((2,2))
b = a
b is a
True

c = a.copy() #深拷贝
c is a
False

基本的矩阵运算

转置:

a = np.array([[1,0],[2,3]])
print a
[[1 0]
[2 3]]

print a.transpose()
[[1 2]
[0 3]]

numpy.linalg模块中有很多关于矩阵运算的方法:

特征值、特征向量:

a = np.array([[1,0],[2,3]])

nplg.eig(a)
(array([ 3., 1.]), array([[ 0. , 0.70710678],
[ 1. , -0.70710678]]))

Python Numpy介绍相关推荐

  1. python软件界面-python软件界面介绍(python软件介绍)

    python软件界面介绍 1.接口初始化 当我们使用pycharm工具时,我们将遇到的第一个问题是,在进行各种配置后界面变得混乱时,我们该怎么办?我们应该还原,那么如何还原初始设置? 尽管此工具是从e ...

  2. python语言介绍-00-python语言介绍

    以下为摘录的python的介绍 Python是一种解释型语言.这就是说,与C语言和C的衍生语言不同,Python代码在运行之前不需要编译.其他解释型语言还包括PHP和Ruby. Python是动态类型 ...

  3. Matlab和Python(Numpy,Scipy)与Lapack的关系

    说到数值计算,可能许多人都能立马想到Matlab.Matlab多年的持续影响力已经让它成为许多人心中科学计算的代名词.但它底层一个重要的库Lapack却很少有人知道. 而Python年龄比Matlab ...

  4. 2.16 关于 Python Numpy 的说明-深度学习-Stanford吴恩达教授

    ←上一篇 ↓↑ 下一篇→ 2.15 Python 中的广播 回到目录 2.17 Jupyter/iPython 笔记本的快速指南 关于 Python Numpy 的说明 (A Note on Pyth ...

  5. python numpy.ones(shape, dtype=None, order=’C’) empty(shape[, dtype, order]) empty_like(a) 空数组 全1数组

    ones(shape[, dtype, order]) 依据给定形状和类型(shape[, dtype, order])返回一个新的元素全部为1的数组. def ones(shape, dtype=N ...

  6. python 初始化数组 numpy,Python Numpy 数组的初始化和基本操作

    Python 是一种高级的,动态的,多泛型的编程语言.Python代码很多时候看起来就像是伪代码一样,因此你可以使用很少的几行可读性很高的代码来实现一个非常强大的想法. 一.基础: Numpy的主要数 ...

  7. python numpy array转置_Python numpy数组转置与轴变换

    这篇文章主要介绍了Python numpy数组转置与轴变换,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 矩阵的转置 >>> im ...

  8. [转载] python numpy 总结

    参考链接: Python中的numpy.compress 先决条件 在阅读这个教程之前,你多少需要知道点python.如果你想重新回忆下,请看看Python Tutorial. 如果你想要运行教程中的 ...

  9. [转载] Python Numpy

    参考链接: 在Python中使用Numpy在单行中将两个矩阵相乘 NumPy介绍 NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算, ...

最新文章

  1. PostgreSQL:Java使用CopyManager实现客户端文件COPY导入
  2. 十二、Redis五大数据类型之四Hash
  3. 练习系列 - 5、求子数组的最大和
  4. SQL Server 的 Statistics 簡介
  5. php 图片合成,PHP中多张图片合成一张图片例子
  6. MySQL应用安装_mysql安装和应用
  7. idea历史版本下载
  8. 网站随机动态密码代码
  9. 异步 HttpContext.Current实现取值的方法(解决异步Application,Session,Cache...等失效的问题)...
  10. 手机安装python3.5_CentOS 7安装Python3.5
  11. 计算机科学 高中研究项目,高中信息科技教学中渗透计算机科学史的实践研究...
  12. file input 移动端选择文件夹_免费 |《MNN For Swift》移动端机器学习实战课程
  13. 转换字符串的字符成单个字符并用逗号分隔
  14. python中的if语句
  15. vue语音播放通知功能
  16. 吴式太极大师战波简介
  17. win10计算机亮度无法调节,win10电脑调不了亮度怎么办?教你win10电脑调不了亮度处理方法...
  18. 苹果v10模板需要几的php,苹果MACCMSv10源码模板安装常见问题
  19. Comparable与Comparator的再学习与思考
  20. 2020ciscn 部分二进制WP(持续更新)

热门文章

  1. PyTorch 训练加速
  2. python Manager dict
  3. Python取top N相关的模块:heapq模块
  4. 总结Hbase 与 MongoDB
  5. kcf跟踪算法实例整理
  6. 青龙羊毛——火源星球新脚本(搬运)
  7. java 串口波特率_JAVA串口通信的方法
  8. python 多态 知乎_Python函数接口的一些设计心得
  9. 计算机网络最提出的优点是什么,2013年计算机一级B考试模拟试题十八及答案解析...
  10. java异常详细讲解_java异常的讲解