Data Science LabManual
Data Science LabManual
Lab Manual
Class : TEAI&DS
The instructor‘s manual is to be developed as a reference and hands-on resource. It should include
prologue (about University/program/ institute/ department/foreword/ preface), curriculum of the
course, conduction and Assessment guidelines, topics under consideration, concept, objectives,
outcomes, set of typical applications/assignments/ guidelines, and references
The laboratory assignments are to be submitted by student in the form of journal. Journal consists of
Certificate, table of contents, and handwritten write-up of each assignment (Title, Date of Completion,
Objectives, Problem Statement, Software and Hardware requirements, Assessment grade/marks and
assessor's sign, Theory- Concept in brief, algorithm, flowchart, test cases, Test Data Set(if applicable),
mathematical model (if applicable), conclusion/analysis. Program codes with sample output of all
performed assignments are to be submitted as softcopy. As a conscious effort and little contribution
towards Green IT and environment awareness, attaching printed papers as part of write-ups and program
listing to journal must be avoided. Use of DVD containing students programs maintained by Laboratory
In-charge is highly encouraged. For reference one or two journals may be maintained with program
prints in the Laboratory.
Problem statements must be decided jointly by the internal examiner and external examiner. During
practical assessment, maximum weightage should be given to satisfactory implementation of the
problem statement. Relevant questions may be asked at the time of evaluation to test the student’s
3
Third Year of Artificial Intelligence and Data Science Software Laboratory III
understanding of the fundamentals, effective and efficient implementation. This will encourage,
transparent evaluation and fair approach, and hence will not create any uncertainty or doubt in the minds
of the students. So, adhering to these principles will consummate our team efforts to the promising start
of student's academics.
The instructor is expected to frame the assignments by understanding the prerequisites, technological
aspects, utility and recent trends related to the topic. The assignment framing policy need to address the
average students and inclusive of an element to attract and promote the intelligent students. Use of open
source software is encouraged. Based on the concepts learned. Instructor may also set one assignment
or mini-project that is suitable to AI & DS branch beyond the scope of the syllabus.
4
Third Year of Artificial Intelligence and Data Science Software Laboratory III
ASSIGNMENT 1
TITLE: DATA WRANGLING I
PROBLEM STATEMENT: -
Perform the following operations using Python on any open-source dataset (e.g., [Link])
Import all the required Python Libraries.
1. Locate open-source data from the web (e.g. [Link]
2. Provide a clear description of the data and its source (i.e., URL of the web site).
OBJECTIVE:
1. To Learn and understand the concepts of Python Libraries.
2. To learn and understand the Data Science for the analysis of real time problems
3. To understand and practice Data Preprocessing & Data Normalization.
PREREQUISITE: -
1 Basic of Python Programming
2 Concept of Data Preprocessing, Data Formatting, Data Normalization and Data Cleaning
THEORY:
1. Introduction to Big Data
Big data means really a big data, it is a collection of large datasets that cannot be processed using
traditional computing techniques. Big data is not merely a data, rather it has become a complete
subject, which involves various tools, techniques, and frameworks. Big data involves the data
5
Third Year of Artificial Intelligence and Data Science Software Laboratory III
produced by different devices and applications. Given below are some of the fields that come under
the umbrella of Big Data.
2. Introduction to Dataset
A dataset is a collection of records, similar to a relational database table. Records are similar to
table rows, but the columns can contain not only strings or numbers, but also nested data structures
such as lists, maps, and other records.
a. NumPy
1. Basic array operations: add, multiply, slice, flatten, reshape, index arrays
2. Advanced array operations: stack arrays, split into sections, broadcast arrays
b. Pandas
c. Scikit Learn
Introduced to the world as a Google Summer of Code project, Scikit Learn is a robust machine
learning library for Python. It features ML algorithms like SVMs, random forests, k-means
clustering, spectral clustering, mean shift, cross-validation and more... Even NumPy, SciPy and
related scientific operations are supported by Scikit Learn with Scikit Learn being a part of the
SciPy Stack.
6. Pre-processing: Preparing input data as a text for processing with machine learning algorithms.
4. Description of Dataset
The Iris dataset was used in R.A. Fisher's classic 1936 paper, The Use of Multiple
Measurements in Taxonomic Problems and can also be found on the UCI Machine Learning Repository.
It includes three iris species with 50 samples each as well as some properties about each flower. One
flower species is linearly separable from the other two, but the other two are not linearly separable from
each other.
Total Sample- 150
7
Third Year of Artificial Intelligence and Data Science Software Laboratory III
csv_url = '[Link]
2. Now Read CSV File as a Dataframe in Python from from path where you saved the same The
Iris data set is stored in .csv format. ‘.csv’ stands for comma separated values. It is easier to load .csv
files in Pandas data frame and perform various analytical operations on it.
[Link] in the dataset from the UCI Machine Learning Repository link and specify column names to
use
8
Third Year of Artificial Intelligence and Data Science Software Laboratory III
Fig.2 Sample Dataset
2 [Link](n=5)
Return the last n rows.
13 [Link][:m, :n] a subset of the first m rows and the first n columns
Table 1. Panda Data frame functions for Data Preprocessing
9
Third Year of Artificial Intelligence and Data Science Software Laboratory III
Checking of Missing Values in Dataset:
● isnull() is the function that is used to check missing values or null values in pandas python.
● isna() function is also used to get the count of missing values of column and row wise count
of missing values
Syntax: [Link]()
Syntax: [Link]().any()
c. count of missing values across each column using isna() and isnull()
In order to get the count of missing values of the entire dataframe isnull() function is used. sum() which does the
column wise sum first and doing another sum() will get the count of missing values of the entire dataframe.
Syntax: [Link]().sum().sum()
Syntax: [Link]().sum(axis = 1)
[Link] Formatting: Ensuring all data formats are correct (e.g. object, text, floating number, integer, etc.) is
another part of this initial ‘cleaning’ process. If you are working with dates in Pandas, they also need to be stored
in the exact format to use special date-time functions.
10
Third Year of Artificial Intelligence and Data Science Software Laboratory III
(cm)'].astype("int
")
B Data normalization:- Mapping all the nominal data values onto a uniform scale (e.g. from 0 to 1) is
involved in data normalization. Making the ranges consistent across variables helps with statistical
analysis and ensures better comparisons later [Link] is also known as Min-Max scaling.
Algorithm:
iris = load_iris()
df = [Link]([Link], columns=iris.feature_names)
[Link]()
x = df[['score']].[Link](float)
min_max_scaler = [Link]()
x_scaled = min_max_scaler.fit_transform(x)
11
Third Year of Artificial Intelligence and Data Science Software Laboratory III
Step 7: Run the normalizer on the dataframe
df_normalized = [Link](x_scaled)
df_normalized
Label Encoding: Label Encoding refers to converting the labels into a numeric form so as to
convert them into the machine-readable form. It is an important preprocessing step for the
structured dataset in supervised learning.
Example : Suppose we have a column Height in some dataset. After applying label
encoding, the Height column is converted into:
Where 0 is the label for tall, 1 is the label for medium, and 2 is a label for short height.
Label Encoding on iris dataset: For iris dataset the target column which is Species. It
contains three species Iris-setosa, Iris-versicolor, Iris-virginica.
12
Third Year of Artificial Intelligence and Data Science Software Laboratory III
Sklearn Functions for Label Encoding:
● [Link] : It Encode labels with value between 0 and n_classes-1.
● fit_transform(y):
Parameters: yarray-like shape (n_samples,) Target values.
Returns: yarray-like of shape (n_samples,) Encoded labels.
This transformer should be used to encode target values, and not the input.
Algorithm:
Step 1 : Import pandas and sklearn library for preprocessing
from sklearn import preprocessing
Step 2: Load the iris dataset in dataframe object df
Step 3: Observe the unique values for the Species column.
df['Species'].unique()
output:array(['Iris-setosa','Iris-versicolor','Iris-virginica'], dtype=object)
Step 4: define label_encoder object knows how to understand word labels.
label_encoder = [Link]()
Step 5: Encode labels in column 'species'.
df['Species']= label_encoder.fit_transform(df['Species'])
Step 6: Observe the unique values for the Species column.
df['Species'].unique()
Output: array([0, 1, 2], dtype=int64)
CONCLUSION: In this way we have explored the functions of the python library for Data
Preprocessing, Data Wrangling Techniques and How to handle missing values on Iris Dataset.
ASSIGNMENT QUESTION
1. Explain Data Frame with Suitable example.
2. What is the limitation of the label encoding method?
3. What is the need of data normalization?
4. What are the different Techniques for Handling Missing Data?
13
Third Year of Artificial Intelligence and Data Science Software Laboratory III
ASSIGNMENT 2
PROBLEM STATEMENT: -
Create an “Academic performance” dataset of students and perform the following operations using Python.
1. Scan all variables for missing values and inconsistencies. If there are missing values and/or
inconsistencies, use any of the suitable techniques to deal with them.
2. Scan all numeric variables for outliers. If there are outliers, use any of the suitable techniques to deal
with them.
3. Apply data transformations on at least one of the variables. The purpose of this transformation should be
one of the following reasons: to change the scale for better understanding of the variable, to convert a
non-linear relation into a linear one, or to decrease the skewness and convert the distribution into a normal
distribution.
OBJECTIVE:
Students should be able to perform the data wrangling operation using Python on any open-source dataset.
PREREQUISITE: -
1. Basic of Python Programming
2. Concept of Data Preprocessing, Data Formatting, Data Normalization and Data Cleaning
THEORY:
1. Creation of Dataset using Microsoft Excel.
To fill the values in the dataset the RANDBETWEEN is used. Returns a random integer number between
the numbers you specify.
Bottom The smallest integer and Top The largest integer RANDBETWEEN will return.
14
Third Year of Artificial Intelligence and Data Science Software Laboratory III
2 Identification and Handling of Null Values
Missing Data can occur when no information is provided for one or more items or for a whole
unit. Missing Data is a very big problem in real-life scenarios. Missing Data can also refer to as
NA(Not Available) values in pandas. In DataFrame sometimes many datasets simply arrive with
missing data, either because it exists and was not collected, or it never existed. For Example,
Suppose different users being surveyed may choose not to share their income, some users may
choose not to share the address in this way many datasets went missing.
In Pandas missing data is represented by two values:
1. None: None is a Python singleton object that is often used for missing data in Python code.
2. NaN: NaN (an acronym for Not a Number), is a special floating-point value recognized by all
systems that use the standard IEEE floating-point representation.
Pandas treat None and NaN as essentially interchangeable for indicating missing or null values.
To facilitate this convention, there are several useful functions for detecting, removing, and
replacing null values in Pandas DataFrame :
● isnull()
● notnull()
● dropna()
● fillna()
● replace()
1. Checking for missing values using isnull() and notnull()
Algorithm:
Step 1 : Import pandas and numpy in order to check missing values in Pandas DataFrame
import pandas as pd
import numpy as np
Step 2: Load the dataset in dataframe object df
df=pd.read_csv("/content/[Link]")
Step 3: Display the data frame
df
Step 4: Use isnull() function to check null values in the dataset.
[Link]()
Step 5:To create a series true for NaN values for specific columns. for example math score in
dataset and display data with only math score as NaN
series = [Link](df["math score"])df[series]
● Checking for missing values using notnull()
15
Third Year of Artificial Intelligence and Data Science Software Laboratory III
In order to check null values in Pandas Dataframe, notnull() function is used. This
function returns dataframe of Boolean values which are False for NaN values.
1. Algorithm:
Step 1 : Import pandas and numpy in order to check missing values in
PandasDataFrame
import pandas as pd
import numpy as np
Step 2: Load the dataset in dataframe object df
df=pd.read_csv("/content/[Link]")
Step 3: Display the data frame
df
Step 4: Use notnull() function to check null values in the dataset.
[Link]()
Step 5: To create a series true for NaN values for specific columns. for
example math score in dataset and display data with only math score
as NaN
series1 = [Link](df["math score"])df[series1]
See that there are also categorical values in the dataset, for this, you need to
useLabel Encoding
from [Link] import LabelEncoderle =
LabelEncoder()
df['gender'] = le.fit_transform(df['gender'])newdf=df
In order to fill null values in a datasets, fillna(), replace() functions are used. These functions
replace NaN values with some value of their own. All these functions help in filling null
values in datasets of a DataFrame.
For replacing null values with NaN
missing_values = ["Na", "na"]
df = pd.read_csv("[Link]", na_values =missing_values)
df
replacing missing values in forenoon column with minimum/maximum number of that column
Following line will replace Nan value in dataframe with value -99
[Link](to_replace = [Link], value = -99)
● Deleting null values using dropna() method
In order to drop null values from a dataframe, dropna() function is used.
This function drops Rows/Columns of datasets with Null values in different
ways.
1. Dropping rows with at least 1 null value
[Link]()
Similarly, an Outlier is an observation in a given dataset that lies far from the rest of the observations.
That means an outlier is vastly larger or smaller than the remaining values in the set.
Mean is the accurate measure to describe the data when we do not have any outliers present. Median is
used if there is an outlier in the dataset. Mode is used if there is an outlier AND about ½ or more of the
data is the same.
18
Third Year of Artificial Intelligence and Data Science Software Laboratory III
‘Mean’ is the only measure of central tendency that is affected by the outliers which in turn impacts
Standard deviation.
Example:
Consider a small dataset, sample= [15, 101, 18, 7, 13, 16, 11, 21, 5, 15, 10, 9]. Bylooking at it, one
can quickly say ‘101’ is an outlier that is much larger than the other values.
2. Algorithm:
19
Third Year of Artificial Intelligence and Data Science Software Laboratory III
df
Step 4:Select the columns for boxplot and draw the boxplot.
Step 5: We can now print the outliers for each column with reference to the box plot.
print([Link](df['math score']>90))
print([Link](df['reading score']<25))
print([Link](df['writing score']<30))
Handling of Outliers:
For removing the outlier, one must follow the same process of removing an
entry from the dataset using its exact position in the dataset because in all the above
methods ofdetecting the outliers end result is the list of all those data items that satisfy
the outlier definition according to the method used.
Below are some of the methods of treating the outliers
● Trimming/removing the outlier
20
Third Year of Artificial Intelligence and Data Science Software Laboratory III
● Quantile based flooring and capping
● Mean/Median imputation
22
Third Year of Artificial Intelligence and Data Science Software Laboratory III
Reducing skewness A data transformation may be used to reduce skewness. A distribution that
is symmetric or nearly so is often easier to handle and interpret than a skewed distribution. The
logarithm, x to log base 10 of x, or x to log base e of x (ln x), or x to log base 2 of x, is a strong
transformation with a major effect on distribution shape. It is commonly used for reducing right
skewness and is often appropriate for measured variables. It can not be applied to zero or negative
values.
1. Algorithm:
Step 1 : Detecting outliers using Z-Score for the Math_score variable
andremove the outliers.
23
Third Year of Artificial Intelligence and Data Science Software Laboratory III
Step 2: Observe the histogram for math_score variable.
import [Link] as plt new_df['math
score'].plot(kind = 'hist')
Step 3: Convert the variables to logarithm at the scale 10.
df['log_math'] = np.log10(df['math score'])
Assignment Question:
1. Explain the methods to detect the outlier.
2. Explain data transformation methods.
3. Write the algorithm to display the statistics of Null values present in the dataset.
4. Write an algorithm to replace the outlier value with the mean of the variable.
.
24
Third Year of Artificial Intelligence and Data Science Software Laboratory III
ASSIGNMENT 3
PROBLEM STATEMENT: -
OBJECTIVE: To analyze and demonstrate knowledge of statistical data analysis techniques for
decision- making
PREREQUISITE: -
1. Basic of Python Programming
2. Concept of statistics mean, median, minimum, maximum, standard deviation
THEORY:
The data are summarized in some, but not all ways. We chose descriptives that are either most often
reported, or most often covered in introductory courses. These are as follows:
Central Tendency
oMean
oMedian
oMode
Dispersion
oStandard deviation (Std. deviation)
oMinimum
oMaximum
Central Tendency
The Mean, Median and Mode are the three measures of central tendency. Mean is the arithmetic average of
a data set. This is found by adding the numbers in a data set and dividing by the number of observations in
the data set. The median is the middle number in a data set when the numbers are listed in either ascending
25
Third Year of Artificial Intelligence and Data Science Software Laboratory III
or descending order. The mode is the value that occurs the most often in a data set and the range is the
difference between the highest and lowest values in a data set.
The Mean
Here,
∑ represents the summation
X represents observations
N represents the number of observations .
In the case where the data is presented in a tabular form, the following formula is used to compute the
mean
Mean = ∑f x / ∑f
Where ∑f = N
The Median
If the total number of observations (n) is an odd number, then the formula is given below:
If the total number of the observations (n) is an even number, then the formula is given below:
Consider the case where the data is continuous and presented in the form of a frequency distribution, the
median formula is as follows.
Find the median class, the total count of observations ∑f.
The median class consists of the class in which (n / 2) is present.
Here
l = lesser limit belonging to the median class
c = cumulative frequency value of the class before the median class
f = frequency possessed by the median class
h = size of the class
The Mode
26
Third Year of Artificial Intelligence and Data Science Software Laboratory III
The mode is the most frequently occurring observation or value.
Consider the case where the data is continuous, and the value of mode can be computed using the
following steps.
a] Determine the modal class that is the class possessing the maximum frequency.
b] Calculate the mode using the below formula.
There are six main steps for finding the standard deviation by hand. We’ll use a small data set of 6 scores
to walk through the steps.
Data set
46 69 32 60 52 41
27
Third Year of Artificial Intelligence and Data Science Software Laboratory III
Mean (x̅)
x̅ = (46 + 69 + 32 + 60 + 52 + 41) ÷ 6 = 50
46 46 – 50 = -4
69 69 – 50 = 19
32 32 – 50 = -18
60 60 – 50 = 10
52 52 – 50 = 2
41 41 – 50 = -9
(-4)2 = 4 × 4 = 16
192 = 19 × 19 = 361
102 = 10 × 10 = 100
28
Third Year of Artificial Intelligence and Data Science Software Laboratory III
Squared deviations from the mean
22 = 2 × 2 = 4
(-9)2 = -9 × -9 = 81
Sum of squares
Variance
Standard deviation
√177.2 = 13.31
From learning that SD = 13.31, we can say that each score deviates from the mean by 13.31 points on
average.
Consider HR dataset it contains fields like Age, Monthly Income, Attrition, Business Travel and so on so
following are steps to find statistics (mean, median, minimum, maximum, standard deviation)
29
Third Year of Artificial Intelligence and Data Science Software Laboratory III
#Storing age and monthly income in array and then finding maximum and minimum values
array1 = [Link](df['MonthlyIncome'])
array2=[Link](df["Age"])
print("Income",array1)
print("Age array",array2)
print("Maximum income among the employees is :",max(array1))
print("Minimum income among the employees is :",min(array1))
print("Maximum age among the employees is :",max(array2))
print("Minimum age among the employees is :",min(array2))
##Use of describe()
To display basic statistical details like percentile, mean, standard deviation etc. for Iris-Vigginica use
describe.
30
Third Year of Artificial Intelligence and Data Science Software Laboratory III
Conclusion: In this way we have explored the functions of the python library for calculating Data statistics
(mean, median, minimum, maximum, standard deviation).
Assignment Question:
31
Third Year of Artificial Intelligence and Data Science Software Laboratory III
Assignment No: 4
Objective of the Assignment: Students should be able to data analysis using liner regression
using Python for any open-source dataset.
Prerequisite:
1. Basic of Python Programming
[Link] of Regression.
32
Third Year of Artificial Intelligence and Data Science Software Laboratory III
which is equal to the average squared difference between an observation’s actual and
predicted values.
● It is shown as an equation of line like : Y =
m*X + b + e
Where : b is intercepted, m is slope of the line and e is error term.
This equation can be used to predict the value of target variable Y based on given predictor
variable(s) X, as shown in Fig. 1.
● Fig. 2 shown below is about the relation between weight (in Kg) and height (in cm), a
linear relation. It is an approach of studying in a statistical manner to summarise and
learn the relationships among continuous (quantitative) variables.
● Here a variable, denoted by ‘x’ is considered as the predictor, explanatory, or
independent variable.
Fig.2 : Relation between weight (in Kg) and height (in cm)
33
Third Year of Artificial Intelligence and Data Science Software Laboratory III
MultiVariate Regression :It concerns the study of two or more predictor variables. Usually a
transformation of the original features into polynomial features from a given degree is preferred
and further Linear Regression is applied on it.
● A simple linear model is the one which involves only one dependent and one independent
variable. Regression Models are usually denoted in Matrix Notations.
● However, for a simple univariate linear model, it can be denoted by the regression
equation
(1)
9.𝑦 = β + β 𝑥
0 1
34
Third Year of Artificial Intelligence and Data Science Software Laboratory III
where 𝑦 is the dependent or the response variable 𝑥 is the independent or the input variable
● This linear equation represents a line also known as the ‘regression line’. The least square
estimation technique is one of the basic techniques used to guess the values of the
parameters and based on a sample set.
● This technique estimates parameters β and β and by trying to minimize the square
0
1
of errors at all the points in the sample set. The error is the deviation of the actual sample
● data point from the regression line. The technique can be represented by the equation.
𝑛
2
𝑚𝑖𝑛 ∑ (𝑦 − 𝑦) (2)
𝑖=0
35
Third Year of Artificial Intelligence and Data Science Software Laboratory III
and β such
β ∑ (𝑥 − 𝑥 ) (𝑦 − 𝑦 )/ ∑ (𝑥
= 𝑥) (3)
1 𝑖 𝑖−
𝑖=1 𝑖 𝑖=1
β = 𝑦 −β 𝑥 (4)
0 1
36
Third Year of Artificial Intelligence and Data Science Software Laboratory III
Once the Linear Model is estimated using equations (3) and (4), we can estimate thevalue
of the dependent variable in the given range only. Going outside the range is called
extrapolation which is inaccurate if simple regression techniques are used.
Mathematically, the MSE can be calculated as the average sum of the squared difference
between the actual value and the predicted or estimated value represented by the regression
model (line or plane).
An MSE of zero (0) represents the fact that the predictor is a perfect predictor.
RMSE:
Root Mean Squared Error method that basically calculates the least-squares error and takes a
root of the summed values.
Mathematically speaking, Root Mean Squared Error is the square root of the sum of all errors
divided by the total number of values. This is the formula to calculate RMSE
37
Third Year of Artificial Intelligence and Data Science Software Laboratory III
RMSE - Least Squares Regression Method – Edureka R-Squared :
R-Squared is the ratio of the sum of squares regression (SSR) and the sum of squares total
(SST).
SST : total sum of squares (SST), regression sum of squares (SSR), Sum of square of errors
(SSE) are all showing the variation with different measures.
38
Third Year of Artificial Intelligence and Data Science Software Laboratory III
A value of R-squared closer to 1 would mean that the regression model covers
most part of the variance of the values of the response variable and can be
termed as a good model.
One can alternatively use MSE or R-Squared based on what is appropriate and the need of the
hour. However, the disadvantage of using MSE rather than R-squared is that it will be difficult
to gauge the performance of the model using MSE as the value of MSE can vary from 0 to any
larger number. However, in the case of R-squared, the value is bounded between 0 and .
1 95 85
2 85 95
3 80 70
4 70 65
5 60 70
(i) linear regression equation that best predicts standard XIIth score
39
Third Year of Artificial Intelligence and Data Science Software Laboratory III
(ii) Interpretation of the regression line.
Interpretation 1
For an increase in value of x by 0.644 units there is an increase in value of y in one unit.
Interpretation 2
Even if x = 0 value of independent variable, it is expected that value of y is 26.768 Score in XII
standard (Yi) is 0.644 units depending on Score in X standard (Xi) but other factors will also
contribute to the result of XII standard by 26.768 .
(iii) If a student's score is 65 in std X, then his expected score in XII standard is
78.288
Output:
68.63
Step 6: Predict the y_pred for all values of x.
y_pred= predict(x)
y_pred
Output:
array([81.50684932, 87.94520548, 71.84931507, 68.63013699, 71.84931507])
Step 7: Evaluate the performance of Model (R-Suare)
R squared calculation is not implemented in numpy… so that one should be borrowed
from sklearn.
Output:
0.4803218090889323
Step 8: Plotting the linear regression model
y_line = model[1] + model[0]* x
[Link](x, y_line, c = 'r') [Link](x,
y_pred) [Link](x,y,c='r')
42
Third Year of Artificial Intelligence and Data Science Software Laboratory III
Output:
43
Third Year of Artificial Intelligence and Data Science Software Laboratory III
import sklearn
from sklearn.linear_model import LinearRegressionlm =
LinearRegression()
model=[Link](xtrain, ytrain)
Step 10: Predict the y_pred for all values of train_x and test_x
ytrain_pred = [Link](xtrain)ytest_pred
= [Link](xtest)
Step 12: Calculate Mean Square Paper for train_y and test_y
from [Link] import mean_squared_error, r2_scoremse =
mean_squared_error(ytest, ytest_pred)
print(mse)
mse = mean_squared_error(ytrain_pred,ytrain) print(mse)
Output:
33.44897999767638
mse = mean_squared_error(ytest, ytest_pred) print(mse)
Output:
19.32647020358573
Step 13: Plotting the linear regression model
[Link](ytrain ,ytrain_pred,c='blue',marker='o',label='Training data')
[Link](ytest,ytest_pred ,c='lightgreen',marker='s',label='Test data')[Link]('True values')
[Link]('Predicted')
[Link]("True value vs Predicted value")[Link](loc=
'upper left') #[Link](y=0,xmin=0,xmax=50)
[Link]()
[Link]()
44
Third Year of Artificial Intelligence and Data Science Software Laboratory III
Conclusion:
In this way we have done data analysis using linear regression for Boston Dataset andpredict the
price of houses using the features of the Boston Dataset.
Assignment Question:
1) Compute SST, SSE, SSR, MSE, RMSE, R Square for the below example .
Student Score in X standard (Xi) Score in XII standard (Yi)
1 95 85
2 85 95
3 80 70
4 70 65
5 60 70
2) Comment on whether the model is best fit or not based on the calculated values.
3) Write python code to calculate the RSquare for Boston Dataset. (Consider the linear regression
model created in practical session)
45
Assignment No: 5
Title:
1. Implement logistic regression using Python/R to perform classification on
Social_Network_Ads.csv dataset.
2. Compute Confusion matrix to find TP, FP, TN, FN, Accuracy, Error rate, Precision, Recall
on the given dataset.
Objective: Students should be able to data analysis using logisticregression using Python for any
open-source dataset
Prerequisite:
Logistic Regression is one of the most simple and commonly used Machine Learning
algorithms for two-class classification. It is easy to implement and can be used as the
baseline for any binary classification problem. Its basic fundamental concepts are also
constructive in deep learning. Logistic regression describes and estimates the relationship
between one dependent binary variable and independent variables.
Logistic regression is a statistical method for predicting binary classes. The outcome or
target variable is dichotomous in nature. Dichotomous means there are only two possible
classes. For example, it can be used for cancer detection problems. It computes the
It is a special case of linear regression where the target variable is categorical in nature. It
uses a log of odds as the dependent variable. Logistic Regression predicts the probability
Where, y is a dependent variable and x1, x2 ... and Xn are explanatory variables.
Sigmoid Function:
3. Sigmoid Function
The sigmoid function, also called logistic function, gives an ‘S’ shaped curve that can take any
real-valued number and map it into a value between 0 and 1. If the curve goes to positive infinity,
y predicted will become 1, and if the curve goes to negative infinity, y predicted will become 0.
If the output of the sigmoid function is more than 0.5, we can classify the outcome as 1 or YES,
and if it is less than 0.5, we can classify it as 0 or NO. The outputcannotFor example: If the output
is 0.75, we can say in terms of probability as: There is a 75 percent chance that a patient will suffer
from cancer.
4. Types of LogisticRegression
Binary Logistic Regression: The target variable has only two possible outcomes such as
Spam or Not Spam, Cancer or No Cancer.
Multinomial Logistic Regression: The target variable has three or more nominal
categories such as predicting the type of Wine.
Ordinal Logistic Regression: the target variable has three or more ordinal categories
such as restaurant or product rating from 1 to 5.
The following table shows the confusion matrix for a two class classifier.
Here each row indicates the actual classes recorded in the test data set and the each column
indicates the classes as predicted by the classifier.
Numbers on the descending diagonal indicate correct predictions, while the ascending diagonal
concerns prediction errors.
● Number of positive (Pos) : Total number instances which are labelled as positive in a
given dataset.
● Number of negative (Neg) : Total number instances which are labelled as negative in a
given dataset.
● Number of True Positive (TP) : Number of instances which are actually labelled as
positive and the predicted class by classifier is also positive.
● Number of True Negative (TN) : Number of instances which are actually labelled as
negative and the predicted class by classifier is also negative.
● Number of False Positive (FP) : Number of instances which are actually labelled as
negative and the predicted class by classifier is positive.
● Number of False Negative (FN): Number of instances which are actually labelled as
positive and the class predicted by the classifier is negative.
● Accuracy: Accuracy is calculated as the number of correctly classified instances divided
by totalnumber of instances.
The ideal value of accuracy is 1, and the worst is 0. It is also calculated as the sum of true
positive and true negative (TP + TN) divided by the total number of instances.
𝑇𝑃+𝑇𝑁 𝑇𝑃+𝑇𝑁
𝑎𝑐𝑐 = 𝑇𝑃+𝐹𝑃+𝑇𝑁+𝐹𝑁
= 𝑃𝑜𝑠+𝑁𝑒𝑔
● Error Rate: Error Rate is calculated as the number of incorrectly classified instances
divided by total number of instances.
The ideal value of accuracy is 0, and the worst is 1. It is also calculated as the sum of
false positive and false negative (FP + FN) divided by the total number of instances.
𝐹𝑃+𝐹𝑁 𝐹𝑃+𝐹𝑁
𝑒𝑟𝑟 = 𝑇𝑃+𝐹𝑃+𝑇𝑁+𝐹𝑁 = 𝑃𝑜𝑠+𝑁𝑒𝑔
Or
● Recall: .It is calculated as the number of correctly classified positive instances divided by
the total number of positive instances. It is also called recall or sensitivity. The ideal value
of sensitivity is 1, whereas the worst is 0.
It is calculated as the number of correctly classified positive instances divided by the total
number of positive instances.
𝑟𝑒𝑐𝑎𝑙𝑙 = 𝑇𝑃
𝑇𝑃+𝐹𝑁
Algorithm (Boston Dataset):
Step 1: Import libraries and create alias for Pandas, Numpy and Matplotlib
Step 2: Import the Social_Media_Adv Dataset
Step 3: Initialize the data frame
Step 4: Perform Data
Preprocessing
● Convert Categorical to Numerical Values if applicable
● Check for Null Value
● Covariance Matrix to select the most promising features
● Divide the dataset into Independent(X) and
Dependent(Y)variables.
● Split the dataset into training and testing datasets
● Scale the Features if necessary.
Step 6: Predict the y_pred for all values of train_x and test_x
Conclusion:
In this way we have done data analysis using logistic regression for Social Media Adv. and
evaluate the performance of model.
: DSBDAL
.
Assignment No: 6
Title :
1. Implement Simple Naïve Bayes classification algorithm using Python/R on [Link]
dataset.
2. Compute Confusion matrix to find TP, FP, TN, FN, Accuracy, Error rate, Precision,
Recall on the given dataset.
Objective:
Students should be able to data analysis using Naïve Bayes classification algorithm
using Python for any open-source dataset.
Prerequisite:
1. Basic of Python Programming
[Link] of Join and Marginal Probability.
Conditional Probabilities:
We have a dataset with some features Outlook, Temp, Humidity, and Windy, and the
target here is to predict whether a person or team will play tennis or not.
Conditional Probability
: DSBDAL
Here, we are predicting the probability of class1 and class2 based on the given condition. If I try
to write the same formula in terms of classes and features, we will get the following equation
Now we have two classes and four features, so if we write this formula for class C1, it will be
Here, we replaced Ck with C1 and X with the intersection of X1, X2, X3, X4. You might have a
question, It’s because we are taking the situation when all these features are present at the same
time.
The Naive Bayes algorithm assumes that all the features are independent of each other or in other
words all the features are unrelated. With that assumption, we can further simplify the above
This is the final equation of the Naive Bayes and we have to calculate the probability of both C1
P (N0 | Today) > P (Yes | Today) So, the prediction that golf would be played is ‘No’.
● Divide the dataset into Independent (X) and Dependent (Y) variables.
● Split the dataset into training and testing datasets.
● Scale the Features if necessary.
3) Step 6: Predict the y_pred for all values of train_x and test_x
Y_pred = [Link](X_test)
Conclusion:
In this way we have done data analysis using Naive Bayes Algorithm for Iris dataset and
evaluated the performance of the model.
: DSBDAL
Assignment No: 7
Objective:
Prerequisite:
1. Basic of Python Programming
2. Basic of English language.
Text mining is also referred to as text analytics. Text mining is a process of exploring sizable textual
data and finding patterns. Text Mining processes the text itself, while NLP processes with the
underlying metadata. Finding frequency counts of words, length of the sentence, presence/absence of
specific words is known as text mining. Natural language processing is one of the components of text
mining. NLP helps identify sentiment, finding entities in the sentence, and category of blog/article.
Text mining is preprocessed data for text analytics. In Text Analytics, statistical and machine learning
algorithms are used to classify information.
: DSBDAL
NLTK(natural language toolkit) is a leading platform for building Python programs to work with
human language data. It provides easy-to-use interfaces and lexical resources such as WordNet, along
with a suite of text processing libraries for classification, tokenization, stemming, tagging, parsing,
and semantic reasoning and many more.
Analysing movie reviews is one of the classic examples to demonstrate a simple NLP Bag-of-words
model, on movie reviews.
Tokenization:
Tokenization is the first step in text analytics. The process of breaking down a text paragraph into
smaller chunks such as words or sentences is called Tokenization. Token is a single entity that is the
building blocks for a sentence or paragraph.
Lemmatization Vs Stemming
Stemming algorithm works by cutting the suffix from the word. In a broader sensecuts either the
beginning or end of the word.
On the contrary, Lemmatization is a more powerful operation, and it takes into consideration
morphological analysis of the words. It returns the lemma which is the base form of all its inflectional
forms. In-depth linguistic knowledge is
required to create dictionaries and look for the proper form of the word. Stemming is a general
operation while lemmatization is an intelligent operation where the proper form will be looked in the
dictionary. Hence, lemmatization helps in forming better machine learning features.
Part-II
Example:
The initial step is to make a vocabulary of unique words and calculate TF for each document. TF will
be more for words that frequently appear in a document and less for rare words in a document.
After applying TFIDF, text in A and B documents can be represented as a TFIDF vector of
dimension equal to the vocabulary words. The value corresponding to each word representsthe
importance of that word in a particular document.
TFIDF is the product of TF with IDF. Since TF values lie between 0 and 1, not using ln can result in
high IDF for some words, thereby dominating the TFIDF. We don’t want that, and therefore, we use
: DSBDAL
ln so that the IDF should not completely dominate the TFIDF.
● Disadvantage of TFIDF
It is unable to capture the semantics. For example, funny and humorous are synonyms, but TFIDF
does not capture that. Moreover, TFIDF can be computationally expensive if the vocabulary is vast.
Algorithm for Tokenization, POS Tagging, stop words removal, Stemming and
Lemmatization:
Step 1: Download the required packages
[Link]('punkt') [Link]('stopwords')
[Link]('wordnet') [Link]('averaged_perceptron_tagger')
Step 2: Initialize the text
text= "Tokenization is the first step in text analytics. The process of breaking down a text paragraph into
smaller chunkssuch as words or sentences is called Tokenization."
#Word Tokenization
from [Link] import word_tokenizetokenized_word=word_tokenize(text)
print(tokenized_word)
text= "How to remove stop words with NLTK library in Python?"text= [Link]('[^a-zA-Z]', ' ',text)
tokens = word_tokenize([Link]())filtered_text=[]
: DSBDAL
for w in tokens:
if w not in stop_words: filtered_text.append(w)
print("Tokenized Sentence:",tokens) print("Filterd Sentence:",filtered_text)
Step 5 : Perform Stemming
from [Link] import PorterStemmer
e_words= ["wait", "waiting", "waited", "waits"]ps =PorterStemmer()
for w in e_words:
rootWord=[Link](w)print(rootWord)
Step 5: Create a dictionary of words and their occurrence for each document in the
corpus
numOfWordsA = [Link](uniqueWords, 0)
for word in bagOfWordsA: numOfWordsA[word] += 1
: DSBDAL
numOfWordsB = [Link](uniqueWords, 0)for word in bagOfWordsB:
numOfWordsB[word] += 1
Step 6: Compute the term frequency for each of our documents.
def computeTF(wordDict, bagOfWords):tfDict = {}
bagOfWordsCount = len(bagOfWords) for word, count in
[Link]():
tfDict[word] = count / float(bagOfWordsCount)return tfDict
tfA = computeTF(numOfWordsA, bagOfWordsA)tfB =
computeTF(numOfWordsB, bagOfWordsB)
Conclusion:
In this way we have done text data analysis using TF IDF algorithm.
Assignment Question:
1) Perform Stemming for text = "studies studying cries cry". Compare the results generated
with Lemmatization. Comment on your answer how Stemming and Lemmatization
differ from each other.
2) Write Python code for removing stop words from the below documents, conver the
: DSBDAL
documents into lowercase and calculate the TF, IDF and TFIDF score for each document.
documentA = 'Jupiter is the largest Planet' documentB = 'Mars is the fourth planet from the
Sun'
.
: DSBDAL
Assignment No: 8
2. Write a code to check how the price of the ticket (column name: 'fare') for each passenger is
distributed by plotting a histogram.
Objective:
Students should be able to perform the data Visualizationoperation using Python on any open-
source dataset.
Prerequisite:
1. Basic of Python Programming
2. Seaborn Library, Concept of Data Visualization.
Let's see what the Titanic dataset looks like. Execute the following script:
The dataset contains 891 rows and 15 columns and contains information about the passengers who
boarded the unfortunate Titanic ship. The original task is to predict whether or not the passenger
survived depending upon different features such as their age, ticket, cabin they boarded, the class of
the ticket, etc. We will use the Seaborn library to see if we can find any patterns in the data.
a. Dist-Plot
b. Joint Plot
c. Rug Plot
B. Categorical Plots
a. Bar Plot
b. Count Plot
c. Box Plot
d. Violin Plot
C. Advanced Plots
a. Strip Plot
b. Swarm Plot
D. Matrix Plots
a. Heat Map
b. Cluster Map
Distribution Plots:
These plots help us to visualise the distribution of data. We can use these plots to understand the
Distplot
● We can change the number of bins i.e. number of vertical bars in a histogram
The line that you see represents the kernel density estimation. You can remove this line by passing
False as the parameter for the kde attribute as shown below
Here the x-axis is the age and the y-axis displays frequency. For example, for bins = 10,there are
Joint Plot
● We additionally obtain a scatter plot between the variables to reflect their linear
relationship. We can customise the scatter plot into a hexagonal plot, where, the
: DSBDAL
more the colour intensity, the more will be the number of observations.
# For Plot 2
● From the output, you can see that a joint plot has three parts. A distribution plot at the top
for the column on the x-axis, a distribution plot on the right for the column on the y-axis
and a scatter plot in between that shows the mutual distribution of data for both the
columns. You can see that there is no correlation observed between prices and the fares.
● You can change the type of the joint plot by passing a value for the kind parameter. For
instance, if instead of a scatter plot, you want to display the distribution of data in the form
of a hexagonal plot, you can pass the value hex for the kind parameter.
● In the hexagonal plot, the hexagon with the most number of points gets darker colour. So
if you look at the above plot, you can see that most of the passengers are between the
ages of 20 and 30 and most of them paid between 10-50 for the tickets.
The rugplot() is used to draw small bars along the x-axis for each point in the dataset. To plot a
rug plot, you need to pass the name of the column. Let's plot a rug plot for fare.
[Link](dataset['fare'])
: DSBDAL
From the output, you can see that most of the instances for the fares have values between 0 and 100.
These are some of the most commonly used distribution plots offered by the Python's Seaborn
Library. Let's see some of the categorical plots in the Seaborn library.
Categorical Plots
Categorical plots, as the name suggests, are normally used to plot categorical data. The categorical
plots plot the values in the categorical column against another categorical column ora numeric
column. Let's see some of the most commonly used categorical data.
From the output, you can clearly see that the average age of male passengers is just less than 40 while
the average age of female passengers is around 33.
In addition to finding the average, the bar plot can also be used to calculate other aggregate values
for each category. To do so, you need to pass the aggregate function to the estimator. For instance,
you can calculate the standard deviation for the age of each gender as follows:
import numpy as np
sns
Notice, in the above script we use the std aggregate function from the numpy library to calculate the
standard deviation for the ages of male and female passengers. The output looks like this:
: DSBDAL
b. The Count Plot
The count plot is similar to the bar plot, however it displays the count of the categories in a specific
column. For instance, if we want to count the number of males and women passenger we can do so
using count plot as follows:
[Link](x='sex', data=dataset)
The box plot is used to display the distribution of the categorical data in the form of quartiles. The
centre of the box shows the median value. The value from the lower whisker to the bottomof the
box shows the first quartile. From the bottom of the box to the middle of the box lies the second
quartile. From the middle of the box to the top of the box lies the third quartile and finally from the
top of the box to the top whisker lies the last quartile.
Now let's plot a box plot that displays the distribution for the age with respect to each gender. You
need to pass the categorical column as the first parameter (which is sex in our case) and the numeric
column (age in our case) as the second parameter. Finally, the dataset is passed as the third parameter,
take a look at the following script:
Let's try to understand the box plot for females. The first quartile starts at around 1 and ends at 20
which means that 25% of the passengers are aged between 1 and 20. The second quartile starts at
around 20 and ends at around 28 which means that 25% of the passengers are aged between20 and
28. Similarly, the third quartile starts and ends between 28 and 38, hence 25% passengers are aged
within this range and finally the fourth or last quartile starts at 38 and ends around 64.
If there are any outliers or the passengers that do not belong to any of the quartiles, they are called
outliers and are represented by dots on the box plot.
You can make your box plots more fancy by adding another layer of distribution. For instance, if you
want to see the box plots of forage of passengers of both genders, along with the informationabout
whether or not they survived, you can pass the survived as value to the hue parameter as shown below:
[Link](x='sex', y='age', data=dataset, hue="survived")
Now in addition to the information about the age of each gender, you can also see the distribution of
the passengers who survived. For instance, you can see that among the male passengers, on average
more younger people survived as compared to the older ones. Similarly, you can see that the variation
: DSBDAL
among the age of female passengers who did not survive is much greater than the age of the surviving
female passengers.
Let's plot a violin plot that displays the distribution for the age with respect to each gender.
You can see from the figure above that violin plots provide much more information about the data as
compared to the box plot. Instead of plotting the quartile, the violin plot allows us to see all the
components that actually correspond to the data. The area where the violin plot is thicker has a higher
number of instances for the age. For instance, from the violin plot for males, it is clearly evident that the
number of passengers with age between 20 and 40 is higher than all the rest of the age brackets.
Like box plots, you can also add another categorical variable to the violin plot using the hue parameter
as shown below:
Now you can see a lot of information on the violin plot. For instance, if you look at the bottom of the
violin plot for the males who survived (left-orange), you can see that it is thicker than the bottom of
the violin plot for the males who didn't survive (left-blue). This means that the number of young male
passengers who survived is greater than the number of young male passengers who did not survive.
Advanced Plots:
The stripplot() function is used to plot the violin plot. Like the box plot, the first parameter is the
categorical column, the second parameter is the numeric column while the third parameter is the
dataset. Look at the following script:
You can see the scattered plots of age for both males and females. The data points look like strips. It
is difficult to comprehend the distribution of data in this form. To better comprehend the data, pass
True for the jitter parameter which adds some random noise to the data. Look at the following script:
Now you have a better view for the distribution of age across the genders.
Like violin and box plots, you can add an additional categorical column to strip plot using hue
parameter as shown below:
Let's add another categorical column to the swarm plot using the hue parameter.
From the output, it is evident that the ratio of surviving males is less than the ratio of surviving
females. Since for the male plot, there are more blue points and less orange points. On the other hand,
for females, there are more orange points (surviving) than the blue points (not surviving). Another
observation is that amongst males of age less than 10, more passengers survived as compared to those
who didn't.
Matrix Plots
Matrix plots are the type of plots that show data in the form of rows and columns. Heat maps are the
prime examples of matrix plots.
Heat Maps
Heat maps are normally used to plot correlation between numeric columns in the form of a matrix. It
is important to mention here that to draw matrix plots, you need to have meaningful information on
rows as well as columns. Let's plot the first five rows of the Titanic dataset to see if both the rows and
column headers have meaningful information. Execute the following script:
import pandas as pd
: DSBDAL
import numpy as np
sns
From the output, you can see that the column headers contain useful information such aspassengers
surviving, their age, fare etc. However the row headers only contain indexes 0, 1, 2, etc. To plot
matrix plots, we need useful information on both columns and row headers. One way to do this is to
call the corr() method on the dataset. The corr() function returns the correlation between all the
numeric columns of the dataset. Execute the following script:
[Link]()
In the output, you will see that both the columns and the rows have meaningful header information,
as shown below:
: DSBDAL
Now to create a heat map with these correlation values, you need to call the heatmap() function and
pass it your correlation dataframe. Look at the following script:
corr = [Link]()
[Link](corr)
From the output, it can be seen that what heatmap essentially does is that it plots a box for every
combination of rows and column value. The colour of the box depends upon the gradient. For
instance, in the above image if there is a high correlation between two features, the corresponding
cell or the box is white, on the other hand if there is no correlation, the corresponding cell remains
black.
The correlation values can also be plotted on the heatmap by passing True for the annotparameter.
Execute the following script to see this in action:
annot=True)
Third Year of Artificial Intelligence and Data Science Software Laboratory III
You can also change the colour of the heatmap by passing an argument for the cmap
parameter. For now, just look at the following script:
corr = [Link]()
[Link](corr)
a. Cluster Map:
In addition to the heat map, another commonly used matrix plot is the cluster map. The
cluster map basically uses Hierarchical Clustering to cluster the rows and columns of the
matrix.
Let's plot a cluster map for the number of passengers who travelled in a specific month ofa
specific year. Execute the following script:
4. Checking how the price of the ticket (column name: 'fare') for each
83
Third Year of Artificial Intelligence and Data Science Software Laboratory III
From the histogram, it is seen that for around 730 passengers the price of the ticket is 50.
For 100 passengers the price of the ticket is 100 and so on.
Conclusion-
Seaborn is an advanced data visualisation library built on top of Matplotlib library. In this
assignment, we looked at how we can draw distributional and categorical plots using the
Seabornlibrary. We have seen how to plot matrix plots in Seaborn. We also saw how to
change plot stylesand use grid functions to manipulate subplots.
Assignment Questions
1. List out different types of plot to find patterns of data
2. Explain when you will use distribution plots and when you will use categorical
plots.
3. Write the conclusion from the following swarm plot (consider titanic dataset)
84
Third Year of Artificial Intelligence and Data Science Software Laboratory III
Assignment No: 9
1. Use the inbuilt dataset 'titanic' as used in the above problem. Plot a box plot for distribution
of
age with respect to each gender along with the information about whether they survived or
not. (Column names : 'sex' and 'age')
2. Write observations on the inference from the above statistics.
Objective:
Students should be able to perform the data Visualization operation using Python on any
open-source dataset.
Prerequisite:
1. Basic of Python Programming
2. box plot Library, Concept of Data Visualization.
Theory:
Data Visualisation plays a very important role in Data mining. Various data scientists spent
their time exploring data through visualisation. To accelerate this process, we need to have
a well-documentation of all the plots.
Even plenty of resources can’t be transformed into valuable goods without planning and
architecture.
The box plot is used to display the distribution of the categorical data in the form of
85
Third Year of Artificial Intelligence and Data Science Software Laboratory III
quartiles. The centre of the box shows the median value. The value from the lower whisker
to the bottomof the box shows the first quartile. From the bottom of the box to the middle
of the box lies the second quartile. From the middle of the box to the top of the box lies the
third quartile and finally from the top of the box to the top whisker lies the last quartile.
Now let's plot a box plot that displays the distribution for the age with respect to each gender.
You need to pass the categorical column as the first parameter (which is sex in our case)
and the numeric column (age in our case) as the second parameter. Finally, the dataset is
passed as the third parameter, take a look at the following script:
Let's try to understand the box plot for females. The first quartile starts at around 1 and ends
at 20 which means that 25% of the passengers are aged between 1 and 20. The second
quartile starts at around 20 and ends at around 28 which means that 25% of the passengers
are aged between20 and 28. Similarly, the third quartile starts and ends between 28 and 38,
hence 25% passengers are aged within this range and finally the fourth or last quartile starts
at 38 and ends around 64.
If there are any outliers or the passengers that do not belong to any of the quartiles, they are
called outliers and are represented by dots on the box plot.
You can make your box plots more fancy by adding another layer of distribution. For
instance, ifyou want to see the box plots of forage of passengers of both genders, along with
the informationabout whether or not they survived, you can pass the survived as value to
86
Third Year of Artificial Intelligence and Data Science Software Laboratory III
Now in addition to the information about the age of each gender, you can also see the
distribution of the passengers who survived. For instance, you can see that among the male
passengers, on average more younger people survived as compared to the older ones.
Similarly, you can see that the variation among the age of female passengers who did not
survive is much greater than the age of the surviving female passengers.
The dataset contains 891 rows and 15 columns and contains information about the
passengers who boarded the unfortunate Titanic ship. The original task is to predict whether
or not the passenger survived depending upon different features such as their age, ticket,
cabin they boarded, the class of the ticket, etc.
Theory:
A box plot is a graphical representation of data that shows the distribution of a dataset. It
shows the median, quartiles, and outliers of a dataset. The box represents the interquartile
range (IQR), which is the range between the first quartile (Q1) and the third quartile (Q3).
87
Third Year of Artificial Intelligence and Data Science Software Laboratory III
The line inside the box represents the median. The whiskers represent the range of data,
excluding outliers, and the dots or asterisks represent outliers.
Example:
Here is an example code to plot a box plot for the distribution of age with respect to each
gender in the 'titanic' dataset using Python:
titanic = sns.load_dataset('titanic')
[Link]()
In this code, we first import the required libraries 'seaborn' and '[Link]'. We then
load the 'titanic' dataset using the 'sns.load_dataset()' function. Next, we use the
'[Link]()' function to plot the box plot for the distribution of age with respect to each
gender, along with information about whether they survived or not. We pass the 'sex' and
'age' columns of the dataset as the x and y parameters, respectively. We also pass the
'survived' column of the dataset as the hue parameter, which colors the box plot based on
whether the passengers survived or not. Finally, we use the '[Link]()' function to display
the plot.
Conclusion-
We learned how to plot a box plot for the distribution of age with respect to each gender in the
'titanic' dataset, along with information about whether they survived or not. We also drew some
observations from the statistics. Box plots are a useful tool to visualize the distribution of data
and to draw inferences from it.
Assignment Questions
1. List out different types of plot to find patterns of data
2. Explain when you will use distribution plots and when you will use categorical
plots.
3. Write the conclusion from the following swarm plot (consider titanic dataset)
88
Third Year of Artificial Intelligence and Data Science Software Laboratory III
Group B
Assignment 11
PROBLEM STATEMENT:
Create databases and tables, insert small amounts of data, and run simple queries
using Impala
OBJECTIVE:
1. To Learn and understand the concepts of Python Libraries.
2. To learn and understand the Data Science for the analysis of real time problems
3. To understand and practice Data Preprocessing & Data Normalization.
The objectives of this manual are to provide you with the knowledge to create databases
and tables in Impala, insert data into these tables, and execute simple queries.
Students will have a good understanding of how to use Impala to analyze data.
Theory:
Impala is an open-source SQL query engine allows you to query data stored in Apache
Hadoop clusters. It's a fast, distributed, and highly scalable system that can run complex
queries on large datasets in near real-time. In this manual, we will guide you through the
process of creating databases and tables, inserting small amounts of data, and running simple
queries using Impala.
Impala uses a distributed architecture, which means that data is stored across multiple nodes in
a cluster. Impala also uses a query engine that allows you to write SQL queries that are executed
in parallel across the nodes in the cluster. This allows for fast query execution, even on large
datasets.
Impala uses a metadata store to keep track of the databases and tables that are created. The
metadata store is used to store information about the location of the data, the schema of the
tables, and other metadata. Impala supports a wide range of data formats, including Parquet,
Avro, and RCFile.
Example:
To create a database in Impala, you can use the following SQL command:
89
Third Year of Artificial Intelligence and Data Science Software Laboratory III
INSERT INTO my_table VALUES (1, 'John', 25), (2, 'Jane', 30), (3, 'Bob', 40);
This will insert three rows into the table, each with a unique id, name, and age. To run a simple
query on this table, you can use the following SQL command:
Conclusion:
Impala is a powerful SQL query engine that can be used to analyze large datasets stored in
Apache Hadoop clusters. We have seen how to create databases and tables, insert data, and
execute simple queries in Impala.
90