[Data analysis study notes day08] + Elemental ndarray element processing element statistics function calculation function + + + judging function element reordering element to function

Element calculation function

  1. ceil(): Up to the nearest integer, number or array parameters
  2. floor(): Down to the nearest integer, number or array parameters
  3. rint(): Rounding, or array parameter is the number
  4. isnan(): Determining whether the element is a NaN (Not a Number), a number or parameter array
  5. multiply(): Multiplication element, number or array parameters
  6. divide(): Division elements, or array parameter is the number
  7. abs(): The absolute value of the element, the parameter is a number or array
  8. where(condition, x, y): Ternary operator, x if condition else y

Sample Code (1,2,3,4,5,6,7):

# randn() 返回具有标准正态分布的序列。
arr = np.random.randn(2,3)

print(arr)

print(np.ceil(arr))

print(np.floor(arr))

print(np.rint(arr))

print(np.isnan(arr))

print(np.multiply(arr, arr))

print(np.divide(arr, arr))

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

operation result:

# print(arr)
[[-0.75803752  0.0314314   1.15323032]
 [ 1.17567832  0.43641395  0.26288021]]

# print(np.ceil(arr))
[[-0.  1.  2.]
 [ 2.  1.  1.]]

# print(np.floor(arr))
[[-1.  0.  1.]
 [ 1.  0.  0.]]

# print(np.rint(arr))
[[-1.  0.  1.]
 [ 1.  0.  0.]]

# print(np.isnan(arr))
[[False False False]
 [False False False]]

# print(np.multiply(arr, arr))
[[  5.16284053e+00   1.77170104e+00   3.04027254e-02]
 [  5.11465231e-03   3.46109263e+00   1.37512421e-02]]

# print(np.divide(arr, arr))
[[ 1.  1.  1.]
 [ 1.  1.  1.]]

# print(np.where(arr > 0, 1, -1))
[[ 1  1 -1]
 [-1  1  1]]

Elements of statistical functions

  1. np.mean(), np.sum(): Average value of all elements, all elements and parameters are number or array
  2. np.max(), np.min(): The maximum value of all elements, all the elements of the minimum value, the parameter is the number or array
  3. np.std(), np.var(): All standard deviation of the elements, all the elements of the variance, or array parameter is the number
  4. np.argmax(), np.argmin(): The maximum index value index, the index value of minimum index, number or array parameters
  5. np.cumsum(), np.cumprod(): Returns a one-dimensional array, each element and all previous accumulated elements and tired product, number or array parameters
  6. Multidimensional array default statistics for all dimensions, axisparameters can be specified axis of statistics, the value of 0press column statistics, the value of 1press row statistics.

Sample code:

arr = np.arange(12).reshape(3,4)
print(arr)

print(np.cumsum(arr)) # 返回一个一维数组,每个元素都是之前所有元素的 累加和

print(np.sum(arr)) # 所有元素的和

print(np.sum(arr, axis=0)) # 数组的按列统计和

print(np.sum(arr, axis=1)) # 数组的按行统计和

operation result:

# print(arr)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

# print(np.cumsum(arr)) 
[ 0  1  3  6 10 15 21 28 36 45 55 66]

# print(np.sum(arr)) # 所有元素的和
66

# print(np.sum(arr, axis=0)) # 0表示对数组的每一列的统计和
[12 15 18 21]

# print(np.sum(arr, axis=1)) # 1表示数组的每一行的统计和
[ 6 22 38]

Analyzing the function element

  1. np.any(): There is at least one element specified conditions are met, returns True
  2. np.all(): All elements meet specified criteria, returns True

Sample code:

arr = np.random.randn(2,3)
print(arr)

print(np.any(arr > 0))
print(np.all(arr > 0))

operation result:

[[ 0.05075769 -1.31919688 -1.80636984]
 [-1.29317016 -1.3336612  -0.19316432]]

True
False

Elements to reorder function

np.unique(): Find unique values ​​and returns the result of the sort, similar to the set of a collection of Python

Sample code:

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

print(np.unique(arr))

operation result:

[[1 2 1]
 [2 3 4]]

[1 2 3 4]

Copyright © BigCat all right reserved,powered by Gitbook「Revision Time: 2017-03-12 23:55:41」

Published 176 original articles · won praise 56 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_35456045/article/details/104084396