NumPy Array Indexing
Access Array Elements
Array indexing is the same as accessing an array element.
You can access an array element by referring to its index number.
The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second
has index 1 etc.
ExampleGet your own Python Server
Get the first element from the following array:
import numpy as np
arr = [Link]([1, 2, 3, 4])
print(arr[0])
o/p:1
Example2
Get the second element from the following array.
import numpy as np
arr = [Link]([1, 2, 3, 4])
print(arr[1])
o/p=2
Example
Get third and fourth elements from the following array and add them.
import numpy as np
arr = [Link]([1, 2, 3, 4])
print(arr[2] + arr[3])
o/p:7
Access 2-D Arrays
To access elements from 2-D arrays we can use comma separated integers representing the
dimension and the index of the element.
Think of 2-D arrays like a table with rows and columns, where the dimension represents the row and
the index represents the column.
Example
Access the element on the first row, second column:
import numpy as np
arr = [Link]([[1,2,3,4,5], [6,7,8,9,10]])
print('2nd element on 1st row: ', arr[0, 1])
NumPy Array Slicing
Slicing arrays
Slicing in python means taking elements from one given index to another given index.
We pass slice instead of index like this: [start:end].
We can also define the step, like this: [start:end:step].
If we don't pass start its considered 0
If we don't pass end its considered length of array in that dimension
If we don't pass step its considered 1
ExampleGet your own Python Server
Slice elements from index 1 to index 5 from the following array:
import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5])
o/p: [2 3 4 5]
Example
Slice elements from index 4 to the end of the array:
import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])
print(arr[4:])
o/p: [5 6 7]
Slice elements from the beginning to index 4 (not included):
import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])
print(arr[:4])
o/P: [1 2 3 4]
Negative Slicing
Use the minus operator to refer to an index from the end:
Example
Slice from the index 3 from the end to index 1 from the end:
import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])
print(arr[-3:-1])
o/p:
Data Types in Python
By default Python have these data types:
strings - used to represent text data, the text is given under quote marks. e.g. "ABCD"
integer - used to represent integer numbers. e.g. -1, -2, -3
float - used to represent real numbers. e.g. 1.2, 42.42
boolean - used to represent True or False.
complex - used to represent complex numbers. e.g. 1.0 + 2.0j, 1.5 + 2.5j
Data Types in NumPy
NumPy has some extra data types, and refer to data types with one character, like i for integers, u for
unsigned integers etc.
Below is a list of all data types in NumPy and the characters used to represent them.
i- integer
b- boolean
u- unsigned integer
f- float
c- complex float
m- timedelta
M- datetime
O- object
S- string
U- unicode string
V- fixed chunk of memory for other type ( void )
Checking the Data Type of an Array
The NumPy array object has a property called dtype that returns the data type of the array:
ExampleGet your own Python Server
Get the data type of an array object:
import numpy as np
arr = [Link]([1, 2, 3, 4])
print([Link])
int64
Example
Get the data type of an array containing strings:
import numpy as np
arr = [Link](['apple', 'banana', 'cherry'])
print([Link])
o/p<U6
Creating Arrays With a Defined Data Type
We use the array() function to create arrays, this function can take an optional argument: dtype that
allows us to define the expected data type of the array elements:
Example
Create an array with data type string:
import numpy as np
arr = [Link]([1, 2, 3, 4], dtype='S')
print(arr)
print([Link])
[b'1' b'2' b'3' b'4']
|S1