NumPy常見函數和使用示例

轉自:

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[0] = 42 

    print(arr3d) 

    arr3d[0] = old_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 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: 

    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 a term adopted by NumPy to describe indexing using integer arrays. 

    arr = np.empty((84)) 

    for in range(8): 

        arr[i] = i 

    print(arr) 

    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[[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 a 1D array of elements corresponding to each tuple of indices:'

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

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

 

    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((35)) 

    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)) 

    x = randn(8

    y = randn(8

    print(x) 

    print(y) 

    print(np.maximum(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.11.21.31.41.5]) 

    yarr = np.array([2.12.22.32.42.5]) 

    cond = np.array([TrueFalseTrueTrueFalse]) 

    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(44

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

 

    # Mathematical and Statistical Methods 

    arr = np.random.randn(54# 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([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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章