Professional Documents
Culture Documents
NOTES
Generally Arrays are have same data type elements and are stored in continuous
memory location.
The numpy dimensions are called as Axes.
The number of Axes/Dimensions is called as Rank.
Numpy array is called as Ndarray.
#Creating an array
arr1 = np.array([[1,2,3],[11,12,13]]) #[a,b,c] - This is an array
"""We get output as (2,3) that means there are 2 arrays with 3 elements each. Shape
are equal elements in all the arrays"""
[1 2 3 4 5 6 7]
The type is: <class 'numpy.ndarray'>
[11 12 13 14 15]
The type is: <class 'numpy.ndarray'>
# 2- np.arange(start,stop,step,dtype)
b = np.arange(1,20,3,dtype=np.int64)
print("This is the array using np.arange:",b)
print("\n")
Array Indexing
Array indexing in NumPy refers to the process of accessing elements, sub-arrays, or
specific slices of a NumPy array.
There are 3 ways to do array indexing:
1) Slicing: Just like lists in python, NumPy arrays can be sliced. As arrays can be
multidimensional, you need to specify a slice for each dimension of the array.
2) Integer array indexing: In this method, lists are passed for indexing for each
dimension. One to one mapping of corresponding elements is done to construct a new
arbitrary array.
3) Boolean array indexing: This method is used when we want to pick elements from
array which satisfy some condition.
This is temp3: 12
This is temp4: [10 15]
This is temp5: [ 2 11 8]
Integer
Float
Object
Boolean
String
Datetime
Timedelta
dt_float32 = np.dtype(np.float32)
print("Byteorder:", dt_float32.byteorder)
print("Item Size:", dt_float32.itemsize)
print("Name:", dt_float32.name)
print("\n")
dt_bool = np.dtype(np.bool_)
print("Byteorder:", dt_bool.byteorder)
print("Item Size:", dt_bool.itemsize)
print("Name:", dt_bool.name)
print("\n")
dt_string = np.dtype(np.string_)
print("Byteorder:", dt_string.byteorder)
print("Item Size:", dt_string.itemsize)
print("Name:", dt_string.name)
print("\n")
dt_datetime = np.dtype(np.datetime64)
print("Byteorder:", dt_datetime.byteorder)
print("Item Size:", dt_datetime.itemsize)
print("Name:", dt_datetime.name)
print("\n")
dt_timedelta = np.dtype(np.timedelta64)
print("Byteorder:", dt_timedelta.byteorder)
print("Item Size:", dt_timedelta.itemsize)
print("Name:", dt_timedelta.name)
Byteorder: =
Item Size: 4
Name: int32
Byteorder: =
Item Size: 4
Name: float32
Byteorder: |
Item Size: 1
Name: bool
Byteorder: |
Item Size: 0
Name: bytes
Byteorder: =
Item Size: 8
Name: datetime64
Byteorder: =
Item Size: 8
Name: timedelta64
In [8]: a1 = np.array([1,2,3,4])
z = a1.copy()
y = a1.view()
print("This is the copy: ",z)
print("This is the view: ",y)
print("This is to check if the copy array holds the data: ",z.base)
print("This is to check if the view array holds the data: ",y.base)
This is the copy: [1 2 3 4]
This is the view: [1 2 3 4]
This is to check if the copy array holds the data: None
This is to check if the view array holds the data: [1 2 3 4]
Reshape
The reshape is used to change the shape of the array.
In [9]: a2 = np.array([[1,2,3,4],[5,6,7,8]])
print("This is the original array: \n",a2)
print("The shape of the original array: ",a2.shape)
print("\n")
new_arr = a2.reshape(4,2)
print("This is the array after reshaping: \n",new_arr)
print('This is shape of the new_arr: ',new_arr.shape)
1
2
3
4
[1 2 3]
[4 5 6]
1
2
3
4
5
6
[[1 2 3]
[4 5 6]]
[[ 7 8 9]
[10 11 12]]
1
2
3
4
5
6
7
8
9
10
11
12
11
12
13
14
15
16
17
18
19
20
21
22
1
4
2
5
3
6
1 5
2 6
3 7
4 8
1.0
2.0
3.0
ndenumerate()
It is used when we need to iterate through each element of the array while also getting
the index number of the element that we are iterating through.
#For an 2D array
q2 = np.array([[1,2,3],[4,5,6]])
for idx, x in np.ndenumerate(q2):
print(idx,x)
(0,) 1
(1,) 2
(2,) 3
(0, 0) 1
(0, 1) 2
(0, 2) 3
(1, 0) 4
(1, 1) 5
(1, 2) 6
[[ 1 2 3]
[11 12 13]]
[ 1 2 3 11 12 13]
[[ 1 2 3]
[11 12 13]]
[[[ 1 11]
[ 2 12]
[ 3 13]]]
In [18]: a1 = np.array([1,2,3])
a2 = np.array_split(a1,2)
print('This is the original array: ',a1)
print('This is the split array: ',a2)
Array Sorting
We use the np.sort(array_name) to sort the array.
In [19]: v1 = np.array([1,2,3,5,4,8,6,9,7])
print('This is the sorted array: ',np.sort(v1))
Array Searching
Where(array_name operator value) this is used to search the value based on the
conditon inside the where.
Searchsorted(array_name, value)
(array([0, 1, 2, 3]),)
1