Arrays - Ipynb - Colaboratory
Arrays - Ipynb - Colaboratory
ipynb - Colaboratory
Array
An array is a collection of items stored at contiguous memory locations. The idea is to store
multiple items of the same type together. This makes it easier to calculate the position of each
element by simply adding an offset to a base value, i.e., the memory location of the first element of
the array (generally denoted by the name of the array).
Creating an Array
An Array in Python can be created by importing an array module. array(data_type, value_list) is used
to create an array with data type and value list specified in its arguments.
Basic Operations
https://colab.research.google.com/drive/1BEPz-PC6SKOv9U_0TJx7Xdp_yMCAQpgl#printMode=true 1/12
18/11/2023, 19:12 Arrays.ipynb - Colaboratory
https://colab.research.google.com/drive/1BEPz-PC6SKOv9U_0TJx7Xdp_yMCAQpgl#printMode=true 2/12
18/11/2023, 19:12 Arrays.ipynb - Colaboratory
https://colab.research.google.com/drive/1BEPz-PC6SKOv9U_0TJx7Xdp_yMCAQpgl#printMode=true 3/12
18/11/2023, 19:12 Arrays.ipynb - Colaboratory
print("\r")
print("\r")
https://colab.research.google.com/drive/1BEPz-PC6SKOv9U_0TJx7Xdp_yMCAQpgl#printMode=true 4/12
18/11/2023, 19:12 Arrays.ipynb - Colaboratory
print("\r")
https://colab.research.google.com/drive/1BEPz-PC6SKOv9U_0TJx7Xdp_yMCAQpgl#printMode=true 5/12
18/11/2023, 19:12 Arrays.ipynb - Colaboratory
print("\r")
NumPy
NumPy, which stands for Numerical Python, is a library consisting of multidimensional array
objects and a collection of routines for processing those arrays. It provides various computing tools
such as comprehensive mathematical functions, linear algebra routines.
https://colab.research.google.com/drive/1BEPz-PC6SKOv9U_0TJx7Xdp_yMCAQpgl#printMode=true 6/12
18/11/2023, 19:12 Arrays.ipynb - Colaboratory
import numpy as np
You can create an array from a regular Python list or tuple using an array() function. The type of the
resulting array is deduced from the type of the elements in the sequences.
import numpy as np
Often, the element is of an array is originally unknown, but its size is known. Hence, NumPy offers
several functions to create arrays with initial placeholder content. These minimize the necessity of
https://colab.research.google.com/drive/1BEPz-PC6SKOv9U_0TJx7Xdp_yMCAQpgl#printMode=true 7/12
18/11/2023, 19:12 Arrays.ipynb - Colaboratory
growing arrays, an expensive operation. For example: np.zeros, np.ones, np.full, np.empty, etc.
array([[6, 6, 6],
[6, 6, 6],
[6, 6, 6]])
array([[0.31860091, 0.17439734],
[0.26124141, 0.23708155],
[0.51282263, 0.72894495]])
arange: This function returns evenly spaced values within a given interval. Step size is specified.
array([0])
https://colab.research.google.com/drive/1BEPz-PC6SKOv9U_0TJx7Xdp_yMCAQpgl#printMode=true 8/12
18/11/2023, 19:12 Arrays.ipynb - Colaboratory
Reshaping array: We can use reshape method to reshape an array. The original size of the array
remains unchanged.
newarr = arr.reshape(2, 2, 3)
Original array:
[[1 2 3 4]
[5 2 4 2]
[1 2 0 1]]
---------------
Reshaped array:
[[[1 2 3]
[4 5 2]]
[[4 2 1]
[2 0 1]]]
Flatten array: We can use flatten method to get a copy of the array collapsed into one dimension.
# Flatten array
arr = np.array([[1, 2, 3], [4, 5, 6]])
flat_arr = arr.flatten()
Original array:
[[1 2 3]
[4 5 6]]
Fattened array:
[1 2 3 4 5 6]
https://colab.research.google.com/drive/1BEPz-PC6SKOv9U_0TJx7Xdp_yMCAQpgl#printMode=true 9/12
18/11/2023, 19:12 Arrays.ipynb - Colaboratory
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.
Boolean array indexing: This method is used when we want to pick elements from an array which
satisfy some condition.
#Creating a 2D arrays
arr = np.array([[20,25,30,35], [40,50,60,70], [45,48,51,54]])
[[20 25]
[40 50]]
[[20 30]]
[[ True True True True]
[ True True True True]
[ True True True True]]
https://colab.research.google.com/drive/1BEPz-PC6SKOv9U_0TJx7Xdp_yMCAQpgl#printMode=true 10/12
18/11/2023, 19:12 Arrays.ipynb - Colaboratory
a = np.array([1, 2, 5, 3])
# transpose of array
a = np.array([[1, 2, 3], [3, 4, 5], [9, 6, 0]])
Original array:
[[1 2 3]
[3 4 5]
[9 6 0]]
Transpose of array:
[[1 3 9]
[2 4 6]
[3 5 0]]
https://colab.research.google.com/drive/1BEPz-PC6SKOv9U_0TJx7Xdp_yMCAQpgl#printMode=true 11/12
18/11/2023, 19:12 Arrays.ipynb - Colaboratory
https://colab.research.google.com/drive/1BEPz-PC6SKOv9U_0TJx7Xdp_yMCAQpgl#printMode=true 12/12