1、Numpy是什么
很简单,Numpy是Python的一个科学计算的库,提供了矩阵运算的功能,其一般与Scipy、matplotlib一起使用。其实,list已经提供了类似于矩阵的表示形式,不过numpy为我们提供了更多的函数。如果接触过matlab、scilab,那么numpy很好入手。 在以下的代码示例中,总是先导入了numpy:
>>> import numpy as np
>>> print np.version.version
1.6.2<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-size: 14px; font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; line-height: 25.2000007629395px; background-color: rgb(247, 252, 255);"><strong>2、多维数组</strong></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-size: 14px; font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; line-height: 25.2000007629395px; background-color: rgb(247, 252, 255);">多维数组的类型是:numpy.ndarray。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-size: 14px; font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; line-height: 25.2000007629395px; background-color: rgb(247, 252, 255);"><strong>使用numpy.array方法</strong></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-size: 14px; font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; line-height: 25.2000007629395px; background-color: rgb(247, 252, 255);">以list或tuple变量为参数产生一维数组:</p><pre name="code" class="python">>>> 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变量为元素产生二维数组
>>> print np.array([[1,2],[3,4]])
[[1 2]
[3 4]]生成数组的时候,可以指定数据类型,例如numpy.int32, numpy.int16, and numpy.float64等:
使用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到3中产生9个数
>>> print np.linspace(1,3,9)
[ 1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. ]使用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.]]创建一个三维数组:>>> print np.zeros((2,2,2))
[[[ 0. 0.]
[ 0. 0.]]
[[ 0. 0.]
[ 0. 0.]]]数组索引,切片,赋值
>>> a = np.array( [[2,3,4],[5,6,7]] )
>>> print a
[[2 3 4]
[5 6 7]]
>>> print a[1,2]
7
>>> print a[1,:]
[5 6 7]
>>> print a[1,1:2]
[6]
>>> a[1,:] = [8,9,10]
>>> print a
[[ 2 3 4]
[ 8 9 10]]*************数组的加减乘除:
>>> print a > 2
[[False False]
[False False]]
>>> print a+b
[[ 2. 1.]
[ 1. 2.]]
>>> print a-b
[[ 0. 1.]
[ 1. 0.]]
>>> print b*2
[[ 2. 0.]
[ 0. 2.]]
>>> print (a*2)*(b*2)
[[ 4. 0.]
[ 0. 4.]]
>>> print b/(a*2)
[[ 0.5 0. ]
[ 0. 0.5]]
>>> print (a*2)**4
[[ 16. 16.]
[ 16. 16.]]合并数组
>>> 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]]特征值、特征向量:
>>> print nplg.eig(a)
(array([ 3., 1.]), array([[ 0. , 0.70710678],
[ 1. , -0.70710678]]))
本文介绍了Numpy库的基础知识,包括如何使用Numpy进行数组操作、矩阵运算等,并提供了丰富的代码示例。
1339

被折叠的 条评论
为什么被折叠?



