Python Primer
Shankar Venkatagiri
Origins
• Authored in 1991 by Guido van Rossum
• Developed it further at Google
• Q: Why did he call it Python? The BDFL has spoken till 4:45
• Advantages of the language
• Easy to understand - no **pointers**
• Interpreted, not compiled
• Not just for R&D, but also for production
• Facilitates big data, ML, AI, NLP, …
Structure
• Q: How does a typical Python program flow?
1. We import some libraries to enable key tasks
2. We read in data from a CSV file/ JSON file/ database/ . . .
3. We clean the data - missing values, outliers, stemming, . . .
4. We perform some computes and store results in variables
5. For repeat tasks, we define functions
6. We visualise the outcomes with some cool graphs
Essentials
• Fact: Building software is 80% thinking & 20% typing
• Syntax is just nuts and bolts - focus on the big picture
• Don’t sweat the small stuff - there’s always Google!
• Don’t be afraid to make mistakes
• Some tasks are routine (e.g. reading in data). The interesting parts
require innovative thinking
• No prizes for getting things right the first time!
Libraries
• Python has a disaggregated approach
• Libraries for everything, in contrast with R
• NumPy - short for Numerical Python
• Vectorised array oriented computing, fast
• Pandas - for tabular data representation
• Time series manipulation
• Seaborn - for visualisation
• Advanced learners can try dash
Cloud
• Google has a free service to code online
• Video: Get started
• Comes with a code snippet helper
• Copy-paste code and modify!
• Sign-in with your Google credentials
• Save all your work in Colab notebooks
Fresh
• Since this is a tutorial,
folders have already
been supplied
• Notebooks,Data
• Just load them
Agenda
• Introductory Python
• NumPy
• Advanced Python
• Pandas, Seaborn
Introduction
Filename:
[Link]
Assign values to variables using =
Multiple assignments in one go - unique to Python!
Basics
• To work with a set of numbers, use an array
• To store a collection, use a list object [ ]
• Negative index has meaning
Control
• Blocks are enclosed by
{ } in C++/Java/…
• Not in Python!
Indentation
• Two styles of for loops
Branching
• Conditional branching with if-elif-else
Function
• Want to reuse your logic?
• Bundle it into a function using def
• day is an argument to the function
Matrices
• Matrices are arrays of lists
• Accessing, transpose
• Can impose boolean conditions
• Q: How can this be applied?
Advanced Filename:
[Link]
• Typically, data is imported from a source
• UCI Repository Bank Marketing Dataset
• Dataset has 45,211 rows, with 16 predictors, 1 response
• Direct marketing campaign of a Portuguese bank
• Required one or more phone contacts to the same client
• Outcome: A term deposit was subscribed or not
S. Moro, R. Laureano and P. Cortez. Using Data Mining for Bank
Direct Marketing: An Application of the CRISP-DM Methodology.
In P. Novais et al. (Eds.), Proceedings of the European Simulation
and Modelling Conference - ESM'2011, pp. 117-121, Guimarães,
Portugal, October, 2011. EUROSIS.
# bank client data:
1 - age
2 - job : type of job (“admin.”,”unknown”,"unemployed","management", ...)
3 - marital : marital status ("married","divorced","single")
4 - education ("unknown","secondary","primary","tertiary")
5 - default: has credit in default? ("yes","no")
6 - balance: average yearly balance, in euros
7 - housing: has housing loan? ("yes","no")
8 - loan: has personal loan? (“yes","no")
# related with the last contact of the current campaign:
9 - contact: contact communication type ("unknown","telephone","cellular")
10 - day: last contact day of the month
11 - month: last contact month of year ("jan", "feb", "mar", ..., "nov", "dec")
12 - duration: last contact duration, in seconds
# other attributes:
13 - campaign: number of contacts performed during this campaign, for this client
14 - pdays: days passed after last contact (-1 = client was not previously contacted)
15 - previous: number of contacts performed before this campaign for this client
16 - poutcome: outcome of previous campaign ("unknown","other","failure","success")
Output variable (desired target):
17 - y - has the client subscribed a term deposit? ("yes","no")
Files
• Load directories into Colab
• Notebooks,Data,…
Step One
• Examine the data that you have uploaded
• Can access an element using its locational coordinates
• Remember, indexing in Python starts from 0
Step Two
• Consider cleaning the data
• Missing values? Impute with mean/median/…
• Outliers? Pursue appropriate action
Describe
Categorical
columns
Numerical
columns
Aggregation
• Many ways to pivot…
Visualisation
• Use seaborn for engaging barcharts, histograms, …
Plots
• Altair plots are interactive - use the touchpad!
Database
• SQLite is “a C-language library
that implements a small, fast,
self-contained, high-reliability,
full-featured, SQL engine.”
• Study code for DB table creation
• Delete the table if you wish
Feed
• Disclaimer: This demo
may not work!
• Oftentimes, data is read in
from an external feed
• E.g. IEX exchange
shares its data
• Separate library
• pandas-datareader
Time series
• Dates are important features
• Read in as “object”
• Needs conversion
• We look at AMZN stock price movements
Kicker
• ChatGPT: Death knell to
rudimentary programming