码迷,mamicode.com
首页 > 编程语言 > 详细

python数据分析工具包(1)——Numpy(一)

时间:2018-02-22 21:26:37      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:size   nbsp   ast   its   res   建议   cap   desktop   tools   

    在本科阶段,我们常用的科学计算工具是MATLAB。下面介绍python的一个非常好用而且功能强大的科学计算库——Numpy。

  • a powerful N-dimensional array object(一个强大的N维数组对象)
  • sophisticated (broadcasting) functions (先进的(广播?)函数)
  • tools for integrating C/C++ and Fortran code(集成的C / C++和Fortran代码工具)
  • useful linear algebra, Fourier transform, and random number capabilities(有用的线性代数,傅立叶变换和随机数能力)

    以上是官方文档的介绍,具体资料可以按参考这个网站: http://www.numpy.org/

    依旧是pip install numpy安装这个包。我们在ipython中举一些例子来学习它的一些常用操作。当然在此之前,建议先了解一下矩阵等相关的数学知识,就当温习一下大学的线代高数部分了。

 1 #导入numpy
 2 >>> import numpy as np
 3 #生成一个指定内容的数组
 4 >>> a = np.arange(15).reshape(3, 5)
 5 >>> a
 6 array([[ 0,  1,  2,  3,  4],
 7        [ 5,  6,  7,  8,  9],
 8        [10, 11, 12, 13, 14]])
 9 >>> a.shape     #数组行列数
10 (3, 5)
11 >>> a.ndim    #数组维度
12 2
13 >>> a.dtype.name  #数组中元素类型
14 int64
15 >>> a.itemsize   #数组中每个元素的字节大小
16 8
17 >>> a.size   #数组元素的总数
18 15
19 >>> type(a)  #输出a的属性
20 <type numpy.ndarray>
21 #直接给定元素生成数组
22 >>> b = np.array([6, 7, 8])
23 >>> b
24 array([6, 7, 8])
25 >>> type(b)
26 <type numpy.ndarray>

 

    numpy可以生成指定的数组。

 

 1 C:\Users\Administrator\Desktop
 2 λ ipython
 3 Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)]
 4 Type copyright, credits or license for more information
 5 IPython 6.2.1 -- An enhanced Interactive Python. Type ? for help.
 6 
 7 In [1]: import numpy as np
 8 
 9 In [2]: a=np.zeros([3,4])      #生成全零阵
10 
11 In [3]: a
12 Out[3]:
13 array([[0., 0., 0., 0.],
14        [0., 0., 0., 0.],
15        [0., 0., 0., 0.]])
16 
17 In [4]: b=np.ones([3,4])   #生成全1阵
18 
19 In [5]: b
20 Out[5]:
21 array([[1., 1., 1., 1.],
22        [1., 1., 1., 1.],
23        [1., 1., 1., 1.]])
24 
25 In [6]: c=np.random.rand(3,4)     #生成随机阵
26 
27 In [7]: c
28 Out[7]:
29 array([[0.36417168, 0.24336724, 0.78826727, 0.42894367],
30        [0.77198615, 0.95897315, 0.25628233, 0.53995372],
31        [0.02777746, 0.25093856, 0.14544893, 0.10475779]])
32 
33 In [8]: d=np.eye(3)     #生成单位阵
34 
35 In [9]: d
36 Out[9]:
37 array([[1., 0., 0.],
38        [0., 1., 0.],
39        [0., 0., 1.]])
40 
41 In [10]: e=np.mat([[1,2,3],[4,5,6],[7,8,9]])   #矩阵化
42 
43 In [11]: e
44 Out[11]:
45 matrix([[1, 2, 3],
46         [4, 5, 6],
47         [7, 8, 9]])
48 
49 In [12]: f=np.power(e,2)    #计算N次幂
50 
51 In [13]: f
52 Out[13]:
53 matrix([[ 1,  4,  9],
54         [16, 25, 36],
55         [49, 64, 81]], dtype=int32)
56 
57 In [14]: g=f.T     #求转置矩阵
58 
59 In [15]: g
60 Out[15]:
61 matrix([[ 1, 16, 49],
62         [ 4, 25, 64],
63         [ 9, 36, 81]], dtype=int32)
64 
65 In [16]:

    下面对array()和mat()做一个区分。初学者很容易混淆。

    np.array(a)  是将列表数组化, 它与另一个narray的乘法并不是按照矩阵乘法进行的,而是对应元素相乘 。而mat(),在上面的例子可以清楚地看出来,他生成的对象是一个matrix。即将数组矩阵化。对矩阵使用shape()方法,会返回矩阵的维度,而数组则会返回它的行和列。详细资料可以参考官方文档。

python数据分析工具包(1)——Numpy(一)

标签:size   nbsp   ast   its   res   建议   cap   desktop   tools   

原文地址:https://www.cnblogs.com/CCColby/p/8457733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!