Python数据分析学习笔记一

来源:互联网 发布:仿淘宝购物车的htmlcss 编辑:程序博客网 时间:2024/03/29 14:04

一、NumPy初识(1)

NumPy是python用于科学计算的一个重要的组成部分,可以使用IPython直接使用,或在程序中进行import numpy导入

1、NumPy数组创建和获取

NumPy数组一般要求是同一种类型的,但是也有例外;快速生成某一个范围内的数组:numpy.arange(10);可以生成从0开始到9的数组获取数组的数据类型:    a = numpy.arange(10)    print a.dtype    结果是:int64确定数组维度:    print a.shape    结果是:(10,)    在一维数组中,输出的是数组元素的个数创建多维数组:    b = numpy.array([numpy.arange(2),numpy.arange(2)])    print m    结果是:[          [0 1]          [0 1]          ]    在这里使用bumpy中的arange创建了两个一位数组,并将它们当作参数传递给了array函数中,并生成一个二维数组。    **因为每次都要写numpy.这样的语法,所以可以使用import numpy as np简化.以后在程序中就可以使用np来代替numpy了.**获取m数组的维度:print m.shape    输出:(2,2)    需要注意的是,获取的维度是Python的元组(tuple)选取数组元素:    首先需要注意的是,在Python中跟其他语言总一样,数组的下标计数也都是从0开始的;    m = np.array(np.arange*(2),np.arange(2))    print a[0,1]    输出为: 1

2、NumPy数组的类型

NumPy在Python原有基础上,增加了许多种数据类型,如下图:![NumPy中支持的数据类型](http://img.blog.csdn.net/20160427213825187)指定数据类型:    a = np.arange(10,dtype=np.uint16)自定义数据类型:    自定义数据类型是一种异构数据类型,可以当做电子表格或数据库中的一行数据的结构。例:t = np.dtype([('name',np.str_,40),('numitems',np.int32),('price',np.float32)])    在用array创建数组时,如果没有指定数据类型的时候,会默认为浮点数,而使用自定义数据类型的时候,则必须指定数据类型,否则将会报错,例:    item = np.array([('Meaning of live DVD',42,32.1),('Butter',65,12.8)],dtype=t)    print item[1]    输出为:('Butter', 65, 12.800000190734863)  

3、一维数组的切片

NumPy中的数组切片根Python中的数组切片操作很像:选取指定范围内的数据:    t = np.arange(10)    sub = t[3:7]    print sub    输出为:[3 4 5 6]使用下标0~7之间的数据,步长为2进行数组的截取:    t = np.arange(10)    sub = t[0:7:2]    print sub    输出为:[0 2 4 6]使用负数反转数组:    t = np.arange(10)    sub = t[::-1]    print sub    输出为:[9 8 7 6 5 4 3 2 1 0]

4、改变数组维度:

使用reshape函数进行数组维度的重新指定:    原来数组为使用arange函数生成的有10个元素的一维数组,使用reshape函数使之变为2行5列的二维数组:     t = np.arange(10).reshape(2,5)    print t    输出为:[[0 1 2 3 4]           [5 6 7 8 9]]使用ravel函数将数组摊平,以上例中最终产生的二维数组为例:    t = np.arange(10).reshape(2,5)    print t.ravel()    print t    输出为:[0 1 2 3 4 5 6 7 8 9]            [[0 1 2 3 4]            [5 6 7 8 9]]    由输出结果可以看出ravel函数会展开数组,但是不会改变数组本身,这也是NumPy中对于数组大部分的操作会出现的一种情况使用flatten函数展开数组:    t = np.arange(10).reshape(2,5)    print t.flatten()    print t    输出为:[0 1 2 3 4 5 6 7 8 9]            [[0 1 2 3 4]            [5 6 7 8 9]]    可以看出,flatten的功能与ravel相似,不过flatten会申请内存来接收结果使用元组进行数组的维度改变:    t = np.arange(12).reshape(3,4)    print t    t.shape = (2,6)    print t    输出为:    第一次打印:[[ 0  1  2  3]              [ 4  5  6  7]              [ 8  9 10 11]]    第二次打印:            [[ 0  1  2  3  4  5]            [ 6  7  8  9 10 11]]使用transpose进行数组矩阵变换:    t = np.arange(24).reshape(3,8)    t = t.transpose()    print t    输出为:        [[ 0  8 16]        [ 1  9 17]        [ 2 10 18]        [ 3 11 19]        [ 4 12 20]        [ 5 13 21]        [ 6 14 22]        [ 7 15 23]]     使用resize()来进行数组的维度变换:    t = np.arange(24).reshape(2,12)    print t    t.resize((4,6))    print t    输出为:    第一个打印:    [[ 0  1  2  3  4  5  6  7  8  9 10 11]     [12 13 14 15 16 17 18 19 20 21 22 23]]    第二个打印:      [[ 0  1  2  3  4  5]    [ 6  7  8  9 10 11]    [12 13 14 15 16 17]    [18 19 20 21 22 23]]结果可以看出,resize()其实跟reshape的作用是一致的,但是resize()会改变数组本身的组成
0 0
原创粉丝点击