`
Introduction to Python
`
INTRODUCTION
Computational tools for data science/analytics
Menu based Programming languages
• MS Excel • Python
• Tableau • R
• MS Power BI • Matlab
• SAS Analytics • …
`
INTRODUCTION
Computational tools for data science/analytics
Menu based Programming languages
• MS Excel • Python
• Tableau • R
• MS Power BI • SQL
• SAS Analytics • Matlab
`
INTRODUCTION
Best programming languages for data science/analytics
`
INTRODUCTION
Why?
• Because python and R include a large number of
libraries for data analytics, data visualization,
ML/SL, web scraping, text analytics, deep
learning.
• With these libraries, applications can be
developed in a very efficient way
`
Python
Python is a computer language suitable for
• Data Analysis • Web development
• Data Visualization • Front/Back/Full stack
• Machine learning • Artificial intelligence
• App development • scripting
• Game development
`
Python
Pros
• Open-source languages
• Thousands of libraries
• Available for Linux and MS Windows OS
Cons
• Developers may update the libraries without
notice
`
INTRODUCTION
We will introduce the Python language
and will develop predictive analytics
applications with many different libraries
`
INTRODUCTION – The Python ecosystem
A collection of
• Python language
• User interfaces
• libraries
`
INTRODUCTION – The Python ecosystem
A collection of
• Python language –version 3.6+
• User interfaces (Jupyter Notebook)
• libraries
`
INTRODUCTION – The Python ecosystem
A collection of
• Python language –version 3.6+
• User interfaces (Jupyter Notebook)
• libraries
Use a bundled Python distribution: Anaconda
Anaconda includes Python, Jupyter Notebook, and
most libraries we will need
`
INTRODUCTION – Installing Python
Anaconda comes with over 100 Python libraries
Search for Anaconda download or visit
[Link]
`
INTRODUCTION – Installing Python
Visit [Link]
Select the link for your OS (Mac or Windows)
Open installer and follow the steps
If you have trouble visit
[Link]
guide/troubleshooting/
`
ANACONDA
.
`
ANACONDA
.
`
Jupyter Notebook
`
INTRODUCTION – Jupyter notebook
• A web application for writing code, get the
results in-line, add Markdown text
• Text in the file is used to document the code
• file extension is .ipynb
• file extension for plain python code is .py
`
INTRODUCTION – Jupyter notebook
.
`
INTRODUCTION – Jupyter notebook
Keyboard Input modes
• Edit mode (green ribbon)
• Command mode (blue ribbon)
`
INTRODUCTION – Jupyter notebook
.
`
INTRODUCTION – Jupyter notebook
markdown text
commented line
Python command
Output
`
Python libraries
`
INTRODUCTION – Python libraries
Why do we need libraries?
Libraries allow users
to develop applications
without having to code low-level details
`
INTRODUCTION
.
`
INTRODUCTION – Python library
• A collection of high-level functions
• Used to perform data operations without
the need to write detailed code
• A library is a collection of Modules
• Modules are made of Classes
• Classes include Methods (functions)
`
INTRODUCTION – a Python library
library
…
`
INTRODUCTION – Python library notation
• library
• [Link]
• [Link]
• [Link]()
`
Python Objects
• Python is an object-oriented language
• In Python, everything is an object
• Every object has attributes
• Methods can be applied to an object via the dot
syntax
`
INTRODUCTION – Python libraries
Python library
• numpy for working with arrays
• pandas for working with data sets
• matplotlib for plotting
• statsmodels for statistical modeling
• scikit-learn for model building
`
INTRODUCTION – Python libraries
Python library functions
• numpy for working with arrays
• pandas for working with data sets
• matplotlib for plotting
• statsmodels for statistical modeling
• scikit-learn for machine learning
`
INTRODUCTION – importing a library
import numpy as np
library name alias
`
INTRODUCTION – importing a module
import [Link] as plt
library name module alias
`
INTRODUCTION – importing a class
from sklearn.model_selection import kfold
library name module class
`
INTRODUCTION – importing and using a class
from sklearn.model_selection import kfold
library name module class
kfold. ( …, …, …)
method arguments
`
INTRODUCTION – Python example
.
`
INTRODUCTION – Python example
.
module
class
method
`
Python Data Structures
`
INTRODUCTION
Data Structures p30
• List [a,b,c] collection of objects
• set {a,b,c} collection unique items
• tuple (a,b,c) immutable collection
• dictionary {key1:val1, key2:val2, …} pairs
`
LIST
A list is an ordered collection of objects
• x = [1, 7, 8, 3, 7]
• y = [7, 1, 3, 7, 8]
Objects can be extracted by their positional index
The index starts at position 0
• x[0] = 1 • y[0] = 7
• x[2] = 8 • y[2] = 3
`
INTRODUCTION – slicing a list
.
`
INTRODUCTION – slicing a list
.
`
INTRODUCTION – slicing a list
.
show
values with
index 1
and 2
show last
value
show
first 4
show all
excluding
show all
last value
beyond
the first 4
`
INTRODUCTION – functions for lists
all work in-place
`
INTRODUCTION – lists
`
INTRODUCTION – lists
`
INTRODUCTION
Python constructs p55-56
• iterator
• enumerate
• zip
`
INTRODUCTION – Creating lists
.
`
INTRODUCTION – Creating lists
.
`
INTRODUCTION – Creating lists
.
with for loop
`
INTRODUCTION – Creating lists
.
with for loop
with list comprehension
`
INTRODUCTION – Creating lists
.
`
INTRODUCTION – Creating lists
.
`
INTRODUCTION – Arithmetic Operations
.
`
INTRODUCTION – Creating lists
.
`
FUNCTION
.
create a function
y = x2
`
INTRODUCTION
.
create a function
y = x2
y = x2
`
INTRODUCTION – Apply a function to many values
.
create a function
y = 2x
`
SET
A set is an unordered collection of unique objects
sets do not support indexing or slicing