0% found this document useful (0 votes)
2 views42 pages

Numpy Library in Python

NumPy, short for Numerical Python, is a powerful library in Python that supports large multidimensional arrays and provides tools for efficient data operations. It is preferred over Python lists for mathematical operations due to its homogeneous data storage and performance advantages. NumPy allows for easy array creation, manipulation, and provides various methods for reshaping, indexing, and slicing arrays.

Uploaded by

sarangishika29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views42 pages

Numpy Library in Python

NumPy, short for Numerical Python, is a powerful library in Python that supports large multidimensional arrays and provides tools for efficient data operations. It is preferred over Python lists for mathematical operations due to its homogeneous data storage and performance advantages. NumPy allows for easy array creation, manipulation, and provides various methods for reshaping, indexing, and slicing arrays.

Uploaded by

sarangishika29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What is the NumPy library in Python?

NumPy stands for Numerical Python and is one of the most useful scientific
libraries in Python programming. It provides support for large multidimensional
array objects and various tools to work with them. Various other libraries like
Pandas, Matplotlib, and Scikit-learn are built on top of this amazing
library.

Arrays are a collection of elements/values, that can have one or more


dimensions. An array of one dimension is called a Vector while having two
dimensions is called a Matrix.

NumPy arrays are called ndarray or N-dimensional arrays and they store
elements of the same type and size. It is known for its high-performance and
provides efficient storage and data operations as arrays grow in size.

NumPy comes pre-installed when you download Anaconda. But if you want to
install NumPy separately on your machine, just type the below command on
your terminal:

pip install numpy

Now you need to import the library:

import numpy as np

np is the de facto abbreviation for NumPy used by the data science community.
Python Lists vs NumPy Arrays – What’s the Difference?

If you’re familiar with Python, you might be wondering why use NumPy arrays
when we already have Python lists? After all, these Python lists act as an array
that can store elements of various types. This is a perfectly valid question and
the answer to this is hidden in the way Python stores an object in memory.

A Python object is actually a pointer to a memory location that stores all the
details about the object, like bytes and the value. Although this extra
information is what makes Python a dynamically typed language, it also comes
at a cost which becomes apparent when storing a large collection of objects, like
in an array.

Python lists are essentially an array of pointers, each pointing to a location that
contains the information related to the element. This adds a lot of overhead in
terms of memory and computation. And most of this information is rendered
redundant when all the objects stored in the list are of the same type!

To overcome this problem, we use NumPy arrays that contain only


homogeneous elements, i.e. elements having the same data type. This makes it
more efficient at storing and manipulating the array. This difference becomes
apparent when the array has a large number of elements, say thousands or
millions. Also, with NumPy arrays, you can perform element-wise
operations, something which is not possible using Python lists!

This is the reason why NumPy arrays are preferred over Python lists when
performing mathematical operations on a large amount of data.
Creating a NumPy Array

Basic ndarray

NumPy arrays are very easy to create given the complex problems they solve.
To create a very basic ndarray, you use the [Link]() method. All you have to
pass are the values of the array as a list:

[Link]([1,2,3,4])

Output:

array([1, 2, 3, 4])

This array contains integer values. You can specify the type of data in the dtype
argument:

[Link]([1,2,3,4],dtype=np.float32)

Output:

array([1., 2., 3., 4.], dtype=float32)

Since NumPy arrays can contain only homogeneous data types, values will be
upcast if the types do not match:

[Link]([1,2.0,3,4])
Output:

array([1., 2., 3., 4.])

Here, NumPy has upcast integer values to float values.


NumPy arrays can be multi-dimensional .

[Link]([[1,2,3,4],[5,6,7,8]])

array([[1, 2, 3, 4],

[5, 6, 7, 8]])

Here, we created a 2-dimensional array of values.

Note: A matrix is just a rectangular array of numbers with shape N x M where


N is the number of rows and M is the number of columns in the matrix. The one
you just saw above is a 2 x 4 matrix.

Array of zeros

NumPy lets you create an array of all zeros using the [Link]() method. All
you have to do is pass the shape of the desired array:

[Link](5)

array([0., 0., 0., 0., 0.])

The one above is a 1-D array while the one below is a 2-D array:

[Link]((2,3))

array([[0., 0., 0.],

[0., 0., 0.]])


Array of ones

You could also create an array of all 1s using the [Link]() method:

[Link](5,dtype=np.int32)

array([1, 1, 1, 1, 1])

Random numbers in ndarrays

Another very commonly used method to create ndarrays is [Link]()


method. It creates an array of a given shape with random values from [0,1):

# random

[Link](2,3)

array([[0.95580785, 0.98378873, 0.65133872],

[0.38330437, 0.16033608, 0.13826526]])

An array of your choice

Or, in fact, you can create an array filled with any given value using the
[Link]() method. Just pass in the shape of the desired array and the value you
want:

[Link]((2,2),7)
array([[7, 7],

[7, 7]])

Imatrix in NumPy

Another great method is [Link]() that returns an array with 1s along its diagonal
and 0s everywhere else.

An Identity matrix is a square matrix that has 1s along its main diagonal and
0s everywhere else. Below is an Identity matrix of shape 3 x 3.

Note: A square matrix has an N x N shape. This means it has the same number
of rows and columns.

# identity matrix

[Link](3)

array([[1., 0., 0.],


[0., 1., 0.],

[0., 0., 1.]])

Evenly spaced ndarray

You can quickly get an evenly spaced array of numbers using the [Link]()
method:

[Link](5)
array([0, 1, 2, 3, 4])

The start, end and step size of the interval of values can be explicitly defined by
passing in three numbers as arguments for these values respectively. A point to
be noted here is that the interval is defined as [start,end) where the last number
will not be included in the array:

[Link](2,10,2)

array([2, 4, 6, 8])

Alternate elements were printed because the step-size was defined as 2. Notice
that 10 was not printed as it was the last element.

Another similar function is [Link](), but instead of step size, it takes in the
number of samples that need to be retrieved from the interval. A point to note
here is that the last number is included in the values returned unlike in the case
of [Link]().

[Link](0,1,5)

array([0. , 0.25, 0.5 , 0.75, 1. ])

Great! Now you know how to create arrays using NumPy. But its also important
to know the shape of the array.
The Shape and Reshaping of NumPy Arrays

Once you have created your ndarray, the next thing you would want to do is
check the number of axes, shape, and the size of the ndarray.

Dimensions of NumPy arrays

You can easily determine the number of dimensions or axes of a NumPy array
using the ndims attribute:

# number of axis
a = [Link]([[5,10,15],[20,25,20]])
print('Array :','\n',a)

print('Dimensions :','\n',[Link])

Array :
[[ 5 10 15]
[20 25 20]]
Dimensions :
2

This array has two dimensions: 2 rows and 3 columns.

Shape of NumPy array

The shape is an attribute of the NumPy array that shows how many rows of
elements are there along each dimension. You can further index the shape so
returned by the ndarray to get value along each dimension:
a = [Link]([[1,2,3],[4,5,6]])
print('Array :','\n',a)
print('Shape :','\n',[Link])
print('Rows = ',[Link][0])
print('Columns = ',[Link][1])

Array :
[[1 2 3]
[4 5 6]]
Shape :
(2, 3)
Rows = 2

Columns = 3

Size of NumPy array

You can determine how many values there are in the array using the size
attribute. It just multiplies the number of rows by the number of columns in the
ndarray:

# size of array
a = [Link]([[5,10,15],[20,25,20]])
print('Size of array :',[Link])

print('Manual determination of size of array :',[Link][0]*[Link][1])

Size of array : 6
Manual determination of size of array : 6
Reshaping a NumPy array

Reshaping a ndarray can be done using the [Link]() method. It changes the
shape of the ndarray without changing the data within the ndarray:

# reshape
a = [Link]([3,6,9,12])

[Link](a,(2,2))

array([[ 3, 6],

[ 9, 12]])

Here, I reshaped the ndarray from a 1-D to a 2-D ndarray.

While reshaping, if you are unsure about the shape of any of the axis, just input
-1. NumPy automatically calculates the shape when it sees a -1:

a = [Link]([3,6,9,12,18,24])
print('Three rows :','\n',[Link](a,(3,-1)))

print('Three columns :','\n',[Link](a,(-1,3)))

Three rows :
[[ 3 6]
[ 9 12]
[18 24]]
Three columns :
[[ 3 6 9]
[12 18 24]]

Flattening a NumPy array

Sometimes when you have a multidimensional array and want to collapse it to a


single-dimensional array, you can either use the flatten() method or the ravel()
method:

a = [Link]((2,2))
b = [Link]()
c = [Link]()
print('Original shape :', [Link])
print('Array :','\n', a)
print('Shape after flatten :',[Link])
print('Array :','\n', b)
print('Shape after ravel :',[Link])

print('Array :','\n', c)

Original shape : (2, 2)


Array :
[[1. 1.]
[1. 1.]]
Shape after flatten : (4,)
Array :
[1. 1. 1. 1.]
Shape after ravel : (4,)
Array :

[1. 1. 1. 1.]

But an important difference between flatten() and ravel() is that the former
returns a copy of the original array while the latter returns a reference to the
original array. This means any changes made to the array returned from ravel()
will also be reflected in the original array while this will not be the case with
flatten().

b[0] = 0

print(a)

[[1. 1.]

[1. 1.]]

The change made was not reflected in the original array.

c[0] = 0

print(a)

[[0. 1.]
[1. 1.]]
But here, the changed value is also reflected in the original ndarray.

What is happening here is that flatten() creates a Deep copy of the ndarray
while ravel() creates a Shallow copy of the ndarray.

Deep copy means that a completely new ndarray is created in memory and the
ndarray object returned by flatten() is now pointing to this memory location.
Therefore, any changes made here will not be reflected in the original ndarray.

A Shallow copy, on the other hand, returns a reference to the original memory
location. Meaning the object returned by ravel() is pointing to the same memory
location as the original ndarray object. So, definitely, any changes made to this
ndarray will also be reflected in the original ndarray too.

Transpose of a NumPy array

Another very interesting reshaping method of NumPy is the transpose()


method. It takes the input array and swaps the rows with the column values, and
the column values with the values of the rows:

a = [Link]([[1,2,3],
[4,5,6]])
b = [Link](a)
print('Original','\n','Shape',[Link],'\n',a)

print('Expand along columns:','\n','Shape',[Link],'\n',b)

Original
Shape (2, 3)
[[1 2 3]
[4 5 6]]
Expand along columns:
Shape (3, 2)
[[1 4]
[2 5]
[3 6]]

On transposing a 2 x 3 array, we got a 3 x 2 array. Transpose has a lot of


significance in linear algebra.

Expanding and Squeezing a NumPy array

Expanding a NumPy array

You can add a new axis to an array using the expand_dims() method by
providing the array and the axis along which to expand:

# expand dimensions
a = [Link]([1,2,3])
b = np.expand_dims(a,axis=0)
c = np.expand_dims(a,axis=1)
print('Original:','\n','Shape',[Link],'\n',a)
print('Expand along columns:','\n','Shape',[Link],'\n',b)

print('Expand along rows:','\n','Shape',[Link],'\n',c)

Original:
Shape (3,)
[1 2 3]
Expand along columns:
Shape (1, 3)
[[1 2 3]]
Expand along rows:
Shape (3, 1)
[[1]
[2]
[3]]

Indexing and Slicing of NumPy array

So far, we have seen how to create a NumPy array and how to play around with
its shape. In this section, we will see how to extract specific values from the
array using indexing and slicing.

Slicing 1-D NumPy arrays

Slicing means retrieving elements from one index to another index. All we have
to do is to pass the starting and ending point in the index like this: [start: end].

However, you can even take it up a notch by passing the step-size. What is that?
Well, suppose you wanted to print every other element from the array, you
would define your step-size as 2, meaning get the element 2 places away from
the present index.
Incorporating all this into a single index would look something like this:
[start:end:step-size].

a = [Link]([1,2,3,4,5,6])

print(a[Link])

[2 4]

Notice that the last element did not get considered. This is because slicing
includes the start index but excludes the end index.

A way around this is to write the next higher index to the final index value you
want to retrieve:

a = [Link]([1,2,3,4,5,6])

print(a[Link])

[2 4 6]

If you don’t specify the start or end index, it is taken as 0 or array size,
respectively, as default. And the step-size by default is 1.

a = [Link]([1,2,3,4,5,6])
print(a[:6:2])
print(a[1::2])

print(a[Link])

[1 3 5]
[2 4 6]
[2 3 4 5 6]

Slicing 2-D NumPy arrays

Now, a 2-D array has rows and columns so it can get a little tricky to slice 2-D
arrays. But once you understand it, you can slice any dimension array!

Before learning how to slice a 2-D array, let’s have a look at how to retrieve an
element from a 2-D array:

a = [Link]([[1,2,3],
[4,5,6]])
print(a[0,0])
print(a[1,2])

print(a[1,0])

1
6
4

Here, we provided the row value and column value to identify the element we
wanted to extract. While in a 1-D array, we were only providing the column
value since there was only 1 row.

So, to slice a 2-D array, you need to mention the slices for both, the row and the
column:
a = [Link]([[1,2,3],[4,5,6]])
# print first row values
print('First row values :','\n',a[0:1,:])
# with step-size for columns
print('Alternate values from first row:','\n',a[0:1,::2])
#
print('Second column values :','\n',a[:,1::2])

print('Arbitrary values :','\n',a[0:1,1:3])

First row values :


[[1 2 3]]
Alternate values from first row:
[[1 3]]
Second column values :
[[2]
[5]]
Arbitrary values :
[[2 3]]

Slicing 3-D NumPy arrays

So far we haven’t seen a 3-D array. Let’s first visualize how a 3-D array looks
like:
a = [Link]([[[1,2],[3,4],[5,6]],# first axis array
[[7,8],[9,10],[11,12]],# second axis array
[[13,14],[15,16],[17,18]]])# third axis array
# 3-D array

print(a)

[[[ 1 2]
[ 3 4]
[ 5 6]]

[[ 7 8]
[ 9 10]
[11 12]]

[[13 14]
[15 16]
[17 18]]]

In addition to the rows and columns, as in a 2-D array, a 3-D array also has a
depth axis where it stacks one 2-D array behind the other. So, when you are
slicing a 3-D array, you also need to mention which 2-D array you are slicing.
This usually comes as the first value in the index:

# value
print('First array, first row, first column value :','\n',a[0,0,0])
print('First array last column :','\n',a[0,:,1])

print('First two rows for second and third arrays :','\n',a[1:,0:2,0:2])

First array, first row, first column value :


1
First array last column :
[2 4 6]
First two rows for second and third arrays :
[[[ 7 8]
[ 9 10]]

[[13 14]
[15 16]]]

If in case you wanted the values as a single dimension array, you can always use
the flatten() method to do the job!

print('Printing as a single array :','\n',a[1:,0:2,0:2].flatten())

Printing as a single array :


[ 7 8 9 10 13 14 15 16]
Negative slicing of NumPy arrays

An interesting way to slice your array is to use negative slicing. Negative slicing
prints elements from the end rather than the beginning. Have a look below:

a = [Link]([[1,2,3,4,5],
[6,7,8,9,10]])

print(a[:,-1])

[ 5 10]

Here, the last values for each row were printed. If, however, we wanted to
extract from the end, we would have to explicitly provide a negative step-size
otherwise the result would be an empty list.

print(a[:,-1:-3:-1])

[[ 5 4]
[10 9]]

Having said that, the basic logic of slicing remains the same, i.e. the end index
is never included in the output.

An interesting use of negative slicing is to reverse the original array.

a = [Link]([[1,2,3,4,5],
[6,7,8,9,10]])
print('Original array :','\n',a)
print('Reversed array :','\n',a[::-1,::-1])

Original array :
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
Reversed array :
[[10 9 8 7 6]
[ 5 4 3 2 1]]

You can also use the flip() method to reverse an ndarray.

a = [Link]([[1,2,3,4,5],
[6,7,8,9,10]])
print('Original array :','\n',a)
print('Reversed array vertically :','\n',[Link](a,axis=1))

print('Reversed array horizontally :','\n',[Link](a,axis=0))

Original array :
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
Reversed array vertically :
[[ 5 4 3 2 1]
[10 9 8 7 6]]
Reversed array horizontally :
[[ 6 7 8 9 10]
[ 1 2 3 4 5]]
Stacking and Concatenating NumPy arrays

Stacking ndarrays

You can create a new array by combining existing arrays. This you can do in
two ways:

● Either combine the arrays vertically (i.e. along the rows) using the
vstack() method, thereby increasing the number of rows in the resulting
array
● Or combine the arrays in a horizontal fashion (i.e. along the columns)
using the hstack(), thereby increasing the number of columns in the
resultant array

a = [Link](0,5)
b = [Link](5,10)
print('Array 1 :','\n',a)
print('Array 2 :','\n',b)
print('Vertical stacking :','\n',[Link]((a,b)))

print('Horizontal stacking :','\n',[Link]((a,b)))

Array 1 :
[0 1 2 3 4]
Array 2 :
[5 6 7 8 9]
Vertical stacking :
[[0 1 2 3 4]
[5 6 7 8 9]]
Horizontal stacking :
[0 1 2 3 4 5 6 7 8 9]

A point to note here is that the axis along which you are combining the array
should have the same size otherwise you are bound to get an error!

a = [Link](0,5)
b = [Link](5,9)
print('Array 1 :','\n',a)
print('Array 2 :','\n',b)
print('Vertical stacking :','\n',[Link]((a,b)))

print('Horizontal stacking :','\n',[Link]((a,b)))


Concatenating ndarrays

While stacking arrays is one way of combining old arrays to get a new one, you
could also use the concatenate() method where the passed arrays are joined
along an existing axis:

a = [Link](0,5).reshape(1,5)
b = [Link](5,10).reshape(1,5)
print('Array 1 :','\n',a)
print('Array 2 :','\n',b)
print('Concatenate along rows :','\n',[Link]((a,b),axis=0))

print('Concatenate along columns :','\n',[Link]((a,b),axis=1))

Array 1 :
[[0 1 2 3 4]]
Array 2 :
[[5 6 7 8 9]]
Concatenate along rows :
[[0 1 2 3 4]
[5 6 7 8 9]]
Concatenate along columns :
[[0 1 2 3 4 5 6 7 8 9]]

The drawback of this method is that the original array must have the axis along
which you want to combine. Otherwise, get ready to be greeted by an error.

Another very useful function is the append method that adds new elements to
the end of a ndarray. This is obviously useful when you already have an existing
ndarray but want to add new values to it.

# append values to ndarray


a = [Link]([[1,2],
[3,4]])

[Link](a,[[5,6]], axis=0)

array([[1, 2],
[3, 4],

[5, 6]])

Broadcasting in NumPy arrays – A class apart!

Broadcasting is one of the best features of ndarrays. It lets you perform


arithmetics operations between ndarrays of different sizes or between an ndarray
and a simple number!
Broadcasting essentially stretches the smaller ndarray so that it matches the
shape of the larger ndarray:

a = [Link](10,20,2)
b = [Link]([[2],[2]])
print('Adding two different size arrays :','\n',a+b)

print('Multiplying an ndarray and a number :',a*2)

Adding two different size arrays :


[[12 14 16 18 20]
[12 14 16 18 20]]

Multiplying an ndarray and a number : [20 24 28 32 36]

Its working can be thought of like stretching or making copies of the scalar, the
number, [2, 2, 2] to match the shape of the ndarray and then perform the
operation element-wise. But no such copies are being made. It is just a way of
thinking about how broadcasting is working.

This is very useful because it is more efficient to multiply an array with a scalar
value rather than another array! It is important to note that two ndarrays can
broadcast together only when they are compatible.

Ndarrays are compatible when:

1. Both have the same dimensions


2. Either of the ndarrays has a dimension of 1. The one having a dimension
of 1 is broadcast to meet the size requirements of the larger ndarray

In case the arrays are not compatible, you will get a ValueError.

a = [Link]((3,3))
b = [Link]([2])
a+b

array([[3., 3., 3.],


[3., 3., 3.],

[3., 3., 3.]])

Here, the second ndarray was stretched, hypothetically, to a 3 x 3 shape, and


then the result was calculated.

NumPy Ufuncs – The secret of its success!

Python is a dynamically typed language. This means the data type of a variable
does not need to be known at the time of the assignment. Python will
automatically determine it at run-time. While this means a cleaner and easier
code to write, it also makes Python sluggish.

This problem manifests itself when Python has to do many operations


repeatedly, like the addition of two arrays. This is so because each time an
operation needs to be performed, Python has to check the data type of the
element. This problem is overcome by NumPy using the ufuncs function.

The way NumPy makes this work faster is by using vectorization.


Vectorization performs the same operation on ndarray in an element-by-element
fashion in a compiled code. So the data types of the elements do not need to be
determined every time, thereby performing faster operations.

ufuncs are Universal functions in NumPy that are simply mathematical


functions. They perform fast element-wise functions. They are called
automatically when you are performing simple arithmetic operations on NumPy
arrays because they act as wrappers for NumPy ufuncs.
For example, when adding two NumPy arrays using ‘+’, the NumPy ufunc add()
is automatically called behind the scene and quietly does its magic:

a = [1,2,3,4,5]
b = [6,7,8,9,10]

%timeit a+b

a = [Link](1,6)
b = [Link](6,11)

%timeit a+b

You can see how the same addition of two arrays has been done in significantly
less time with the help of NumPy ufuncs!

Maths with NumPy arrays

Here are some of the most important and useful operations that you will need to
perform on your NumPy array.

Basic arithmetic operations on NumPy arrays

The basic arithmetic operations can easily be performed on NumPy arrays. The
important thing to remember is that these simple arithmetics operation symbols
just act as wrappers for NumPy ufuncs.

print('Subtract :',a-5)
print('Multiply :',a*5)
print('Divide :',a/5)
print('Power :',a**2)

print('Remainder :',a%5)

Subtract : [-4 -3 -2 -1 0]
Multiply : [ 5 10 15 20 25]
Divide : [0.2 0.4 0.6 0.8 1. ]
Power : [ 1 4 9 16 25]

Remainder : [1 2 3 4 0]

Mean, Median and Standard deviation

To find the mean and standard deviation of a NumPy array, use the mean(),
std() and median() methods:

a = [Link](5,15,2)
print('Mean :',[Link](a))
print('Standard deviation :',[Link](a))

print('Median :',[Link](a))

Mean : 9.0
Standard deviation : 2.8284271247461903
Median : 9.0

Min-Max values and their indexes

Min and Max values in an ndarray can be easily found using the min() and
max() methods:
a = [Link]([[1,6],
[4,3]])
# minimum along a column
print('Min :',[Link](a,axis=0))
# maximum along a row

print('Max :',[Link](a,axis=1))

Min : [1 3]
Max : [6 4]

You can also easily determine the index of the minimum or maximum value in
the ndarray along a particular axis using the argmin() and argmax() methods:

a = [Link]([[1,6,5],
[4,3,7]])
# minimum along a column
print('Min :',[Link](a,axis=0))
# maximum along a row

print('Max :',[Link](a,axis=1))

Min : [0 1 0]
Max : [1 2]

Let me break down the output for you. The minimum value for the first column
is the first element along the column. For the second column, it is the second
element. And for the third column, it is the first element.

You can similarly determine what the output for maximum values indicates.
Sorting in NumPy arrays

For any programmer, the time complexity of any algorithm is of prime essence.
Sorting is an important and very basic operation that you might well use on a
daily basis as a data scientist. So, it is important to use a good sorting algorithm
with minimum time complexity.

The NumPy library is a legend when it comes to sorting elements of an array. It


has a range of sorting functions that you can use to sort your array elements. It
has implemented quicksort, heapsort, mergesort, and timesort for you under the
hood when you use the sort() method:

a = [Link]([1,4,2,5,3,6,8,7,9])

[Link](a, kind='quicksort')

array([1, 2, 3, 4, 5, 6, 7, 8, 9])

You can even sort the array along any axis you desire:

a = [Link]([[5,6,7,4],
[9,2,3,7]])# sort along the column
print('Sort along column :','\n',[Link](a, kind='mergresort',axis=1))
# sort along the row

print('Sort along row :','\n',[Link](a, kind='mergresort',axis=0))

Sort along column :


[[4 5 6 7]
[2 3 7 9]]
Sort along row :
[[5 2 3 4]
[9 6 7 7]]
Min and Max Function in Numpy
import numpy as np

arr = [Link]([[3,5,6],[7,8,9]])

print("The array is: ")

print(arr)

print("\n")

print("Min value is: ")

print([Link](arr))

print("\n")

print("Max value is: ")

print([Link](arr))

Output:

The array is:

[[3 5 6]

[7 8 9]]
Min value is:

Max value is:

Finding the Median in Numpy


Median function in numpy is used for finding the median of the array. Let’s look

at an example to find out about it:

import numpy as np

arr = [Link]([[10,20,30],[40,50,60],[70,80,90]])

print("The array is: ")

print(arr)
print("\n")

print("Median function applied: ")

print([Link](arr))

Output:

The array is:

[[10 20 30]

[40 50 60]

[70 80 90]]

Median function applied:

50.0

Finding the Mean in Numpy


The mean function in numpy is used for calculating the mean of the elements

present in the array. You can calculate the mean by using the axis number as

well but it only depends on a special case, normally if you want to find out the

mean of the whole array then you should use the simple [Link]() function. For

example:

import numpy as np

arr = [Link]([[10,20,30],[40,50,60],[70,80,99]])

print("The array is: ")

print(arr)

print("\n")

print("Mean function applied: ")

print([Link](arr))

print("Mean function applied on axis 1: ")

print([Link](arr, axis=1))

Output:

The array is:

[[10 20 30]
[40 50 60]

[70 80 99]]

Mean function applied:

51.0

Mean function applied on axis 1:

[20. 50. 83.]

Finding Standard Deviation in Numpy


You can find out the standard deviation of an array using the std() function, for

example:

import numpy as np

arr = [Link]([10,20,30])

print("The standard deviation of the array is: ")


print([Link](arr))

Output:

The standard deviation of the array is:

8.16496580927726

Finding Variance in Numpy

As you may or may not know, that variance is the mean (average) of squared

deviations, and in order to calculate the variance in numpy we use the var()

function.

import numpy as np

arr = [Link]([10,20,30])

print("The variance of the array is: ")

print([Link](arr))

Output:

The variance of the array is:

66.66666666666667
NumPy arrays and Images

NumPy arrays find wide use in storing and manipulating image data. But what
is image data really?

Images are made up of pixels that are stored in the form of an array. Each pixel
has a value ranging between 0 to 255 – 0 indicating a black pixel and 255
indicating a white pixel. A colored image consists of three 2-D arrays, one for
each of the color channels: Red, Green, and Blue, placed back-to-back thus
making a 3-D array. Each value in the array constitutes a pixel value. So, the
size of the array depends on the number of pixels along each dimension.

Have a look at the image below:

Python can read the image as an array using the [Link]() method in
the SciPy library. And when we output it, it is simply a 3-D array containing the
pixel values:
import numpy as np
import [Link] as plt
from scipy import misc

# read image
im = [Link]('./[Link]')
# image

im

array([[[115, 106, 67],


[113, 104, 65],
[112, 103, 64],
...,
[160, 138, 37],
[160, 138, 37],
[160, 138, 37]],

[[117, 108, 69],


[115, 106, 67],
[114, 105, 66],
...,
[157, 135, 36],
[157, 135, 34],
[158, 136, 37]],

[[120, 110, 74],


[118, 108, 72],
[117, 107, 71],

...,

We can check the shape and type of this NumPy array:

print([Link])
print(type(type))

(561, 997, 3)

[Link]

Now, since an image is just an array, we can easily manipulate it using an array
function that we have looked at in the article. Like, we could flip the image
horizontally using the [Link]() method:

# flip

[Link]([Link](im, axis=1))

Or you could normalize or change the range of values of the pixels. This is
sometimes useful for faster computations.

im/255

array([[[0.45098039, 0.41568627, 0.2627451 ],


[0.44313725, 0.40784314, 0.25490196],
[0.43921569, 0.40392157, 0.25098039],
...,
[0.62745098, 0.54117647, 0.14509804],
[0.62745098, 0.54117647, 0.14509804],
[0.62745098, 0.54117647, 0.14509804]],

[[0.45882353, 0.42352941, 0.27058824],


[0.45098039, 0.41568627, 0.2627451 ],
[0.44705882, 0.41176471, 0.25882353],
...,
[0.61568627, 0.52941176, 0.14117647],
[0.61568627, 0.52941176, 0.13333333],
[0.61960784, 0.53333333, 0.14509804]],

[[0.47058824, 0.43137255, 0.29019608],


[0.4627451 , 0.42352941, 0.28235294],
[0.45882353, 0.41960784, 0.27843137],
...,
[0.6 , 0.52156863, 0.14117647],
[0.6 , 0.52156863, 0.13333333],
[0.6 , 0.52156863, 0.14117647]],

...,

You might also like