Data Analysis Learning Notes (ii)--numpy: Array Object related operations

Source: Internet
Author: User
Tags mathematical functions python list
arithmetic of arrays and scalaraddition, subtraction, multiplication, addition, square and other operations
arr1 = Np.array ([Np.arange (5), Np.arange (5,10)]) arr2 = Np.array (Np.arange (5)) arr1 ' [[0 1 2 3 4] [5 6 7 8]]] ' arr


 2 ' [0 1 2 3 4] ' arr1+arr2 ' [[0 2 4 6] [8 5 7]] ' ' ARR1-ARR2 ' [[9 0 0 0 0] [0 5 5 5 5]] ARR1*ARR2 ' [[0 1 4 9] [0 6]] ' Arr1.dot (ARR2) # standard matrix multiplication Np.dot (ARR1,ARR2) # Standard Matrix  multiplied by ' [0] ' ' arr2**2 ' ' [1 4 9] ' ' ARR2/2 ' [0.  0.5 1. 1.5 2. ] ' # Some of the following Python built-in operations can only be valid for one-dimensional arrays, and multidimensional arrays need to take out their elements to further manipulate ARR3 = Np.array (Np.arange ( -5,5), dtype=np.float) # Several mathematical functions built into Python abs (ARR3) # take absolute value [5]. 4.3. 2.1. 0.1. 2.3.  4.] Max (ARR3) # takes the largest 4.0 pow (arr3,3) # The second party [-125.  -64.   -27.   -8.    -1.    0.1.   8.27. 64.] # The following error: Typeerror:type Numpy.ndarray doesn ' t define __round__ method round (ARR3) # rounding # change: Take out the element for operation [Round (item ARR4 = Np.array (Np.arange (1,5)) math.sqrt (ARR4) # Error in the ' for item ' ARR3] # math: typeerror:only size-1 arrays can be Converted to Python scalars # change: Remove the element for operation [MATH.SQRT (item) for item in ARR4] ' [1.0, 1.4142135623730951, 1.7320508075688772, 2.0] '
 
Operations built into the NumPy
arr1 = Np.array ([Np.arange (5), Np.arange (5,10)])
arr2 = Np.array (Np.arange (5))
# min and Max index
np.argmin (ARR1)  # 0
Np.argmax (arr1)  # 9
# mean
Arr1.mean ()      # equals Np.mean (arr1) or Np.average (arr1)
' 4.5 ' '
# median
Np.median (arr1) # median
' ' 4.5 '
# cumulative
arr1.cumsum ()    # Cumulative, equivalent to Np.cumsum ( ARR1)
' [0  1  3  6] '
# tired
Np.diff (arr1)
' [[1 1 1 1] [1]
 1 1 ]
 "'
# Standard matrix multiplication
Arr1.dot (arr2)   # equals Np.dot (ARR1,ARR2)
'" '  Splits the row and column coordinates of all non-0 elements, the two
Np.nonzero (arr1)
' "
(Array ([0, 0, 0, 0, 1, 1, 1, 1, 1]), array ([1, 2, 3, 4, 0, 1, 2, 3, 4]) the value of the position of # (0,0) is 0, is excluded from the
'
# ' sort, the default is quick sort
a = Np.array ([[[2,3,1,5],[2,1,0,3]])
Np.sort (A , axis=0)     # vertical sort ' [
[2 1 0 3]
 [2 3 1 5]]
 '
np.sort (A,axis=1)     # horizontal sort
' [[1 + 2 3 5]
  [0 1 2 3]]
 "'
# Limit the size of the element
Np.clip (arr1,3,6)
' [[3 3 3 3]
 [4 5
 6 6]] '"
indexes and slices of an arrayIndexing and slicing of one-dimensional arrays
# Create a test array
arr = Np.arange (9)  # [0 1 2 3 4 5 6 7 8]
arr
# get
arr[2]       # 2
# Slice operation, operation and list are consistent 
  arr[2:6]     # [2 3 4 5]
Arr[::2]     # [0 2 4 6 8]
arr[::-1]    # [8 7 6 5 4 3 + 2]
# slice
s = Slice (2,6)
arr[s]       # equivalent to Arr[2:6]-> [2 3 4 5]
Indexes and slices of multidimensional dimension arrays
# Create Test array
arr = np.arange. Reshape (2,3,4) "[[
[0  1  2  3] [
  4
  5 6 7] [8  9]]

 [[EUR]
  [A]
  [O]] "
# [] The position of each ', ' in place and the position at which it was created corresponds, that is to say all, must be given explicitly before the specified position, and then can be ignored.
every latitude is a one-dimensional array, and the operation is consistent with a one-dimensional array, Start:end:step
arr[: 0,0]   # The number of the No. 0 row of all two-dimensional arrays, column No. 0, [0]
arr[0]       # equals to Arr[0,:,:, and arr[0,...]
arr[0,1]     # 1th row of the first two-dimensional array  [4 5 6 7]
Arr[0,1,::2] # The first two-dimensional array of line 1th, step to 2  [4 6]
arr[...,1]   # The 1th column of all two-dimensional arrays
' [[1  5 9] [[]] '
arr[::-1]   # Reverses all two-dimensional arrays
' [[[12 13 [[

 0  1  2  3] [
  4  5
  6 7] [[8<] [+] C46/>9]]
 ' '
arr[::-1,::-1,::-1]  # Reverses all two-dimensional arrays and reverses the elements ' [[[
19] [
  17 16]<] [ C52/>[15]

 [[  9  8]
  [7  6  5 4  ]
  [3  2  1 0]]]
 '
s = Slice (none,none,-1)
arr[(s,s,s)] # Consistent with the previous example ' [[
[A] [
  15] [14 [

 [  9  8] [
  7  6  5 4  ]
  [3  2
 1 0]]] "
Boolean index
arr = Np.arange. Reshape (2,3,4)
# output ARR value is greater than 10
arr[arr>10]  # (23]
  # arr value greater than 10 is replaced by 0
arr[arr>10] = 0 ' [[     
[0 1 2 3  ] [
  4
  5 6 7] [8  9  0]]

 [[0  0  0  0]
  [0 0 0 0  ]
  [0  0
0 0]]] "
Fancy Index
arr = Np.arange. Reshape ((8, 4))
# Get data for [1,5,2,6] rows
arr[[1,5,2,6]
'
[[4  5  6  7]
 [O]
 [8  9]
 [A]]
'
# get data from [0,1] columns
arr[:,[0,1]]
# If rows and columns give arguments, get the data on the corresponding coordinates, such as Get [(1,0), (5,2), (2,1), (6,3), (0,2)] Data
# Note: rows, Columns to correspond to
arr[[1,5,2,6,0],[0,2,1,3,2]]
'
[4  9  2] '
# to
get a method of a region
# First determine the required row data, such as [1,5,2,6]
# and then default all rows, determine the required columns, such as [0,2]
arr[[1,5,2,6]][:,[0,2]] '
[[4  6]
 [20 22]
 [8]
 [A]]
'
# You can use the IX_ function directly to solve the above problem
arr[np.ix_ ([1,5,2,6],[0,2])]
' [[4  6  5 7] [+]
 [8  9] [to]
 ] '
Array TransposeArray transpose
# We can use the method provided by NumPy to achieve the effect of transpose, row and column reversal
arr = np.arange. Reshape ((3, 5))
' [[0  1  2
 3 4] [5  6  7  8  9]
 [
arr] '. T
' [[0  5]
 [1  6] [2 7] [3  8]
 [4  9]]
Arr.transpose () # No parameters and, (0,1)-> (1,0)
' [[0  5]
 [1  6] [
 2 7  ]
 [3  8
 [4  9 14]] "
Array transpose function transpose ()

When the transpose () parameter is empty, the default parameter is the flashback order of the latitude ordinal number, such as (0,1)-> (1,0), which is the row and column Exchange
When the latitude is greater than or equal to 3 o'clock, the dimension order can be exchanged before the corresponding transpose

arr = Np.arange () reshape (2,3,2)
' [[0  1]
  [2  3] [
  4 5  ]]
  [[6 7] [ 8  9]
  [a]] ' '
# depth becomes row, line becomes depth
arr = Arr.transpose ()   # arr.transpose (2,1,0)
' [[0  6]
  [2  8]
  [4]]

 [[1  7]
  [3  9]
  [5]]] '
# columns become rows, rows become columns
arr = arr.transpose (1,0,2) ' [[
0  1]
  [6  7]]

 [[2  3]
  [8  9]]

 [[4  5]
  [a]] '
#深度变为列, the column becomes depth
arr = arr.transpose (0,2,1) "[[[
0  2  4]
  [1 3 5  ]]

 [[6 8  ]
  [7  9 11]]]"
change the latitude of an array
# Create one-dimensional array
arr = Np.arange (
#) Change latitude through reshape ()
arr = arr.reshape (2,3,4) ' [[
0  1  2  3]
  [4  5  6  7]
  [8  9]] [[

 [
A] [of] [[a]]] # directly change the shape of the array, note: The number of elements before and after the conversion should be consistent
Arr.shape = (6,4)
' ' [[0  1  2  3] [
 4  5 6  7]
 [8  9]
 [EUR]
 [A]
 [+]]
# Change Latitude by resize () function
arr.resize (12,2)
' [[0  1]
 [2  3] [
 4 5] [6 7  ]
 [8  9]
 [All
 ]
[[A] [[a] [a] [[a]] ' ' Reshape function: Do not change the original array dimension, there is a return value
Resize function: Directly change the original array dimension, no return value
Shape property: Direct change of the original array dimension ' '
# array tile
arr.ravel ()
The difference between Arr.flatten () is that the Ravel and flatten functions return a view of a one-dimensional array, but the flatten function also requests that memory be allocated to save the result '
Merging of arrays
# Arrays a = Np.arange (a). Reshape (3,4) "' [[0 1 2 3] [4 5 6 7] [8 9]] '" B =     A * 2 "" [[0 2 4 6] [8 10 12 14] [16 18 20 22]] ' # Transverse merge Np.hstack ((a,b)) # is equivalent to Np.concatenate ((A, B), Axis=1) Axis:1 for transverse, 0 for longitudinal ' [[0 1 2 3 0 2 4 6] [4 5 6 7 8 10 + 12] [14 8 9 10 11 16 18 20]] ' # vertically merged Np.vst  ACK ((a,b)) # equals Np.concatenate ((A, B), axis=0) ' [[0 1 2 3] [4 5 6 7] [8 9 10 11] [0 2 4 6] [8 10 12 14] [16 18 20 22]] ' # Deep merge Np.dstack ((a,b)) ' [[[0 0] [1 2] [2 4] [3 6]] [[4 8] [5 10] [ 
6 12] [7 14]] [[8 16] [9 18] [10 20] [11 22]]] ' # merge Np.column_stack ((A, B) by column) # equivalent to Np.hstack ((a,b))         "[[0 1 2 3 0 2 4 6] [4 5 6 7 8 10 + 12] [14 8 9 10 11 16 + 18]]]" "# Merge Np.row_stack by line ((A, B)) 
# Equals Np.vstack ((a,b)) ' [[0 1 2 3] [4 5 6 7] [8 9 10 11] [0 2 4 6] [8 10 12 14] [16 18 20]] '''
Partitioning of ArraysEqual division
A = Np.arange (a). Reshape (3, 4)
' "[[0  1  2 3  ] [
 4  5
 6 7] [8 9]] ' # split the
number of columns must be a few divisors 1,2,4
np.hsplit (a,4) is   equal to Np.split (A, 4, Axis=1)    axis:1 is horizontal, 0 is longitudinal
' ' [[[0], [
       4], [
       8]]), 
    array ([[[1],
       [5], [9]]), Array ([[2] 
    ,
       [6],
       [10 ]],
    array ([[3],
       [7], [one
       ]])]
'
# longitudinal split, the split quantity must be the number of rows of the approximate 1,3
np.vsplit (a,3)   # equals Np.split (A, 3, axis=0)
' "[Array ([[0, 1, 2, 3]]), Array ([[4, 5, 6, 7]]), Array ([[8, 9, 10  , 11]]]"
Non-equal division
Np.array_split (A,3,axis=1) # split 4 columns into 3 parts
' [[[0, 1],
       [4, 5],
       [8, 9]], Array ([[2],
       [6],
       [ ]), Array ([[3],
       [7], [one
       ]])]
'
np.array_split (a,4,axis=0) # vertically splits 3 rows into 4 parts, and the last array is an empty array
' [ Array ([[0, 1, 2, 3]), Array ([[4, 5, 6, 7]]), Array ([[[8, 9,]  ]), array ([], shape= (0, 4), Dtype=int64)] "
Conversion of Arrays
b = Np.array ([1.+1.J, 3.+2.J]) # Converts the Ndarray type to Python list type list = B.tolist () type (list) #      <class ' list ' > Print (list) # [(1+1j), (3+2J)] # convert Ndarray type to Python string = b.tostring () type (string) # <class ' bytes ' > Print (String) ' ' B ' \x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\ x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x00@ ' ' # converts the specified string to Ndarray type C = np.fromstring (String, Dtype=complex C # [1.+1.J 3.+2.J] Type (c) # <class ' Numpy.ndarray ' > C.dtype # complex128 D = np.fromstring (' 20:42:52 ', Sep= ': ', Dtype=int D # [M] D.dtype # Int64 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.