Rahul ML File' (1) 2
Rahul ML File' (1) 2
Aim: Write a Program to implement Mean, Median, Mode and Variance using Python.
Theory:
Mean:
mean() function can be used to calculate mean/average of a given list of numbers. It returns mean of the
data set passed as parameters.
Arithmetic mean is the sum of data divided by the number of data-points. It is a measure of the central
location of data in a set of values which vary in range. In Python, we usually do this by dividing the sum
of given numbers with the count of number present.
Median:
median() function in the statistics module can be used to calculate median value from an unsorted data-
list. The biggest advantage of using median() function is that the data-list does not need to be sorted
before being sent as parameter to the median() function.
Median is the value that separates the higher half of a data sample or probability distribution from the
lower half. For a dataset, it may be thought of as the middle value. The median is the measure of the
central tendency of the properties of a data-set in statistics and probability theory. For odd set of elements,
the median value is the middle one.
For even set of elements, the median value is the mean of two middle elements.
Mode:
The mode of a set of data values is the value that appears most often. It is the value at which the data is
most likely to be sampled. A mode of a continuous probability distribution is often considered to be any
value x at which its probability density function has a local maximum value, so any peak is a mode.
The mode() function is one of such methods. This function returns the robust measure of a central data
point in a given range of data-sets.
Variance:
variance() function should only be used when variance of a sample needs to be calculated. There’s another
function known as pvariance(), which is used to calculate the variance of an entire population.
In pure statistics, variance is the squared deviation of a variable from its mean. Basically, it measures the
spread of random data in a set from its mean or median value. A low value for variance indicates that the
data are clustered together and are not spread apart widely, whereas a high value would indicate that the
data in the given set are much more spread apart from the average value.
Procedure:
Mean
To calculate the mean, find the sum of all values, and divide the sum by the number of values:
(99+86+87+88+111+86+103+87+94+78+77+85+86) / 13 = 89.77
If there are two numbers in the middle, divide the sum of those numbers by two.77,
78, 85, 86, 86, 86, 87, 87, 94, 98, 99, 103
The median value is the value in the middle, after you have sorted all the values:
77, 78, 85, 86, 86, 86, 87, 87, 88, 94, 99, 103, 111
Mode:
The Mode value is the value that appears the most number of times:
99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86 = 86
Variance:
Variance is an important tool in the sciences, where statistical analysis of data is common. It is the square
of standard deviation of the given data-set and is also known as second central moment of a distribution. It
is usually represented by s^{2}, sigma ^{2}, Var (X) in pure Statistics.
Var(X)= E[(X-µ)2]
Algorithm/Program:
(i) Mean:
import numpy
speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]
x = numpy.mean(speed)
print(x)
import numpy
speed =
[99,86,87,88,111,86,103,87,94,78,77,85,86] x =
numpy.median(speed) print(x)
[99,86,87,88,86,103,87,94,78,77,85,86] x =
numpy.median(speed)
print(x)
(iii) Mode:
//Use the SciPy mode() method to find the number that appears the
of data speed =
[99,86,87,88,111,86,103,87,94,78,77,85,86]
%(statistics.variance(speed)))
Roll No. Name of Date of Date of Grade Sign of Sign of
Student Performance Evaluation Student Faculty
0818IT21104 Rahul Jadhaw
8
Experiment – 2
Aim: Write a program to implement:
NumPy is a Python library that can be used for scientific and numerical applications and is the tool to use
for linear algebra operations. The main data structure in NumPy is the ndarray, which is a shorthand name
for N-dimensional array. When working with NumPy, data in anndarray is simply referred to as an array.
It is a fixed-sized array in memory that contains data of the same type, such as integers or floating point
values. The data type supported by an array can be accessed via the dtype attribute on the array. The
dimensions of an array can be accessed via the shape attribute that returns a tuple describing the length of
each dimension. There are a host of other attributes. A simple way to create an array from data or simple
Python data structures like a list is to use the array() function. The example below creates a Python list of
3 Floating point values, then creates anndarray from the list and access the arrays' shape and data type.
The empty() function will create a new array of the specified shape. The argument to the function is an
array or tuple that specified the length of each dimension of the array to create. The values or content of
the created array will be random and will need to be assigned before use. The example below creates an
empty 3 , 3 two-dimensional array.
The zeros() function will create a new array of the specied size with the contents filled with zero values.
The argument to the function is an array or tuple that species the length of each dimension of the array to
create. The example below creates a 3,5 zero two-dimensional array.
The ones() function will create a new array of the specified size with the contents called with one values.
The argument to the function is an array or tuple that specifies the length of each dimension of the array to
create. The example below creates a 5-element one-dimensional array.
(iii) Combining
Arrays Vertical
Stack
Given two or more existing arrays, you can stack them vertically using the vstack() function. For example,
given two one-dimensional arrays, you can create a new two-dimensional array with two rows by
vertically stacking them. This is demonstrated in the example below.
Horizontal Stack
Given two or more existing arrays, you can stack them horizontally using the hstack() function. For
example, given two one-dimensional arrays, you can create a new one-dimensional array or one row with
the columns of the first and second arrays concatenated.
Algorithm / Program:
# create array
# create array l
a = array(l) #
display array
print(a)
print(a.shape)
print(a.dtype)
import empty a =
empty([3,3])
print(a)
print(a.dtype)
print(a)
print(a.dtype)
import onesa =
ones([3,3])
print(a)
print(a.dtype)
(iii)Combining Arrays:
array([1,2,3])
print(a1)
array([4,5,6])
print(a2) #
vertical stack
a3 =
vstack((a1,
a2))print(a3)
print(a3.shape)
b)Horizontal Stack: #
array([1,2,3]) print(a1)
a2 = array([4,5,6])
print(a2) # create
horizontal stack a3 =
hstack((a1, a2))
print(a3)
print(a3.shape)
Theory:
In general, we recommend loading your data using Pandas or even NumPy functions. This
section assumes you have loaded or generated your data by other means and it is now represented
using Python lists. Let's look at converting your data in lists to NumPy arrays.
You may load your data or generate your data and have access to it as a list. You can convert a
one-dimensional list of data to an array by calling the array() NumPy function.
It is more likely in machine learning that you will have two-dimensional data. That is a table of
data where each row represents a new observation and each column a new feature. Perhaps you
generated the data or loaded it using custom code and now you have a list of lists. Each list
represents a new observation. You can convert your list of lists to a NumPy array the same way
as above, by calling the array() function.
2. One-Dimensional Indexing:
Array Indexing
Once your data is represented using a NumPy array, you can access it using indexing. Let's look
at some examples of accessing data via indexing.
Generally, indexing works just like you would expect from your experience with other
programming languages, like Java, C#, and C++. For example, you can access elements using the
bracket operator [] specifying the zero-o set index for the value to retrieve.
Two-Dimensional Indexing:
Indexing two-dimensional data is similar to indexing one-dimensional data, except that a comma
is used to separate the index for each dimension.
3. Array Slicing:
Array Slicing So far, so good; creating and indexing arrays looks familiar. Now we come to array
slicing, and this is one feature that causes problems for beginners to Python and NumPy arrays.
Structures like lists and NumPy arrays can be sliced. This means that a subsequence of the
structure can be indexed and retrieved. This is most useful in machine learning when specifying
input variables and output variables, or splitting training rows from testing rows. Slicing is
specified using the colon operator : with a from and to index before and after the column
respectively. The slice extends from the from index and ends one item before the to index.
Reshape 2D to 3D Array It is common to need to reshape two-dimensional data where each row
represents a sequence into a three-dimensional array for algorithms that expect multiple samples
of one or more time steps and one or more features. A good example is the LSTM recurrent
neural network model in the Keras deep learning library. The reshape function can be used
directly, specifying the new dimensionality. This is clear with an example where each sequence
has multiple time steps with one observation (feature) at each time step. We can use the sizes in
the shape attribute on the array to specify the number of samples (rows) and columns.
Program:
# list of data
data =
array(data)
print(data)
print(type(data))
# list of data
data = [[11,
of data data =
array(data)
print(data)
print(type(data))
2.One-Dimensional Indexing:
# index data
print(data[0])
print(data[4])
# index array out of bounds
print(data[5])
Two-Dimensional Indexing: #
[55, 66]]) #
index data
print(data[0,0])
# define array
data =
array([ [11,
[55, 66]]) #
index data
print(data[0,])
3. Array Slicing:
4. Data Shape
# shape of one-dimensional
print(data.shape)
44],
of data data =
array(data)
print(data.shape)
# row and column shape of two-dimensional array
22],[33, 44],
data.shape[1])
print(data.shape) # reshape
print(data.shape)
# list of data
data = [[11,
print(data.shape)
1. Vector Arithmetic
2. Vector Dot Product
3. Vector-Scalar Multiplication
Theory:
Vector: -
A vector is a tuple of one or more values called scalars. Vectors are built from
components, which are ordinary numbers. You can think of a vector as a list of numbers,
and vector algebra as operations performed on the numbers in the list. Vectors are often
represented using a lowercase character such as v;
Defining a Vector: -
We can represent a vector in Python as a NumPy array. A NumPy array can be created from
a list of numbers. For example, a vector with the length of 4 and the integer values 1, 2, 3
and 4.
V = <1 2 3 4>
Vector Arithmetic
Two vectors of equal length can be added together to create a new third vector.
c = a + b. The new vector has the same length as the other two vectors. Each element of the
new vector is calculated as the addition of the elements of the other vectors at the same
index;For example: c = <a1 + b1; a2 + b2; a3 + b3>
Vector Subtraction
One vector can be subtracted from another vector of equal length to create a new third
vector. c = a - b.
As with addition, the new vector has the same length as the parent vectors and each element
of the new vector is calculated as the subtraction of the elements at the same indices.
c = <a1 - b1; a2 - b2; a3 - b3>
Vector Multiplication
Two vectors of equal length can be multiplied together. c = a * b. As with addition and
subtraction, this operation is performed element-wise to result in a new vector of the
same length.c = <a1* b1; a2 * b2; a3 * b3> or c = <a1b1; a2b2; a3b3>
Vector Division
Two vectors of equal length can be divided. c =a/b
As with other arithmetic operations, this operation is performed element-wise to result in a
new vector of the same length. c = <a1/b1; a2/b2; a3/b3>
Vector-Scalar Multiplication
A vector can be multiplied by a scalar, to scale the magnitude of the vector. To keep notation
simple, we will use lowercase s to represent the scalar value.
c = s * v or c = sv. The multiplication is performed on each element of the vector to result in
a new scaled vector of the same length. c = (s * v1; s * v2; s * v3)
Procedure:
Vector Addition: -
To add two vectors, perform element wise addition operation.
V1 = <1 4 9 5> and V2 = < 4 5 0 2>
V1 + V2 = <1+5; 4+5; 9+0; 5+2> = <6; 9; 9; 7>
Vector Subtraction: -
To subtract two vectors, perform element wise subtraction operation.
V1 = <1 4 9 5> and V2 = < 4 5 0 2>
V1 - V2 = <1-5; 4-5; 9-0; 5-2> = <-4; -1; 9; 3>
Vector Multiplication: -
To Multiply two vectors, perform element wise multiplication operation.
V1 = <1 4 9 5> and V2 = < 4 5 0 2>
V1 * V2 = <1*5; 4*5; 9*0; 5*2> = <5; 20; 0; 10>
Vector Division: -
To divide two vectors, perform element wise division operation.
V1 = <1 4 9 5> and V2 = < 1 2 3 5>
V1 / V2 = <1/1; 4/2; 9/3; 5/5> = <1; 2; 3; 1>
Algorithm/Program:
Defining a Vector
# Import NumPy: Numerical Python
Libraryimport numpyas np
Vector Addition
# vector additionfrom
numpyimport array #
define first vector a =
array([1, 10, 4, 76, -5])
print(a)
# define second vector b
= array([4, 0, -3, 7, -5])
print(b) # add vectors c
= a + b print(c)
Vector Subtraction
# vector subtractionfrom
numpyimport array #
define first vector a =
array([1, 10, 4, 76, -5])
print(a) # define second
vector b = array([4, 0, -3,
7, -5]) print(b) # subtract
vectors c = a - b
print(c)
Vector Multiplication
# Multiplication by a scalar
import numpyas np # define
a vector v = np.array([1, 10,
4, 76, -5]) # multiply with
scalar print(2*v)
Vector Division
# vector divisionfrom
numpyimport array
# define first vector a
= array([1, 2, 3])
print(a) # define
second vector b =
array([1, 2, 3])
print(b) # divide
vectors c = a / b
print(c)
Vector Transpose
import numpyas np # Make a
random vector v =
np.random.rand(5).reshape(-1, 1) #
print vector and its shapeprint(v,
v.shape) # transpose with T
print(v.T, v.T.shape)
Vector-Scalar Multiplication
# vector-scalar
multiplication from
numpyimport array # define
vector and scalar a =
array([1, 2, 3]) s = 0.5#
multiplication c = s * a
print(c)
Theory:
1. Vector Norm
Calculating the size or length of a vector is often required either directly or as part of a broader
vector or vector-matrix operation. The length of the vector is referred to as the vector norm or the
vector's magnitude. The length of a vector is a nonnegative number that describes the extent of
the vector in space, and is sometimes referred to as the vector's magnitude or the norm. The
length of the vector is always a positive number, except for a vector of all zero values. It is
calculated using some measure that summarizes the distance of the vector from the origin of the
vector space. For example, the origin of a vector space for a vector with 3 elements is (0; 0; 0).
Notations are used to represent the vector norm in broader calculations and the type of vector
norm calculation almost always has its own unique notation. We will take a look at a few
common vector norm calculations used in machine learning.
2. Vector L1 Norm
The length of a vector can be calculated using the L1 norm, where the 1 is a superscript of the L.
The notation for the L1 norm of a vector is jjvjj1, where 1 is a subscript. As such, this length is
sometimes called the taxicab norm or the Manhattan norm. L1(v) = ||v||1 The L1 norm is
calculated as the sum of the absolute vector values, where the absolute value of a scalar uses the
notation ja1j. In eect, the norm is a calculation of the Manhattan distance from the origin of the
vector space. ||v||1 = |a1| + |a2| + |a3|. In several machine learning applications, it is important to
discriminate between elements that are exactly zero and elements that are small but nonzero. In
these cases, we turn to a function that grows at the same rate in all locations, but retains
mathematical simplicity:
the L1 norm.
3. Vector L2 Norm
The length of a vector can be calculated using the L2 norm, where the 2 is a superscript of the L.
The notation for the L2 norm of a vector is jjvjj2 where 2 is a subscript. L2(v) = ||v||2 The L2
norm calculates the distance of the vector coordinate from the origin of the vector space. As such,
it is also known as the Euclidean norm as it is calculated as the Euclidean distance from the
origin. The result is a positive distance value. The L2 norm is calculated as the square root of the
sum of the squared vector values. ||v||2 =sqrt(a12+a22+a3**2) The L2 norm of a vector can be
calculated in NumPy using the norm() function with default parameters.
4. Vector Max Norm
The length of a vector can be calculated using the maximum norm, also called max norm. Max
norm of a vector is referred to as Linf where inf is a superscript and can be represented with the
infinity symbol. The notation for max norm is jjvjjinf , where inf is a subscript. Linf (v) = ||v||inf
The max norm is calculated as returning the maximum value of the vector, hence the name. ||v||
inf = max a1; a2; a3 The max norm of a vector can be calculated in NumPy using the norm()
function with the order parameter set to inf.
Program:
# vector L1 norm from numpy
import array from
numpy.linalg import norm
# define vector a =
array([1, 2, 3])
print(a) # calculate
norm l1 = norm(a, 2)
print(l1) # L1 norm
Aim:
Write a program to implement:
1. Matrix Arithmetic
2. Matrix Scalar Multiplication
3. Matrix-Vector Multiplication
4. Matrix-Matrix Multiplication
Theory:
Matrix: -
A matrix is a rectangular arrangement of numbers into rows and columns.
An 𝑚 × 𝑛
expressedby saying that the dimensions of the matrix are 𝑚 × 𝑛.
matrix is a matrix with m rows and n columns. Sometimes this is
Matrix Arithmetic: -
Adding and subtracting matrices: You can add (or subtract) matrices by adding (or
subtracting) corresponding entries. Matrix addition is associative while subtraction isnot.
Procedure:
Matrix Addition: -
To add two matrices, first make sure their shape matches and then perform elementwise
addition operation.
Matrix Subtraction: -
To subtract one matrix from another, first make sure their shape matches and thenperform
element wise subtraction operation.
In this case, the resultant matrix is a Diagonal Matrix and also an Identity Matrix (allIdentity
Matrices are Diagonal Matrix).
Algorithm/Program:
Defining a Matrix: -
# Import NumPy: Numerical Python
Library import numpyas np # Defining a
matrix mat = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print("Matrix: ")
print(mat, mat.shape)
Matrix Addition: -
# Import NumPy: Numerical Python Library
import numpyas np
# Defining matrices mat1
= np.array([[1, 2, 3],
[4, 8, 0],
[3, 4, 7]])
print(mat1, mat1.shape)
mat2 = np.array([[8, 7,
6],
[5, 1, 9],
[6, 5, 2]])
print(mat2, mat2.shape) #
Addition of two matrices
print("Resultant Matrix:
")
print(mat1 + mat2)
Matrix Subtraction: -
# Import NumPy: Numerical Python Library
import numpyas np
mat2 = np.array([[0, 2,
3],
[4, 7, 0],
[3, 4, 6]])
print("Matrix 2: ")
print(mat2,
mat2.shape)
# Subtraction of two
matrices print("Resultant
Matrix: ") print(mat1 -
mat2)
Matrix Division: -
# Import NumPy: Numerical Python Library
import numpyas np
mat2 = np.array([[1, 5,
4],
[4, 2, 3],
[3, 1, 4]])
print("Matrix 2: ")
print(mat2,
mat2.shape)
# Defining matrices
print("Matrix: ") mat =
np.array([[1, 2, 3],
[4, 8, 0],
[3, 4, 7]])
print(mat, mat.shape)
# Define a scalarscalar
= 2 print("Scalar: ")
print(scalar)
# Scalar Matrix
Multiplication sca_mult =
scalar * mat print("Resultant
Matrix")
print(sca_mult, sca_mult.shape)
mat2 = np.array([[0, 2,
3],
[4, 7, 0],
[3, 4, 6]])
print("Matrix 2: ")
print(mat2,
mat2.shape)
# Multiplication of two
matricesmat3 = np.matmul(mat1,
mat2) print("Resultant Matrix: ")
print(mat3, mat3.shape)