NumPy手册

来源:互联网 发布:qt多线程网络编程 编辑:程序博客网 时间:2024/06/05 17:08
NumPy(Numerical Python的简称)是高性能科学 和数据分析的基础包。

其重要功能如下: 
1. ndarray,一个具有矢量运算和复杂广播能力的快速且节省空间的多维数组。 
2. 用于对数组数据进行快速运算的标准数学函数(无需编写循环)。 
3. 线性代数、随机数生成以及傅里叶变换功能。

import numpy as np

1.数据创建函数

Demo:

data = [1.1,2.2,3.3]data = np.array(data)print data                           #[ 1.1  2.2  3.3]print data.dtype                     #float64print data.shape                     #(3L,)data = data*10                       #不同于python里面列表的运算print data                           #[ 11.  22.  33.]data = data.astype(np.int16)print data.dtype                     #int16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2.元素级数组函数

一元ufunc:


Demo:

arr = np.arange(10)print arr           #[0 1 2 3 4 5 6 7 8 9]print np.sqrt(arr)  #[ 0. 1. 1.41421356 1.73205081 2. 2.23606798  2.44948974 2.64575131 2.82842712 3.]
  • 1
  • 2
  • 3
  • 4

二元ufunc:

Demo:

x = np.random.randn(8)y = np.random.randn(8)print xprint yprint np.maximum(x,y)#输出[  6.44384037e-01   4.77467240e-01  -1.63710500e+00  -2.23397938e-01   2.42823340e-04   2.90417057e-01   1.59116209e+00  -9.84905419e-01][-0.83284853  1.37952239  0.21118756 -0.17092715  0.01228589  1.54071412  1.44245279  0.63661402][ 0.64438404  1.37952239  0.21118756 -0.17092715  0.01228589  1.54071412  1.59116209  0.63661402]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3.基本数组统计方法


Demo:

x = np.random.randint(1,10,10)print x                         #[2 8 2 8 7 3 2 3 3 2]print np.argmax(x)              #1  最大值为8,下标为1
  • 1
  • 2
  • 3

4.数据的集合运算

5.常用的numpy.linalg函数

6.部分numpy.random函数


7.数组连接函数

8.take、where、copy

(1)take:

arr = np.arange(10)*100inds = [7,1,2,6]print arr.take(inds)      #[700 100 200 600]
  • 1
  • 2
  • 3

(2)where:

numpy.where函数是三元表达式x if condition else y的矢量化版本。

arr = np.random.randn(4,4)print arr[[ 1.19717763 -0.741477   -2.15110668 -0.46468596] [-1.13466991  1.10899501 -1.61028763 -0.6177533 ] [ 1.95222784 -0.00466218 -1.23314358  0.75340766] [ 0.24350204 -0.23099947  0.11610512  0.21914577]]print np.where(arr < 0, 0, arr)[[ 1.19717763  0.          0.          0.        ] [ 0.          1.10899501  0.          0.        ] [ 1.95222784  0.          0.          0.75340766] [ 0.24350204  0.          0.11610512  0.21914577]]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

(3)copy:

data_copy = data.copy() # 可以得到一份副本,并不影响原来的数据

参考文献 
(1)利用python进行数据分析

原创粉丝点击