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

Exploratory Data Analysis With Python - Notes I

Uploaded by

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

Exploratory Data Analysis With Python - Notes I

Uploaded by

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

OHM SRI SAIRAM

UNIT-I
Introduction to EDA and Python Environment

Introduction to Data Science

Data Science is a multidisciplinary field that combines statistical methods, programming skills, and domain
knowledge to extract meaningful insights and knowledge from structured and unstructured data. It is at
the heart of modern technological advancements, empowering industries to make data−driven decisions and
innovate rapidly.

What is Data Science?

Data Science involves the process of collecting, processing, analyzing, and interpreting data to uncover
patterns and support decision−making. It draws techniques from fields such as:

• Statistics – for understanding data distribution and relationships


• Computer Science – for programming and data engineering
• Mathematics – for algorithm design and modeling
• Domain Expertise – for applying results meaningfully in specific industries

Key Components of Data Science

1. Data Collection: Gathering data from various sources such as databases, web APIs, sensors, or
spreadsheets.
2. Data Cleaning and Preprocessing: Handling missing values, removing noise, and converting raw
data into usable formats.
s. Exploratory Data Analysis (EDA): Visualizing and summarizing the main characteristics of data
using graphs and statistics.
4. Data Modeling: Applying algorithms (e.g., regression, classification, clustering) to find patterns or
make predictions.

Exploring Data Analysis with Python(UNIT −I) Page 1


OHM SRI SAIRAM

5. Model Evaluation: Assessing the performance of models using metrics like accuracy, precision,
recall, or RMSE.
a. Deployment and Visualization: Presenting the results in interactive dashboards or embedding
models in applications for real−time usage.

Common Tools and Technologies

• Programming Languages: Python, R, SQL


• Libraries/Frameworks: Pandas, NumPy, Scikit−learn, TensorFlow, Matplotlib, Seaborn
• Databases: MySQL, PostgreSQL, MongoDB
• Big Data Tools: Hadoop, Spark
• Cloud Platforms: AWS, Azure, Google Cloud
• Visualization Tools: Tableau, Power BI, Plotly

Applications of Data Science

Data Science is transforming numerous sectors, such as:

• Healthcare: Predicting disease, personalized treatments


• Finance: Fraud detection, credit risk analysis
• Retail: Customer segmentation, recommendation engines
• Transportation: Route optimization, predictive maintenance
• Agriculture: Crop yield prediction, smart farming
• Entertainment: Content recommendations, trend analysis

Data Scientist Roles

• Data Analyst – Analyzes and visualizes data for business insights.


• Machine Learning Engineer – Builds predictive models and AI systems.
• Data Engineer – Designs and maintains data infrastructure and pipelines.
• Business Intelligence Analyst – Focuses on KPIs and business metrics.
• Data Scientist – A hybrid role that spans all aspects from data wrangling to decision−making.

Data Science Life Cycle


The Data Science Life Cycle is a structured approach that outlines the major steps involved in solving a
data−driven problem using data science methodologies. Just like the software development life cycle
(SDLC), the data science life cycle ensures that data projects are systematic, repeatable, and goal−oriented.

1. Problem Definition
This is the most critical phase, as a poorly defined problem leads to poor outcomes.

Key questions:
• What is the business problem?

Exploring Data Analysis with Python(UNIT −I) Page 2


OHM SRI SAIRAM

• What are the success criteria?


• What value will the solution bring?

Example:
For a bank, the goal might be predicting loan default to reduce risk.

2. Data Collection
Once the problem is defined, the next step is to gather relevant data.
Sources:
• Databases (MySQL, MongoDB)
• APIs (Twitter API, Weather API)
• Web scraping
• Public datasets (Kaggle, UCI, [Link])
• IoT devices or mobile applications

Activities:
• Identify relevant data sources
• Connect and extract data
• Store in a data warehouse or data lake

3. Data Cleaning and Preprocessing


Raw data is often messy, incomplete, or inconsistent. Cleaning it ensures the quality of analysis.
Tasks Involved:
• Handling missing values (imputation or removal)
• Treating outliers
• Removing duplicates
• Converting data types
• Encoding categorical variables
Tools:
• Python (Pandas, NumPy)
• R
• SQL

4. Exploratory Data Analysis (EDA)


EDA helps in understanding the underlying patterns in the data and forms the basis for modeling decisions.
Activities:
• Summary statistics (mean, median, mode, etc.)
• Visualization (histograms, scatter plots, box plots)
• Correlation analysis
• Univariate, bivariate, and multivariate analysis
Goals:
• Spot trends and outliers
• Form hypotheses
• Identify data transformations needed

Exploring Data Analysis with Python(UNIT −I) Page 3


OHM SRI SAIRAM

5. Feature Engineering and Selection


This involves creating new input variables (features) and selecting the most relevant ones for modeling.
Techniques:
• Binning, scaling, normalizing
• Creating interaction terms
• Removing highly correlated variables
• Using feature importance scores
Why It's Important:
Good features can dramatically improve the accuracy of models.

6. Model Building
This is the core stage where machine learning or statistical models are trained to learn from the data.
Model Types:
• Supervised Learning: Regression, Classification
• Unsupervised Learning: Clustering, Dimensionality Reduction
• Reinforcement Learning: Agent−based decision making
Process:
• Split data into training and testing sets
• Choose algorithms (e.g., decision trees, SVM, neural networks)
• Train the model on the training data

7. Model Evaluation
The model is assessed for its performance using various metrics, depending on the problem type.
Common Metrics:
• Accuracy, Precision, Recall, F1−score (for classification)
• RMSE, MAE, R² (for regression)
• Confusion matrix, ROC−AUC curve
Techniques:
• Cross−validation
• Hyperparameter tuning
• Model comparison

8. Model Deployment
Once a model performs well, it’s deployed to make real−time or batch predictions.
Deployment Methods:
• REST APIs
• Web applications (Flask, Django)
• Cloud platforms (AWS SageMaker, GCP AI Platform)
Considerations:
• Scalability
• Monitoring and feedback loops
• Version control

Exploring Data Analysis with Python(UNIT −I) Page 4


OHM SRI SAIRAM

9. Monitoring and Maintenance


After deployment, the model must be continuously monitored for data drift, performance degradation,
and user feedback.
Activities:
• Retraining with new data
• Logging predictions
• Alerting when thresholds are breached

Introduction to EDA (Exploratory Data Analysis)


Exploratory Data Analysis (EDA) is the initial and critical phase in any data science or machine learning
project. It involves analyzing datasets to summarize their main characteristics, often using visual methods.
The goal of EDA is to understand the structure, patterns, and anomalies in the data before applying any
modeling techniques.

Importance of EDA in the Data Science Life Cycle


Exploratory Data Analysis (EDA) is a foundational and essential phase in the Data Science Life Cycle.
It serves as the bridge between raw data and actionable insights. While data collection and cleaning prepare
the data, EDA allows data scientists to understand the data, ask the right questions, and guide modeling
decisions.
1. Understanding the Data
EDA provides a clear picture of the dataset’s structure, content, and quality. It answers questions like:
• What variables are present?
• What are the distributions?
• Are there missing or erroneous values?
2. Detecting Data §uality Issues
EDA helps in identifying:
• Missing values
• Outliers
• Inconsistent data types
• Duplicate records
This ensures that further steps are not built on faulty data.
3. Identifying Patterns and Trends
With EDA, you can discover:
• Relationships between variables (e.g., age and salary)
• Groupings or clusters
• Seasonal or time−based trends
These insights inform feature selection and business understanding.

Exploring Data Analysis with Python(UNIT −I) Page 5


OHM SRI SAIRAM

4. Feature Engineering Guidance


EDA shows which variables are:
• Most relevant
• Redundant
• Non−informative
This helps in creating or transforming variables to improve model performance.
5. Improves Model Accuracy
A good understanding of data through EDA leads to:
• Better preprocessing
• Informed algorithm selection
• Better tuning of model parameters
All of which enhance predictive accuracy.
6. Assumptions Testing
Many models assume certain data characteristics (like linearity, normal distribution). EDA helps:
• Validate those assumptions
• Decide whether transformation (e.g., log−scaling) is needed
7. Visual Storytelling
EDA uses graphs and plots to:
• Communicate findings to non−technical stakeholders
• Create compelling narratives around data
• Support business decisions visually

Setting up Python Environment: Anaconda and Jupiter


Environments
(Reference-I)

Setting up Python Environment: VS Code


[Link]

Exploring Data Analysis with Python(UNIT −I) Page 6


OHM SRI SAIRAM

Introduction to NumPy
NumPy (Numerical Python) is a fundamental package for scientific computing in Python. It provides
efficient arrays, mathematical functions, and tools for working with large, multi-dimensional data. It
is the foundation for many libraries in data science and machine learning, such as Pandas, SciPy, and
TensorFlow.

Installing NumPy
Using pip:
pip install numpy

Basic Example

import numpy as np
# Create a 1D array
arr = [Link]([1, 2, 3, 4, 5])
print(arr)
# Create a 2D array (matrix)
mat = [Link]([[1, 2], [3, 4]])
print(mat)

Numpy Operations

SNO Category Operation / Code Output


1 Array Creation [Link]({1, 2, s)) {1 2 s)
2 2D Array [Link]({{1, 2), {s, 4))) {{1 2) {s 4))
s Zeros [Link]((2, 2)) {{o. o.) {o. o.))
4 Ones [Link]((1, s)) {{1. 1. 1.))
5 Full [Link]((2, 2), c) {{c c) {c c))
a Eye (Identity) [Link](s) {{1. o. o.) {o. 1. o.) {o. o. 1.))
c Random floats [Link](2,2) {{... ...) { ...... ))
8 Random integers [Link](1, 1o, (2, 2)) Example: {{4 8) {2 9))
9 Arange [Link](o, 1o, 2) {o 2 4 a 8)
1o Linspace [Link](o, 1, 5) {o. o.25 o.5 o.c5 1. )
11 Shape [Link] (2, s)
12 Dimensionality [Link] 2
1s Size [Link] a
14 Indexing arr{1, 2) value at row 1, col 2
15 Slicing arr{:, 1) All rows, column 1
1a Reshape [Link](s, 2) Reshaped sx2 matrix

Exploring Data Analysis with Python(UNIT −I) Page 7


OHM SRI SAIRAM

SNO Category Operation / Code Output


1c Flatten [Link]() 1D array
18 Transpose arr.T Transposed matrix
19 Addition a+b Element−wise sum
2o Multiplication a*b Element−wise product
21 Dot product [Link](a, b) Scalar or matrix result
22 Power a ** 2 Square of each element
2s Mean [Link](arr) Mean of elements
24 Sum [Link](arr) Total sum
25 Min/Max [Link](arr), [Link](arr) Minimum, Maximum
2a Std Dev [Link](arr) Standard deviation
2c Axis sum [Link](arr, axis=o) Column−wise sum
28 Boolean Mask arr{arr > 25) Filtered array
29 Logical operators (arr > 1o) & (arr < so) Boolean array
so Inverse [Link](a) Matrix inverse
s1 Determinant [Link](a) Scalar value
s2 Eigenvalues [Link](a) Eigenvalues and vectors
ss Random seed [Link](o) Ensures reproducibility
s4 Broadcasting a + b (shape−matched) Automatically expanded
s5 Unique elements [Link](arr) {unique values)
sa Sort [Link](arr) Sorted array
sc Where condition [Link](arr == 1o) Index/indices
s8 Concatenate [Link]({a, b)) Combined 1D array
s9 Vstack [Link]({a, b)) Vertical stack
4o Hstack [Link]({a, b)) Horizontal stack
41 NaN detection [Link](arr) Boolean array
42 Inf detection [Link](arr) Boolean array
4s Remove NaN arr{~[Link](arr)) Valid data only

For More Numpy Programs Practice- (Reference-2)

Exploring Data Analysis with Python(UNIT −I) Page 8


OHM SRI SAIRAM

Introduction to Pandas in Python


Pandas is a powerful, flexible, and easy−to−use open−source data analysis and manipulation library for
Python.
It provides two core data structures:
• Series – One−dimensional labeled array
• DataFrame – Two−dimensional labeled data structure (like an Excel spreadsheet or SQL table)
Importance of Pandas
1. Efficient Data Handling: Provides powerful data structures like DataFrame and Series for easy
manipulation of structured/tabular data.
2. Simplifies Data Cleaning & Preprocessing: Offers built−in tools to handle missing values, duplicates,
data type conversions, and inconsistencies.
3. Supports Data Analysis & Exploration: Enables filtering, sorting, grouping, and summarizing data
for quick insight generation during EDA.
4. Flexible Input/output Capabilities: Supports reading/writing from CSV, Excel, JSON, SQL, and
more, making it versatile for real−world data sources.
5. Seamless Integration & High Performance: Works efficiently with large datasets and integrates well
with NumPy, Matplotlib, Seaborn, and Scikit−learn.

Installing Pandas
Using pip:
pip install pandas

Basic Example:
Series – One−dimensional Arrays

import pandas as pd
s = [Link]([10, 20, 30, 40])
print(s)

#Output
0 10
1 20
2 30
3 40
dtype: int64

DataFrame – Two−dimensional Matrices

import pandas as pd
data = {
'Name': ['Mahesh', 'Paani', 'Suresh'],
'Age': [25, 30, 35],
'City': ['Chennai', 'Bangalore', 'Tirupati']

Exploring Data Analysis with Python(UNIT −I) Page 9


OHM SRI SAIRAM

}
df = [Link](data)
print(df)

#Output
Name Age City
o Mahesh 25 Chennai
1 Paani so Bangalore
2 Suresh s5 Tirupati

Pandas Operations

SNO Operation Description Example


1 [Link](data) Create a Series [Link]({1o, 2o, so))
2 df=[Link](data) Create a DataFrame [Link]({'A':{1,2), 'B':{s,4)})
s [Link]() View first 5 rows [Link]()
4 [Link](n) View last n rows [Link](2)
5 [Link] Get (rows, columns) [Link]
a [Link] List column names [Link]
c [Link] Show data types [Link]
8 [Link]() Summary of DataFrame [Link]()
Summary stats for numeric
9 [Link]() [Link]()
columns
1o df{'col') Access column as Series df{'Age')
11 df{{'col1','col2')) Access multiple columns df{{'Age','Salary'))
12 [Link]{row) Access row by label [Link]{1)
1s [Link]{row) Access row by index [Link]{o)
14 df{condition) Filter rows using condition df{df{'Age') > 25)
15 df.sort_values('col') Sort by column df.sort_values('Age')
1a df{'new') = ... Add new column df{'Tax') = df{'Salary')*o.1
1c [Link]('col', axis=1) Drop column [Link]('Age', axis=1)
18 [Link](index) Drop row [Link](2)
19 [Link]() Check for missing values [Link]()
2o [Link]() Drop missing values [Link]()
21 [Link](value) Fill missing values [Link](o)
22 [Link]() Check for duplicates [Link]()
2s df.drop_duplicates() Remove duplicates df.drop_duplicates()
24 [Link]() Column−wise mean [Link]()

Exploring Data Analysis with Python(UNIT −I) Page 10


OHM SRI SAIRAM

SNO Operation Description Example


25 [Link]() Column−wise sum [Link]()
2a [Link]('col') Group by column [Link]('City')
2c [Link]().mean() Group and aggregate [Link]('City'){'Salary').mean()
Apply function to
28 [Link](func) df{'Age').apply([Link])
rows/columns
29 [Link]('type') Change data type df{'Age').astype(float)
so [Link](columns={}) Rename columns [Link](columns={'Age':'Vears'})
Concatenate DataFrames
s1 [Link]({df1, df2)) [Link]({df1, df2))
(rows)
[Link]({df1, df2), Concatenate DataFrames
s2 [Link]({df1, df2), axis=1)
axis=1) (columns)
Merge DataFrames on key
ss [Link](df1, df2, on=...) [Link](df1, df2, on='ID')
column
s4 df.to_csv('[Link]') Export to CSV df.to_csv('[Link]', index=False)
s5 [Link]() Plot data (line plot by default) df{'Sales').plot()
sa df.value_counts() Frequency of unique values df{'City').value_counts()
Count of unique values in each
sc [Link]() [Link]()
column
s8 [Link](n) Random sample of n rows [Link](s)
s9 [Link]() Correlation matrix [Link]()
4o df.reset_index() Reset index to default integers df.reset_index(drop=True)

Basic Operations
1. Create DataFrame

import pandas as pd
data = {
'Name': ['Mahesh', 'Paani', 'Suresh'],
'Age': [25, 30, 35],
'City': ['Chennai', 'Bangalore', 'Tirupati']
}
df = [Link](data)
print(df)

#Output
Name Age City
o Mahesh 25 Chennai
1 Paani so Bangalore
2 Suresh s5 Tirupati

Exploring Data Analysis with Python(UNIT −I) Page 11


OHM SRI SAIRAM

2. info()

Syntax: info()

Displays a concise summary of the DataFrame.

import pandas as pd
data = {
'Name': ['Mahesh', 'Paani', 'Suresh'],
'Age': [25, 30, 35],
'City': ['Chennai', 'Bangalore', 'Tirupati']
}

df = [Link](data)
[Link]()

#Output
<class '[Link]'>
RangeIndex: s entries, o to 2
Data columns (total s columns):
# Column Non−Null Count Dtype
−−− −−−−−− −−−−−−−−−−−−−− −−−−−
o Name s non−null object
1 Age s non−null inta4
2 City s non−null object
dtypes: inta4(1), object(2)
memory usage: 2oo.o+ bytes

2. describe()

The describe() function in Pandas is used to generate summary statistics of numerical columns in a
DataFrame.

Syntax : [Link]()

Metric Meaning
count Number of non−null entries
mean Average of the values
std Standard deviation
min Minimum value
25% 1st quartile (25th percentile)
5o% Median (5oth percentile)
75% srd quartile (c5th percentile)
max Maximum value

Exploring Data Analysis with Python(UNIT −I) Page 12


OHM SRI SAIRAM

import pandas as pd
data = {
'Name': ['Mahesh', 'Paani', 'Suresh'],
'Age': [25, 30, 35],
'City': ['Chennai', 'Bangalore', 'Tirupati']
}
df = [Link](data)
print([Link]())

#Output
Age
count [Link]
mean [Link]
std [Link]
min [Link]
25% 2c.5ooooo
5o% [Link]
c5% s2.5ooooo
max [Link]

Note: describe () by default only includes numeric columns. To include all columns (including object
types like strings), use:

[Link](include='all')

3. [Link]

[Link] returns a tuple representing the dimensions of a [Link] in terms of (rows, columns)

Syntax:

[Link]

Example:

import pandas as pd
data = {
'Name': ('Mahesh', 'Paani', 'Suresh'),
'Age': (25, 3o, 35),
'City': ('Chennai', 'Banglore', 'Tirupati')
}

df = [Link](data)
print([Link])

Exploring Data Analysis with Python(UNIT −I) Page 13


OHM SRI SAIRAM

#Output
(3, 3)

Output Explanation
s rows → one for each person: Mahesh, Paani, Suresh
s columns → Name, Age, City

For more Pandas Programs Practice-(Reference-3)

Exploring Data Analysis with Python(UNIT −I) Page 14


Setting up Python Environment
Anaconda and Jupiter Environments
(Reference-I)
Data Science and Machine Learning for Engineering Applications
Python installation, Anaconda-Navigator, and Jupyter notebook: beginner’s tutorial

Introduction
This tutorial will show you how to: i) install Python with Anaconda-Navigator (Section 1);
ii) manage virtual environments with Anaconda (Section 2); iii) install python packages
(Section 3); iv) use Jupyter Notebook (Section 4).
1 Install Anaconda-Navigator
Anaconda Navigator is a desktop GUI (Graphical User Interface) allowing you to launch
applications and manage conda packages and environments without command-line
commands. It includes a GUI, Anaconda Navigator, as a graphical alternative to the
command line interface. Navigator can search for packages, install them in an environment,
run the packages, and update them. The Anaconda guide can be found at the following URL:
[Link]
1.1 Download Anaconda-Navigator
From the Anaconda website at the following URL:
[Link] download the installation files for your
operating system (i.e., MacOS, Linux, or Windows). Install the latest version of python with

Anconda-Navigator. In this case, python 3.9.

16
1.2 Install Anaconda-Navigator
When the download is finished, double-click on the downloaded file in the bottom left-hand
corner of your browser. This will start the installation of Anaconda-Navigator. The
installation process depends on your operating system.

2 Create a virtual environment with Anaconda-Navigator


Python requires a different version for different kinds of applications. The application needs
to run on a specific language version because it requires certain dependencies that are present in
older versions but change in newer versions. Virtual environments make it easy to separate
different applications and avoid problems with different dependencies [4]. Multiple ways of
creating an environment include virtualenv, venv, and conda. However, the conda command is
the preferred interface for managing installations and virtual environments with the Anaconda
Python distribution.
This section shows how to create a virtual environment with Anaconda-Navigator, by
exploiting the GUI (without the command line). If you want to learn more about creating a
virtual environment with conda entirely with the command line, you can read more on this
URL: [Link] manage-your-python-virtual-environment-with-
conda-a0d2934d5195. This last option can be useful to run complex python projects on a
17
remote server where the GUI is not available. However, it is not required for this course.

2.1 Select the environments


Click on the "Environments" button from the left menu. It will show the list of all your environments.

2.2 Create a virtual environment


Click the "Create" button in the bottom left-hand corner to create a new virtual environment.

18
2.3 Choose a new name for your virtual environment
You have to specify the environment name and the Python version. Then, click the "Create"
button.

19
2.4 Check the installed packages
Once created a new environment, the list of all installed packages in that environment
will be shown. Notice that some packages are already installed.

20
3 Packages
To install a new package in the virtual environment, you have two options:
• Using the Anaconda-Navigator GUI directly (Section 3.1).
• Using the command line with the conda or pip commands (Section 3.2).

The main difference between conda and the pip package manager is how the package
dependencies are managed. When pip installs a package, it also automatically installs any
dependent Python packages without checking if these conflict with previously installed
packages. Therefore, it will install a package and any of its dependencies regardless of the
state of the existing installation. In contrast, conda analyzes the current environment,
including everything currently installed and any version limitations specified. It works out
how to install a compatible set of dependencies and shows a warning if this cannot be done
[5]. Using the Anaconda-Navigator GUI to install a package will exploit the conda
package manager. You can learn more about the differences between conda and pip at
the following URL: [Link] com/blog/understanding-conda-and-pip.

3.1 Install a package with the navigator GUI


Installing any package through Anaconda-Navigator GUI is straightforward. You have to
search for the required package, select a package, and click on "Apply" to install it

3.1.1 Search the required package


Select the option "Not Installed" in the top-center menu.

21
Then search for the package that you want to install by typing the name in the
textbox (e.g., in this case, NLTK).

22
3.1.2 Select and install the required package
The Anaconda-Navigator will search in the conda repository for all the conda packages
matching the typed name. Then, select the wanted package line and click on the "Apply"
button in the right-hand bottom corner.

23
It will open a new window with all the dependencies for that package. The conda package
manager
will install all the dependencies for you. Click the "Apply" button to start the package installation.

24
Wait for the download and installation. It could take some minutes.

3.1.3 Check the installed package


You can check if the package has been correctly installed by selecting the "Installed"
selection in the drop-down menu.

25
A new line corresponding to the installed package (in this case, NLTK) should appear.

26
3.1.4 Uninstall the package
To uninstall a package, click the green ✓ on the line corresponding to the package you want to
remove.

Then, click on the "Mark for Removal" option.

27
The green ✓ will become a red crossed box. Then, click the "Apply" button in the right-
hand bottom corner.

It will open a new window with all the packages that will be removed. Finally, click the
"Apply" button in the right-hand bottom corner to start the package uninstallation.

28
3.2 Install a package with the command line
This Section will show you how to install packages by terminal, with the pip (Section 3.2.1) and
conda (Section 3.2.2) commands. To open the terminal for your environment, select the
corresponding line and click the green ▷ symbol.

Then, select the "Open Terminal" option.

29
This will open the terminal with the selected environment activated (i.e., if you install a
package, it will be installed in the activated environment). You can see the activated environment
in the round brackets at the left of the line (e.g., mlds-env ).

3.2.1 Install a package with the pip command


Some packages could not be available in the conda environment. You can find and install
the package with another package manager like pip. To install a package with the pip
command, type the command pip install package-name, in this case, NLTK. You can
find the specific pip command for each package installation on the official documentation
websites.

Press enter on your keyboard to start the download and the installation. It could take some
minutes

3.2.2 Install a package with the conda command


Instead, to install a package with the conda command, type the command conda install
package-name, in this case, NLTK. You can find the specific conda command for each

30
package installation on the official documentation websites.

The terminal will show all the dependencies (i.e., other packages) that will be
installed. press y and then enter to start the download and the installation.

4 Jupyter notebook
Jupyter Notebook is a powerful tool for developing and presenting data science projects
interac-tively. In a Jupyter Notebook document, you can combine code, visualizations,
texts, and dis-play outputs. You can find a good guide at the following URL [3]:
[Link] jupyter-notebook-tutorial/.
The following sections will show you how to: i) install Jupyter notebook using Anaconda (Section 4.1);
ii) launch Jupyter from Anaconda 4.2; iii) create your first Jupyter notebook 4.3; iv) use
cells and kernels to effectively exploit Jupyter notebooks (Sections 4.4 and 4.5); v) exploit
advanced features of Jupyter notebooks (Section 4.6).

4.1 Install Jupyter


To install Jupyter, you must first go into the Home section, which contains all the applications
for the current environment. Please check that the created virtual environment is selected (in this
31
case, mlds-env ).

Then, click the "Install" button under the Jupyter application box.

32
This will start the download and installation process. It may require some minutes.

4.2 Launch Jupyter


After the installation, the Anaconda dashboard will show you the "Launch" button
under the Jupyter Notebook box. Click on "Launch" to start Jupyter notebook.

33
It will open the Notebook Dashboard for exploring, editing, and creating notebooks. Here
you can create new folders, notebooks, etc. The URL for the dashboard is
[Link] Localhost is not a website but indicates that the content is run on
your local machine.

4.3 Create a new Jupyter notebook


To create your first Jupyter notebook click the "New" drop-down button in the top-right menu
and select "Python 3". This will open your first Jupyter notebook in a new tab. You can open
and run multiple notebooks simultaneously in multiple tabs.

34
It will create a new file [Link]. Each .ipynb file is a text file that describes the contents
of your notebook in a format called JSON. Each time you create a new notebook, a new .ipynb
file will be created. Notice that the notebook extension .ipynb is different from the normal
python file extension .py. Please rename now your filename from the top text box, or, very
soon, you will have several [Link], Untitled (1).ipynb notebooks. The notebook’s
name should explain the content.

35
4.3.1 The Jupyter Notebook interface
The two main concepts that you should learn to use notebooks properly are cells and kernels:
• The cell is a container for code to be executed or text to be displayed in the notebook by the
kernel (Section 4.4).
• The kernel is a computational engine that executes the code contained in a notebook
document (Section 4.5).
4.4 Notebook cells
Cells compose the body of the notebook. They could contain code, plain text, images, LaTeX,
math formulas, etc. There are two main cell types that you should learn:
• Code cells (Section 4.4.1)
• Markdown cells (Section 4.4.2)
4.4.1 Code cell
Code cells contain code to be executed in the kernel. When the code is run, the notebook displays
the output below the code cell that generated it. Note that cells do not have to be executed in
order. It is also possible to execute a cell at the end and then one at the beginning of the notebook.
The cell type is shown in the drop-down menu. The default type is Code.

You can run a Code cell by:


• clicking the "Run" button
• pressing ctr + enter
• pressing maiusc + enter (in this case, it also goes to the next cell)

In this case, the execution of the cell will print the string "This is my first

36
Jupyter notebook" as output. Each cell could produce an output.

The following cell will create a new variable called x and assigns the values of 10 to x. In
this case, no output is produced by the cell.

You should use the print function to output the value of x.

4.4.2 Markdown cell


Markdown cells contain text formatted using Markdown [2] and displays its output in-place when
the Markdown cell is run. Markdown is a lightweight markup language that you can use to add
formatting elements to plaintext text documents. This cheat sheet will cover the most common
elements (cheatsheet). To define a Markdown cell, select the cell and click on the "Markdown"
option in the top drop-down menu.

37
4.4.3 Text Markdown cell
You can write plaintext in a cell. This text is not a code. Therefore, it will not be really
executed. You can also empathize text with bold or italic with **bold** and *italic*
respectively.

38
If you run the cell containing plaintext, it will be displayed formatted as output. This can add narrative
to your Jupyter notebook.
4.4.4 Heading Markdown cell
You can also add first, second, and third-level headings.

39
OHM SRI SAIRAM

4.5 Notebook kernel


When you run a code cell, that code is executed within the kernel, and the
outputs are returned to the cells to be displayed. The kernel’s state persists
over time between cells. It pertains to the document as a whole and not
individual cells. For example, if you import libraries in one cell, they will be
available in another. If you define the value of a variable in one cell, the

variable’s value also persists for the other cells.

4.5.1 Restarting a kernel


If you restart the kernel, the notebook’s status is deleted. After the kernel’s
restart, all the values of your variables are reset. To restart the kernel, click
the "Kernel" button in the top menu. Then, select "Restart & Clear Output".
This will restart your kernel and clear all the outputs in the cells. You can
also select "Restart & Run All" to restart your kernel and run all cells in

order.
OHM SRI SAIRAM

Restarting the kernel clears all the cells’ outputs and initializes the run identification

number of each cell.

What do you think will happen if you now print the value of the variable
x again? It will raise an error message because restarting the kernel caused a
reset of the notebook status and, consequently, all the previously defined
variables.
OHM SRI SAIRAM

Therefore, you should define x again to print its value.

4.6 Jupyter notebook advanced tips, tricks, and shortcuts


More advanced tips and commands such as keyboard shortcuts, pretty display, executing
shell commands, using LaTeX could be found here [1]
([Link]

You might also like