Ot Lab 6
Ot Lab 6
Normalize
marks out of 5
(5)
APPARATUS USED:
You can easily use create a new array from a section of an existing array.
You can also stack two existing arrays, both vertically and horizontally. Let’s say you have two
arrays, a1 and a2:
>>> np.hsplit(x, 3)
[array([[1, 2, 3, 4],
[13, 14, 15, 16]]), array([[ 5, 6, 7, 8],
[17, 18, 19, 20]]), array([[ 9, 10, 11, 12],
[21, 22, 23, 24]])]
If you wanted to split your array after the third and fourth column, you’d run:
You can use the view method to create a new array object that looks at the same data as the
original array (a shallow copy).
Views are an important NumPy concept! NumPy functions, as well as operations like indexing
and slicing, will return views whenever possible. This saves memory and is faster (no copy of the
data has to be made). However it’s important to be aware of this - modifying data in a view also
modifies the original array!
>>> b1 = a[0, :]
>>> b1
array([1, 2, 3, 4])
>>> b1[0] = 99
>>> b1
array([99, 2, 3, 4])
>>> a
array([[99, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
Using the copy method will make a complete copy of the array and its data (a deep copy). To use
this on your array, you could run:
>>> b2 = a.copy()
Learn more about copies and views here.
Once you’ve created your arrays, you can start to work with them. Let’s say, for example, that
you’ve created two arrays, one called “data” and one called “ones”
You can add the arrays together with the plus sign.
Basic operations are simple with NumPy. If you want to find the sum of the elements in an array,
you’d use sum(). This works for 1D arrays, 2D arrays, and arrays in higher dimensions.
>>> a.sum()
10
To add the rows or the columns in a 2D array, you would specify the axis.
>>> b.sum(axis=0)
array([3, 3])
You can sum over the axis of columns with:
>>> b.sum(axis=1)
array([2, 4])
Learn more about basic operations here.
Broadcasting
There are times when you might want to carry out an operation between an array and a single
number (also called an operation between a vector and a scalar) or between arrays of two
different sizes. For example, your array (we’ll call it “data”) might contain information about
distance in miles but you want to convert the information to kilometers. You can perform this
operation with:
NumPy understands that the multiplication should happen with each cell. That concept is
called broadcasting. Broadcasting is a mechanism that allows NumPy to perform operations on
arrays of different shapes. The dimensions of your array must be compatible, for example, when
the dimensions of both arrays are equal or when one of them is 1. If the dimensions are not
compatible, you will get a ValueError.
>>> data.max()
2.0
>>> data.min()
1.0
>>> data.sum()
3.0
>>> a.sum()
4.8595784
Or:
>>> a.min()
0.05093587
You can specify on which axis you want the aggregation function to be computed. For example,
you can find the minimum value within each column by specifying axis=0.
>>> a.min(axis=0)
array([0.12697628, 0.05093587, 0.26590556, 0.5510652 ])
The four values listed above correspond to the number of columns in your array. With a four-
column array, you will get four values as your result.
Creating matrices
You can pass Python lists of lists to create a 2-D array (or “matrix”) to represent them in NumPy.
Indexing and slicing operations are useful when you’re manipulating matrices:
>>> data[0, 1]
2
>>> data[1:3]
array([[3, 4],
[5, 6]])
>>> data[0:2, 0]
array([1, 3])
You can aggregate matrices the same way you aggregated vectors:
>>> data.max()
6
>>> data.min()
1
>>> data.sum()
21
You can aggregate all the values in a matrix and you can aggregate them across columns or rows
using the axis parameter:
>>> data.max(axis=0)
array([5, 6])
>>> data.max(axis=1)
array([2, 4, 6])
Once you’ve created your matrices, you can add and multiply them using arithmetic operators if
you have two matrices that are the same size.
Be aware that when NumPy prints N-dimensional arrays, the last axis is looped over the fastest
while the first axis is the slowest. For instance:
[[1., 1.],
[1., 1.],
[1., 1.]],
[[1., 1.],
[1., 1.],
[1., 1.]],
[[1., 1.],
[1., 1.],
[1., 1.]]])
There are often instances where we want NumPy to initialize the values of an array. NumPy
offers functions like ones() and zeros(), and the random.Generator class for random number
generation for that. All you need to do is pass in the number of elements you want it to generate:
>>> np.ones(3)
array([1., 1., 1.])
>>> np.zeros(3)
array([0., 0., 0.])
# the simplest way to generate random numbers
>>> rng = np.random.default_rng(0)
>>> rng.random(3)
array([0.63696169, 0.26978671, 0.04097352])
You can also use ones(), zeros(), and random() to create a 2D array if you give them a tuple
describing the dimensions of the matrix:
Read more about creating arrays, filled with 0’s, 1’s, other values or uninitialized, at array
creation routines.
Task 1:
Task 1: create an array of numbers upto your roll number and then make it into
two arrays from its central element. And also Make it into three arrays
Task 2: add a number in a 1d and and 2d array
Task 3: add two different arrays
Task 4: find mean, average, median, minimum and maximum from an array of
numbers
Task 5: multiply an array with a non zero number
Procedure:
Results:
Conclusion: