NumPy常见函数和使用示例

来源:互联网 发布:高分子材料与工程知乎 编辑:程序博客网 时间:2024/04/27 23:55

转自:

http://blog.sina.com.cn/s/blog_3fe961ae0102uz9z.html

一些常用的函数总结:


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

NumPy常见函数和使用示例

NumPy常见函数和使用示例

 

Python Code:

 

demo.py

import numpy as np 

from numpy.matlib import randn 

 

 

if __name__ == '__main__'

    data1 [67.58,1

    Create an array. 

    arr1 np.array(data1) 

    print(arr1) 

 

    data2 [[1234], [5678]] 

    arr2 np.array(data2) 

    print(arr2) 

    Type of data in array. 

    print(arr1.dtype) 

    print(arr2.ndim) 

    print(arr2.shape) 

 

    print(np.zeros(10)) 

    print(np.zeros((36))) 

    Empty array will contain uninitialized garbage values. 

    print(np.empty((232))) 

 

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

    print(np.arange(15)) 

 

    arr3 np.array([123], dtype=np.float64) 

    arr4 np.array([123], 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([12345]) 

    print(arr5.dtype) 

    arr6 arr5.astype(np.float64) 

    print(arr6.dtype) 

 

    Operations between Arrays and Scalars 

    arr np.array([[1.2.3.], [4.5.6.]]) 

    print(arr arr) 

    print(arr arr) 

 

    arr2d np.array([[123], [456], [789]]) 

    print(arr2d[2]) 

    arr3d np.array([[[123], [456]], [[789], [101112]]]) 

    old_values arr3d[0].copy() 

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

    arr3d[042 

    print(arr3d) 

    arr3d[0old_values 

    print(arr3d) 

 

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

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

    print(names) 

    data randn(74

    print(data) 

 

    Boolean Indexing 

    Suppose each name corresponds to 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 boolean array: 

    print(names == 'Bob'

    print(data[names == 'Bob']) 

    print(data[names == 'Bob'2:],) 

    print(data[names == 'Bob'3]) 

    print(data[((names == 'Bob'(names == 'Will'))]) 

 

    Fancy Indexing 

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

    arr np.empty((84)) 

    for in range(8): 

        arr[i] 

    print(arr) 

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

    print(arr[[4306]]) 

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

    print(arr[[-3-5-7]]) 

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

    arr np.arange(32).reshape((84)) 

    print(arr[[1572], [0312]]) 

 

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

    arr np.arange(15).reshape((35)) 

    print(arr.T) 

 

    print('''A universal function, or ufunc, is 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)) 

    randn(8

    randn(8

    print(x) 

    print(y) 

    print(np.maximum(x, y)) 

    print('''While not common, ufunc can return multiple arrays. modf is one example, vectorized 

version of the built-in Python divmod: it returns the fractional and integral parts of 

floating point array:'''

    arr randn(75 

    print(np.modf(arr)) 

 

    Expressing Conditional Logic as Array Operations 

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

tion else y.'''

    xarr np.array([1.11.21.31.41.5]) 

    yarr np.array([2.12.22.32.42.5]) 

    cond np.array([TrueFalseTrueTrueFalse]) 

    print('''Suppose we wanted to take 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(44

    print(np.where(arr 02-2)) 

 

    Mathematical and Statistical Methods 

    arr np.random.randn(54normally-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 (True) and (False) in the above methods. Thus, sum is often used as means of counting True values in boolean array:'

    arr randn(100

    print((arr 0).sum()) 

    bools np.array([FalseFalseTrueFalse]) 

    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([333221144]) 

    print(np.unique(ints)) 

    


参考资料:

Python for Data Analyze

0 0
原创粉丝点击