zoukankan      html  css  js  c++  java
  • Python For Data Analysis -- NumPy

    NumPy作为python科学计算的基础,为何python适合进行数学计算,除了简单易懂,容易学习

    Python可以简单的调用大量的用c和fortran编写的legacy的库

    Python科学计算的这几个库,单独安装还是蛮麻烦的,所以推荐这个包

    http://www.continuum.io/downloads#all

    conda list #查看所有的可安装包  
    conda install wxpython #安装  
    conda install pyqt #安装  
    conda update ipython #升级

    轻松安装

     

    The NumPy ndarray: A Multidimensional Array Object

    ndarray,可以理解为n维数组,用于抽象矩阵和向量

    Creating ndarrays

    最简单的就是,从list初始化,

    image

    当然还有其他的方式,比如,

    image

    image

    汇总,

    image

    image 

     

    Data Types for ndarrays

    首先对于ndarray只能存放同一类型数据,

    image

    并且由于封装了c和fortran的库,大家的类型必须要统一,所以ndarrays支持如下类型,
    那么各种语言中的类型,都会统一对应到这些类型

    image

    image

    ndarray支持显式的类型转换 (copy)

    int转float:

    image

    string转float:

    image 

    这个比较有用,并且可以看到这里的类型写的float,NumPy会自动将python的类型转成ndarray支持的类型

     

    Operations between Arrays and Scalars

    对于ndarray里面的elems的操作,是不需要自己写for的
    默认对于ndarray或shape相同的ndarray之间的操作,都是会遍历每个element的,称为vectorization,向量化

    image image

     

    Basic Indexing and Slicing (View)

    取出矩阵中的某些数据,或切分出子矩阵

    对于一维向量,和python list操作基本是一致的,最大的区别,是ndarray的slicing不会copy,而是view,即你更改slicing,就相当于更改了原始数据

    image 

    image
    image

    可以看到更改arr_slice同样会影响到arr,这样做的原因是,由于经常会操作很大的矩阵,copy会低效,所以默认是不copy的
    当然你可以显式的copy,arr[5:8].copy()

    二维的,参考下图,

    image

     

    Boolean Indexing

    这个比较有特点,
    对于普通的index,arr[2],这里是指定index第二个
    如果我要index多个,并且不连续,怎么办?

    其实你可以用一个boolean indexing来一一指定是否需要取出

    比如,arr[[True, False, True,False]],注意boolean indexing必须是numpy.array, numpy.matrix也不行,必须getA转成array

    对于Numpy中有matrix类的定义,比较坑爹,会混淆

    一般不会手工参数这样的boolean indexing,会通过一些条件判断得到

    image

    image

    image

    image

    然后你把boolean indexing代入arr就可以取出标为True那维数据

    image
    所以必须保证boolean indexing的维数和矩阵中的对应的维数是一样的
    比如,
    arr[[True, False, True,False]]
    必须保证arr是4行的

    其实每一维都是可以加上条件过滤的,比如,行选names==Bobs,列选第3列

    image

    除了在维度级别进行选择,还能对每个elem进行过滤
    比如把data中,所有小于0的,都设为0

    image 

     

    Fancy Indexing

    Fancy indexing is a term adopted by NumPy to describe indexing using integer arrays.
    和普通的indexing, arr[3], 不同在于,可以指定多个,并且按照指定的顺序返回

    image

    选取第4,3,0,6行

    image

    注意底下两种的区别,
    相当于,取(1, 0), (5, 3), (7, 1), and (2, 2)

    image
    行,选取1,5,7,2
    列,全选,换顺序

    image

     

    Transposing Arrays and Swapping Axes

    转置,transposing

    arr.T

    image

    其实转置是swapaxes的特殊版本,这个可以指定swap哪两个维度

    arr.swapaxes(0, 1)

     

    Universal Functions: Fast Element-wise Array Functions

    这个上面在基本ndarray计算的时候已经介绍过,这里汇总一下
    这种vectorized操作分为一元和二元的,

    image

    image

    image

     

    Data Processing Using Arrays

    Expressing Conditional Logic as Array Operations

    vectorized可以用于简化for循环,那么if-else可以简化吗?

    numpy.where function is a vectorized version of the ternary expression x if condition else y

    np.where(cond, xarr, yarr) 等同于 cond?xarr:yarr

    image

    image

    并且,这个还可以嵌套,即如果if…elseif…..elseif……else…

    image

     

    Mathematical and Statistical Methods

    image

     

    Methods for Boolean Arrays

    image

    any和all

    image

     

    Unique and Other Set Logic

    image

     

    File Input and Output with Arrays

    Storing Arrays on Disk in Binary Format (.npy)

    image

    image

    压缩存储,并指定别名 (.npz)

    image

    image

    Saving and Loading Text Files

    image

    image

     

     

    Linear Algebra

    image

     

    Random Number Generation

    image

  • 相关阅读:
    .Net环境下的缓存技术介绍
    JavaScript 全局对象
    JavaScript escape() 函数
    实现DIV拖动
    巧用Ajax的beforeSend 提高用户体验
    js中ie8下不识别indexOf的解决办法
    页面弹窗效果HTML
    让html页面中的文字不可选中
    MVC路由规则
    C# Math.Round
  • 原文地址:https://www.cnblogs.com/fxjwind/p/3905534.html
Copyright © 2011-2022 走看看