NumPy常见函数和使用示例


array
使用方法:
Type code   CType            Minimum size in bytes
        'c'        character                     1
        'b'        signed integer              1
        'B'        unsigned integer        1
        'u'        Unicode character       2
        'h'        signed integer            2
        'H'        unsigned integer         2
        'i'        signed integer            2
        'I'        unsigned integer         2
        'l'        signed integer            4
        'L'        unsigned integer         4
        'f'        floating point              4
        'd'        floating point              8

 1. 数组的基本操作

from array import *
#创建一个integer类型的数组
myarr = array("l")  #创建数组
myarr.append(3)  #追加元素
myarr.append(1)
myarr.append(8)
#删除最后一个
myarr.pop()
#删除第一个指定的X
myarr.remove(x)
#通过下标取数组的值

num1 = myarr[0]  #第一个值
#在指定位置插入值  
myarr.insert(6,10)

#数组反序
myarr.reverse()

2. NumPy常见函数和使用示例

Python Code:

 demo.py


import numpy as np 

from numpy.matlib import randn 

if __name__ == '__main__': 

    data1 = [6, 7.5, 8, 0 ,1] 

    # 创建一维数组

    arr1 = np.array(data1) 

    print(arr1) 

#创建二维数组

    data2 = [[1, 2, 3, 4], [5, 6, 7, 8]] 

    arr2 = np.array(data2) 

print(arr2) 

    # Type of data in array. 

    print(arr1.dtype) #输出第一个数组的数据类型float64

    print(arr2.ndim) #输出第二个数组的维数 2

    print(arr2.shape) #输出第三个数组的形状(2L, 4L)

#用zeros函数创建数组

    print(np.zeros(10)) #一维数组10个元素都是0

print(np.zeros((3, 6))) #二维数组3行6列都是0

#用Empty函数创建数组,其初始值为乱值

    # Empty array will contain uninitialized garbage values. 

    print(np.empty((2, 3, 2))) #三维数组

#用arrange函数创建数组

    # arange is an array-valued version of the built-in Python range function: 

    print(np.arange(15)) #输出一维数组:[0 1 23 4 5 6 7 8 9 10 11 12 13 14]

 

#同一数组,不同类型

    arr3 = np.array([1, 2, 3], dtype=np.float64) 

    arr4 = np.array([1, 2, 3], dtype=np.int32) 

    print(arr3.dtype) 

print(arr4.dtype) 

 

    # You can explicitly convert or cast an array from one dtype to another using ndarray’s astype method: 

    arr5 = np.array([1, 2, 3, 4, 5]) 

    print(arr5.dtype) #int32

    arr6 = arr5.astype(np.float64) #将数组5的数据类型转换为float64

    print(arr6.dtype) #float64

#数组间以及数组与数之间的运算

    # Operations between Arrays and Scalars 

    arr = np.array([[1., 2., 3.], [4., 5., 6.]]) 

    print(arr * arr) #相应元素相乘,输出为[[1. 4. 9.] [16. 25. 36.]]

    print(arr - arr) #相应元素相减,输出为[[0. 0. 0.] [0. 0. 0.]]

   #创建二维数组,取值,修改值

arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 

print(arr2d[2]) #数组标号从0开始,输出第三行元素:[7 8 9]

#创建三维数组

    arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) #2个二维数组

old_values = arr3d[0].copy() #将第一个二维数组拷贝至old_values 

print(old_values) #输出:[[1 2 3], [4 5 6]]

    print('------------') 

    arr3d[0] = 42 #将第一个二维数组的元素都修改为42

    print(arr3d) #输出[[[42 42 42], [4242 42]], [[7 8 9], [10 11 12]]]

    arr3d[0] = old_values 

    print(arr3d) # 输出 [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Let’s consider an example where we have some data in an array and an array of names with duplicates. 

#创建一维数组,含7个名字

    names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe']) 

print(names) 

#用随机函数创建二维数组,7行4列,

    data = randn(7, 4) 

    print(data) 

 

    # Boolean Indexing 

    # Suppose each name corresponds to a row in the data array. If we wanted to select all 

    # the rows with corresponding name 'Bob'. Like arithmetic operations, comparisons 

    # (such as ==) with arrays are also vectorized. Thus, comparing names with the string 

# 'Bob' yields a boolean array: 

#data数组中的7行分别对应names数组中的每一个名字;

    print(names == 'Bob') #判断names数组中的每一个元素是否等于Bob, 输出 [True False False True FalseFalse False]

    print(data[names == 'Bob']) #输出data中判断为true的行元素,这里是第一行和第四行。

    print(data[names == 'Bob', 2:],) #输出data中判断为true所在的行,及第3列第4列的所有元素;

    print(data[names == 'Bob', 3]) #输出data中判断为true所在的行及第3列的元素

    print(data[((names == 'Bob') | (names == 'Will'))]) #输出名字为Bob以及Will所在的行元素

    # Fancy Indexing 

    # Fancy indexing is a term adopted by NumPy to describe indexing using integer arrays. 

arr = np.empty((8, 4)) #二维数组

#range(8)即 [0, 1, 2, 3, 4, 5, 6, 7]

    for i in range(8): 

        arr[i] = i #第i行的数字都为1;

    print(arr)  # [[0.  0. 0.  0.] [ 1.  1. 1.  1.] [ 2.  2. 2.  2.] [ 3.  3. 3.  3.] [ 4.  4. 4.  4.] [ 5.  5. 5.  5.] [ 6.  6. 6.  6.] [ 7.  7. 7.  7.]]

#给数组传入1个指数数组,则选出对应的行元素;这里4即第四行  

 print('To select out a subset of the rows in a particular order, you can simply pass a list or ndarray of integers specifying the desired order:') 

print(arr[[4, 3, 0, 6]]) #[[ 4.  4. 4.  4.] [ 3.  3. 3.  3.] [ 0.  0. 0.  0.] [ 6.  6. 6.  6.]]

#用负数表示从末尾开始选择行,-1为最后一行,-3为倒数第三行

    print('Hopefully this code did what you expected! Using negative indices select rows from the end:') 

print(arr[[-3, -5, -7]]) # [[ 5.  5.  5.  5.][ 3.  3. 3.  3.] [ 1.  1. 1.  1.]]

#多个指数数组作为参数时,选出一维数组

    print('Passing multiple index arrays does something slightly different; it selects a 1D array of elements corresponding to each tuple of indices:') 

    arr = np.arange(32).reshape((8, 4)) #[[ 0  1 2  3] [ 4  5 6  7] [ 8  9 10 11] [12 13 14 15] [16 17 18 19] [20 2122 23] [24 25 26 27] [28 29 30 31]]

    print(arr[[1, 5, 7, 2], [0, 3, 1, 2]]) #[ 4 23 29 10]

  #输出矩阵转置

    print('Transposing is a special form of reshaping which similarly returns a view on the underlying data without copying anything.') 

    arr = np.arange(15).reshape((3, 5)) 

    print(arr.T) 

    print('''A universal function, or ufunc, is a function that performs elementwise operations on 

data in ndarrays. You can think of them as fast vectorized wrappers for simple functions 

that take one or more scalar values and produce one or more scalar results.''') 

    arr = np.arange(10) 

    print(np.sqrt(arr)) #仍是一维数组,求数组中的每个元素求平方根

    print(np.exp(arr)) #仍是一维数组,求以e为底,数组中的每个元素作为指数的值

    x = randn(8) #生成8个随机数

    y = randn(8) 

    print(x) 

    print(y) 

    print(np.maximum(x, y)) #仍是一维数组,每个元素为数组x和y对应元素最大的那个;

    print('''While not common, a ufunc can return multiple arrays. modf is one example, a vectorized version of the built-in Python divmod: it returns the fractional and integral parts of 

a floating point array:''') 

    arr = randn(7) * 5 

    print(np.modf(arr)) #输出两个数组,第一个数组是小数部分,第二个数组是整数部分

    # Expressing Conditional Logic as Array Operations 

    print('''The numpy.where function is a vectorized version of the ternary expression x if condi 

tion else y.''') 

    xarr = np.array([1.1, 1.2, 1.3, 1.4, 1.5]) 

    yarr = np.array([2.1, 2.2, 2.3, 2.4, 2.5]) 

    cond = np.array([True, False, True, True, False]) 

    print('''Suppose we wanted to take a value from xarr whenever the corresponding value in 

cond is True otherwise take the value from yarr.''') 

    result = np.where(cond, xarr, yarr) 

    print(result) 

    arr = randn(4, 4) 

    print(np.where(arr > 0, 2, -2)) 

    # Mathematical and Statistical Methods 

    arr = np.random.randn(5, 4) # normally-distributed data 

    print(arr) 

    print(arr.mean()) 

    print(np.mean(arr)) 

    print(arr.sum()) 

    print(arr.mean(axis=1)) 

    # Methods for Boolean Arrays 

    print('Boolean values are coerced to 1 (True) and 0 (False) in the above methods. Thus, sum is often used as a means of counting True values in a boolean array:') 

    arr = randn(100) 

    print((arr > 0).sum()) 

    bools = np.array([False, False, True, False]) 

    print(bools.any()) 

    print(bools.all()) 

    # Sorting 

    print('Like Python’s built-in list type, NumPy arrays can be sorted in-place using the sort method:') 

    arr = randn(8) 

    print(arr) 

    print(np.sort(arr)) 

    # Unique and Other Set Logic 

    names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe']) 

    print(np.unique(names)) 

    ints = np.array([3, 3, 3, 2, 2, 1, 1, 4, 4]) 

    print(np.unique(ints)) 
常见 问题:   

(1)arange()类似于内置函数range(),通过指定开始值、终值和步长创建表示等差数列的一维数组,注意得到的结果数组不包含终值。

linspace()通过指定开始值、终值和元素个数创建表示等差数列的一维数组,可以通过endpoint参数指定是否包含终值,默认值为True,即包含终值。

(2)a=np.array([1,3,4,5,6,5,3])
希望颠倒过来变成
[3,5,6,5,4,3,1],有什么函数可以实现?

答:切片功能 a[::-1]

更详细的numpy基础:

http://blog.csdn.net/hickai/article/details/23431843

参考资料:

《Python forData Analyze》

 

转载自:http://blog.sina.com.cn/s/blog_3fe961ae0102uz9z.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值