Subscript access to python numpy array

Subscript access to numpy array

Extraction of array 1 element

>>> import numpy as np
>>> arr = np.arange(0,11)
>>> arr
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
>>> arr[5]
5

Extraction of array 1 element

>>> import numpy as np
>>> arr = np.arange(0,11)
>>> arr
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
>>> arr[5]
5

Slice the array

>>> arr[0:5]
array([0, 1, 2, 3, 4]) #5 not included

Substitute by specifying slice

>>> arr[0:5]=100
array([100, 100, 100, 100, 100,   5,   6,   7,   8,   9,  10])

I intend to make an array copy, but refer to it (even if I don't use slices)

>>> slice_of_arr = arr[0:5]
>>> slice_of_arr
array([100, 100, 100, 100, 100])
>>> slice_of_arr[:]=10
>>> slice_of_arr
array([10, 10, 10, 10, 10])
>>> arr
array([10, 10, 10, 10, 10,  5,  6,  7,  8,  9, 10]) #Change to original array

Array copy

>>> copy_arr = np.copy(arr[0:5])
>>> copy_arr
array([10, 10, 10, 10, 10])
>>> copy_arr[:] = 0
>>> arr
array([ 0, 10, 10, 10, 10,  5,  6,  7,  8,  9, 10]) #No effect on the original array

Two-dimensional subscript

>>> arr_2d = np.array(([0,1,2],[10,11,12],[20,21,22]))
>>> arr_2d 
array([[ 0,  1,  2],
       [10, 11, 12],
       [20, 21, 22]])
>>> arr_2d[0] #Take out the 0th line
array([0, 1, 2])

#Individual access arr_2d[row][col] or arr_2d[row,col]
>>> arr_2d[1][0] 
10
>>> arr_2d[1,0]
10 

Subscript utilization application example

>>> arr2d = np.zeros((10,10))
>>> arr2d
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
>>> arr_length = arr2d.shape[1] #Array length
>>> for i in range(arr_length): arr2d[i] = i
>>> arr2d
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.],
       [ 5.,  5.,  5.,  5.,  5.,  5.,  5.,  5.,  5.,  5.],
       [ 6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.],
       [ 7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.],
       [ 8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.],
       [ 9.,  9.,  9.,  9.,  9.,  9.,  9.,  9.,  9.,  9.]])

>>> arr2d[[2,4,6,8]] #Extract only even rows
array([[ 2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.],
       [ 4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.],
       [ 6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.],
       [ 8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.]])
>>> arr2d[[6,4,2,7]] #Take out in a different order
array([[ 6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.],
       [ 4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.],
       [ 2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.],
       [ 7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.]])

Recommended Posts

Subscript access to python numpy array
python numpy array calculation
Convert numpy int64 to python int
Create a python numpy array
Convert NumPy array "ndarray" to lilt in Python [tolist ()]
Introduction to Python Numerical Library NumPy
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
Python application: Numpy Part 3: Double array
[Python] How to swap array values
How to access wikipedia from python
How to access environment variables in Python
Python / numpy> list (numpy array) file save / load
How to access RDS from Lambda (python)
My Numpy (Python)
Updated to Python 2.7.9
Python multidimensional array
[Beginner] Python array
NumPy array manipulation (3)
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
NumPy array manipulation (1)
Python array basics
Python #Numpy basics
[Python] Numpy memo
"Backport" to python 2
How to sort by specifying a column in the Python Numpy array.
[Python] Road to a snake charmer (4) Tweak Numpy
Just add the python array to the json data
Add rows to an empty array with numpy
[Python numpy] Dynamically specify the index of the array
Speed: Add element to end of Python array
How to make Python faster for beginners [numpy]
Python and numpy tips
[Python] Create structured array (store heterogeneous data with NumPy)
Various ways to extract columns in a NumPy array
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Python basics 8 numpy test
Rewrite Python2 code to Python3 (2to3)
Yum command to access MySQL with Python 3 on Linux
How to install python
python decorator to retry
Introduction to Python language
[Python] Search (NumPy) ABC165C
Introduction to OpenCV (python)-(2)
[Pytorch] numpy to tensor
It's not easy to write Python, it's easy to write numpy and scipy
Convert dict to array
Note to daemonize python
How to use numpy
Introducing Python 2.7 to CentOS 6.6
[Python] Sorting Numpy data
Connect python to mysql
[Python MinMaxScaler] Normalize to 0 ~ 1
How to operate NumPy
Convert elements of numpy array from float to int
Access to dictionary fields
I tried to access Google Spread Sheets using Python
Introduction to Python numpy pandas matplotlib (~ towards B3 ~ part2)
[python] Array slice operation