Python Unit-4 Notes
Python Unit-4 Notes
NumPy
NumPy is a Python library used for working with arrays. It also has
functions for working in domain of linear algebra, fourier transform,
and matrices. NumPy was created in 2005 by Travis Oliphant. It is an
open source project and you can use it freely. NumPy stands for
Numerical Python.
In Python we have lists that serve the purpose of arrays, but they are
slow to process. NumPy aims to provide an array object that is up to
50x faster than traditional Python lists.
The array object in NumPy is called ndarray, it provides a lot of
supporting functions that make working with ndarray very easy.
import numpy
Example
import numpy
arr = numpy.array([1, 2, 3, 4, 5]) # array() is used to create an array
print(arr)
NumPy is usually imported under the np alias.
alias: In Python alias are an alternate name for referring to the same
thing.
Create an alias with the as keyword while importing:
import numpy as np
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Dimensions in Arrays
A dimension in arrays is one level of array depth (nested arrays).
0-D Arrays
0-D arrays, or Scalars, are the elements in an array. Each value in an
array is a 0-D array.
Example Output:
Create a 0-D array with value 42 42
import numpy as np
arr = np.array(42)
print(arr)
1-D Arrays
An array that has 0-D arrays as its elements is called uni-dimensional or
1-D array.
These are the most common and basic arrays.
Example Output:
Create a 1-D array containing the [1 2 3 4 5]
values 1,2,3,4,5:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
2-D Arrays
An array that has 1-D arrays as its elements is called a 2-D array.
These are often used to represent matrix or 2nd order tensors.
Example Output:
Create a 2-D array containing
two arrays with the values 1,2,3 [[ 1 2 3]
and 4,5,6: [4 5 6]]
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
3-D arrays
An array that has 2-D arrays (matrices) as its elements is called 3-D
array.
These are often used to represent a 3rd order tensor.
Example Output:
Create a 3-D array with two 2-D
[[[1 2 3]
arrays, both containing two
[4 5 6]]
arrays with the values 1,2,3 and
4,5,6: [[1 2 3]
import numpy as np [4 5 6]]]
print(arr)
To Check Number of Dimensions?
NumPy Arrays provides the ndim attribute that returns an integer that
tells us how many dimensions the array has.
Example: Output:
Program to check how many
0
dimensions the arrays have:
1
import numpy as np 2
3
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]],
[[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
import numpy as np
print(arr)
print('number of dimensions :',
arr.ndim)
Example 3 Output:
Get third and fourth elements 7
from the following array and add
them.
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[2] + arr[3])
Explanation:
arr[0, 1, 2] prints the value 6.
And this is why:
The first number represents the first dimension, which contains two
arrays:
[[1, 2, 3], [4, 5, 6]]
and:
[[7, 8, 9], [10, 11, 12]]
Since we selected 0, we are left with the first array:
[[1, 2, 3], [4, 5, 6]]
The second number represents the second dimension, which also
contains two arrays:
[1, 2, 3]
and:
[4, 5, 6]
Since we selected 1, we are left with the second array:
[4, 5, 6]
The third number represents the third dimension, which contains three
values:
4
5
6
Since we selected 2, we end up with the third value:
6
import numpy as np
arr=np.array([1,3,5,7,9])
sum=arr[0]+arr[2]
diff=arr[3]-arr[2]
product=arr[1]*arr[3]
div=arr[4]/arr[1]
print("sum of 2 numbers=",sum)
print("Difference of 2 numbers=",diff)
print("Product of 2 numbers=",product)
print("Division of 2 numbers=",div)
import numpy as np
arr=np.array([[1,3,5,7,9],[11,13,15,17,19]])
sum=arr[0,1]+arr[1,1]
diff=arr[1,3]-arr[0,2]
product=arr[1,1]*arr[0,3]
div=arr[1,2]/arr[0,2]
print("sum of 2 numbers=",sum)
print("Difference of 2 numbers=",diff)
print("Product of 2 numbers=",product)
print("Division of 2 numbers=",div)
Negative Indexing
Use negative indexing to access an array from the end.
Example OUTPUT:
Last element from 2nd dim: 10
Print the last element from the
2nd dim:
import numpy as np
arr = np.array([[1,2,3,4,5],
[6,7,8,9,10]])
print('Last element from 2nd dim:
', arr[1, -1])
import numpy as np
arr = np.array([2,4,6,8,10,12,14,16])
print(arr[3:6]) # will print the elements from 3 to 5 [ 8 10 12]
print(arr[:5]) # will print the elements from 0 to 4 [ 2 4 6 8 10]
print(arr[2:]) # will print the elements from 2 to [ 6 8 10 12 14
length of the array. 16]
print(arr[-5:-1]) # will print from the end i.e. -5 to - [ 8 10 12 14]
2 [ 2 4 6 8 10 12
print(arr[:-1]) # will print from end i.e. 0 to -2 14]
STEP
Use the step value to determine the step of the slicing:
Example: Output:
Return every other element from index 1 to
[2 4]
index 5:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5:2])
import numpy as np [ 7 8 9]
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[1, 1:4])
Example 2: Output:
From both elements, return index 2:
import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) [ 3 8]
print(arr[0:2, 2])
Example 3: Output:
From both elements, slice index 1 to index 4
(not included), this will return a 2-D array:
import numpy as np [[ 2 3 4]
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) [7 8 9]]
print(arr[0:2, 1:4])
print(arr) print(arr)
print(x) print(x)
OUTPUT: OUTPUT:
[42 2 3 4 5] [42 2 3 4 5]
[1 2 3 4 5] [42 2 3 4 5]
Example 2: Output
Syntax:
Example 1: Output:
Example 2: Output:
import numpy as np
np.arange(1, 14, 4)
ndarray.ndim
Matrix Addition
To perform addition on the matrix, we will create two matrices using
numpy.array() and add them using the (+) operator.
Example: Output:
Matrix Subtraction
To perform subtraction on the matrix, we will create two matrices using
numpy.array() and subtract them using the (-) operator.
Example: Output:
Matrix Multiplication
First will create two matrices using numpy.arary(). To multiply them,
you can make use of numpy.dot() method. Numpy.dot() is the dot
product of matrix M1 and M2. Numpy.dot() handles the 2D arrays and
perform matrix multiplications.
Example: Output:
Matrix Transpose
The transpose of a matrix is calculated, by changing the rows as
columns and columns as rows. The transpose() function from Numpy
can be used to calculate the transpose of a matrix.
Example 1: Output:
import numpy as np [[ 3 5 4]
[ 6 -10 8]
M1 = np.array([[3, 6, 9], [5, -10, 15], [ 9 15 12]]
[4,8,12]])
M2 = M1.transpose()
print(M2)
Example 2: Output:
In the above array we have total 4 rows ( starting from 0,1,2,3) and 5
(starting from 0,1,2,3,4) columns i.e.,
print(M1)
[[2,4,6,8,10]
[3,6,9,-12,-15]
[4,8,12,16,-20]
[5,-10,15,-20,25]
To print row number 1 and 2 and column 1,2,3 then the statement we
use is,
print(M1[1:3,1:4])
def READ_DATA(R,C,matrix):
for i in range(m):
for j in range(n):
matrix[i][j]=int(input())
import numpy
m= int(input("\n Enter the row size of M "))
n=int(input("\nEnter the columnn size of N"))
matrix=numpy.ndarray(shape=(m,n), dtype=int)
print("\n size:",matrix.size)
print("\n shape: ",matrix.shape)
print("\nDimensions:",matrix.ndim)
print("\n Enter %d elements of %dx%d matrix" %(m*n,m,n))
READ_DATA(m,n,matrix)
print("\n %dx%d matrix are:"%(m,n))
print(matrix)
print("Diagnol Elements of Matrix :")
dia=matrix.diagonal()
print(dia)
r=int(input("enter row number to display"))
print(matrix[r])
c=int(input("Enter Col number to display"))
print(matrix[:,c])
Iterating Arrays
• Iterating means going through elements one by one.
• As we deal with multi-dimensional arrays in numpy, we can do
this using basic for loop of python.
• If we iterate on a 1-D array it will go through each element one by
one
Example :
import numpy as np
arr = np.array([1, 2, 3])
for x in arr:
print(x)
Example: OUTPUT:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
split_arr = np.split(arr, 3, axis=1) # Split into 3 arrays along columns (horizontal)
print(split_arr)
Assignment Question-1:
Illustrate a Numpy program to test element-wise for complex number, real number of a given
array. Also test whether a given number is a scalar type or not.
Pandas
Pandas is a Python library used for working with data sets.
It has functions for analyzing, cleaning, exploring, and manipulating
data.
The name "Pandas" has a reference to both "Panel Data", and
"Python Data Analysis" and was created by Wes McKinney in 2008.
Pandas are also able to delete rows that are not relevant, or contains
wrong values, like empty or NULL values. This is called cleaning the
data.
import pandas
Example
import pandas
mydataset = {
'cars': ["BMW", "Volvo", "Ford"],
'passings': [3, 7, 2]
}
myvar = pandas.DataFrame(mydataset)
print(myvar)
import pandas as pd
Example
import pandas as pd
mydataset = {
'cars': ["BMW", "Volvo", "Ford"],
'passings': [3, 7, 2]
}
myvar = pd.DataFrame(mydataset)
print(myvar)
Checking Pandas Version
The version string is stored under __version__ attribute.
Example
import pandas as pd
print(pd.__version__)
What is a Series?
A Pandas Series is like a column in a table.
It is a one-dimensional array holding data of any type.
Example
Create a simple Pandas Series from a list:
import pandas as pd
a = [1, 7, 2]
myvar = pd.Series(a)
print(myvar)
Labels
If nothing else is specified, the values are labeled with their index
number. First value has index 0, second value has index 1 etc.
This label can be used to access a specified value.
Example
Return the first value of the Series:
print(myvar[0])
Create Labels
With the index argument, you can name your own labels.
Example
Create you own labels:
import pandas as pd
a = [1, 7, 2]
print(myvar)
When you have created labels, you can access an item by referring to
the label.
Example
Return the value of "y":
print(myvar["y"])
print(myvar)
Note: The keys of the dictionary become the labels.
To select only some of the items in the dictionary, use the index
argument and specify only the items you want to include in the Series.
Example
Create a Series using only data from "day1" and "day2":
import pandas as pd
print(myvar)
DataFrames
Data sets in Pandas are usually multi-dimensional tables, called
DataFrames.
Series is like a column, a DataFrame is the whole table.
Example
Create a DataFrame from two Series:
import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
myvar = pd.DataFrame(data)
print(myvar)
What is a DataFrame?
A Pandas DataFrame is a 2 dimensional data structure, like a 2
dimensional array, or a table with rows and columns.
Example
Create a simple Pandas DataFrame:
import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
print(df)
Result
calories duration
0 420 50
1 380 40
2 390 45
Locate Row
As you can see from the result above, the DataFrame is like a table with
rows and columns.
Pandas use the iloc attribute to return one or more specified row(s)
Example
Return row 0:
#refer to the row index:
print(df.iloc[0])
Result
calories 420
duration 50
Name: 0, dtype: int64
Result
calories duration
0 420 50
1 380 40
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
print(df)
Result
calories duration
day1 420 50
day2 380 40
day3 390 45
Result
calories 380
duration 40
Name: 0, dtype: int64
df.describe( )
df.mean(axis=’rows’)
df.mean(axis=’columns’)
To List the unique values of the series and count the value
frequencies.
df.unique( )
df.value_counts()
df1.corrwith(df2)
Assignment Question-1:
df = pd.read_csv('data.csv')
print(df)
Data Visualization:
In today’s world, a lot of data is being generated daily. And sometimes
to analyze this data for certain trends, patterns may become difficult if
the data is in its raw format. To overcome this data visualization comes
into play. Data visualization provides a good, organized pictorial
representation of the data which makes it easier to understand,
observe, and analyze.
Python provides various libraries that come with different features for
visualizing data. All these libraries come with different features and can
support various types of graphs.
matplotlib:
Graphs:
1. Scatter Plot: A scatter plot (aka scatter chart, scatter graph) uses
dots to represent values for two different numeric variables. The
position of each dot on the horizontal and vertical axis indicates
values for an individual data point. Scatter plots are used to
observe relationships between variables.
import pandas as pd
import matplotlib.pyplot as plt
weather = pd.read_csv('D:/Test.csv')
plt.scatter(weather['humidity'], weather['temperature'])
plt.show()
2. Bar Chart
A bar plot or bar chart is a graph that represents the category of data
with rectangular bars with lengths and heights that is proportional to
the values which they represent. It can be created using the bar()
method.
Program Code:
import pandas as pd
import matplotlib.pyplot as plt
weather = pd.read_csv('D:/Test.csv')
plt.bar(weather['humidity'], weather['temperature'])
plt.show()
3. Histogram
A histogram is basically used to represent data in the form of some
groups. It is a type of bar plot where the X-axis represents the bin
ranges while the Y-axis gives information about frequency. The hist()
function is used to compute and create a histogram. In histogram, if we
pass categorical data then it will automatically compute the frequency
of that data i.e. how often each value occurred.
Program Code:
import pandas as pd
import matplotlib.pyplot as plt
plt.hist(weather['temperature'])
plt.title("Histogram")
4. Line Plot
Line Plot in Seaborn plotted using the lineplot() method. In this, we can
pass only the data argument also.
Program Code:
Program Code:
5. Pie plot
A Pie Chart is a circular statistical plot that can display only one series
of data. The area of the chart is the total percentage of the given data.
Pie charts are commonly used in business presentations like sales,
operations, survey results, resources, etc. as they provide a quick
summary.
Program Code:
Assignment Question:
As a part of skill development program planning to offer one
of the programming language Python, C++, Java, Nodejs,
Reactjs. A google form has been circulated to students to
know which course has demand. Consider total student
strength consider as 250. Assume other values. Make use of
pie chart to represent the percentage of students opted for
each course.