0% found this document useful (0 votes)
2 views223 pages

Data Analytics and Visualizationwith Python

The document is a syllabus for a course titled 'Python for Accounting Applications' offered at National Taipei University in Fall 2024, covering topics such as Python programming, data structures, data analytics, and machine learning. It outlines the weekly schedule, including midterm and final project reports, and lists essential Python libraries like NumPy, Pandas, and Matplotlib for data analysis and visualization. The course aims to equip students with the skills to apply Python in accounting and data analytics contexts.

Uploaded by

nazeer elnaker
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)
2 views223 pages

Data Analytics and Visualizationwith Python

The document is a syllabus for a course titled 'Python for Accounting Applications' offered at National Taipei University in Fall 2024, covering topics such as Python programming, data structures, data analytics, and machine learning. It outlines the weekly schedule, including midterm and final project reports, and lists essential Python libraries like NumPy, Pandas, and Matplotlib for data analysis and visualization. The course aims to equip students with the skills to apply Python in accounting and data analytics contexts.

Uploaded by

nazeer elnaker
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

Python for Accounting Applications

Data Analytics and Visualization


with Python
1131PAA06
ACC2, NTPU (U2004) (Fall 2024)
Wed 6, 7, 8, (14:10-17:00) (9:10-12:00) (B3F10)

Min-Yuh Day, Ph.D,


Professor
Institute of Information Management, National Taipei University
[Link]
1
2024-10-23
Syllabus
Week Date Subject/Topics
1 2024/09/11 Introduction to Python for Accounting Applications
2 2024/09/18 Python Programming and Data Science
3 2024/09/25 Foundations of Python Programming
4 2024/10/02 Data Structures
5 2024/10/09 Control Logic and Loops
6 2024/10/16 Functions and Modules; Files and Exception Handling
7 2024/10/23 Data Analytics and Visualization with Python
8 2024/10/30 Midterm Project Report (Self-Learning)
2
Syllabus
Week Date Subject/Topics
9 2024/11/06 Self-Learning
10 2024/11/13 Midterm Project Report
11 2024/11/20 Obtaining Data From the Web with Python;
Statistical Analysis with Python
12 2024/11/27 Machine Learning with Python
13 2024/12/04 Text Analytics with Generative AI and Python
14 2024/12/11 Applications of Accounting Data Analytics with Python
15 2024/12/18 Applications of ESG Data Analytics with Python
16 2024/12/25 Final Project Report
3
Data Analytics
and
Visualization
with Python
4
Outline
• NumPy
• Numerical Python N-dimensional array
• Pandas
• Data Analytics
• Matplotlib
• Basic Data Visualization
• Seaborn
• Advanced Visualization
5
Python
Programming
6
Top Programming Languages

[Link] 7
Python is an
interpreted,
object-oriented,
high-level
programming language
with
dynamic semantics.
Source: [Link] 8
Python Ecosystem for Data Science

Source: [Link] 9
Python Ecosystem for Data Science

Source:[Link] 10
The Quant Finance PyData Stack

Source: [Link] 11
Numpy
NumPy
Base
N-dimensional array
package
12
Python
matplotlib

Source: [Link] 13
Python
Pandas

[Link] 14
W3Schools Python

[Link] 15
W3Schools Python: Try Python

[Link] 16
[Link]

[Link] 17
Google’s Python Class

[Link] 18
Google Colab

[Link] 19
Connect Google Colab in Google Drive

20
Google Colab

21
Google Colab

22
Connect Colaboratory to Google Drive

23
Google Colab

24
Google Colab

25
Google Colab

26
Run Jupyter Notebook
Python3 GPU
Google Colab

27
Google Colab Python Hello World
print('Hello World')

28
Python in Google Colab (Python101)
[Link]

[Link] 29
Anaconda
The Most Popular
Python
Data Science Platform
Source: [Link]
30
Download Anaconda

[Link] 31
Python
HelloWorld
32
Anaconda-Navigator

Launchpad

33
Anaconda Navigator

34
Jupyter Notebook

35
Jupyter Notebook
New Python 3

36
print("hello, world")

37
Source: [Link] 38
Python
Programming
39
Foundations of Python Programming
• Python Syntax
• Python Comments
• Python Variables
• Python Data Types
• Python Numbers
• Python Casting
• Python Strings
• Python Operators
• Python Booleans
40
Python Hello World
print("Hello World")
print("Hello World")

41
Python Syntax
# comment
# comment

42
Python Syntax
Indentation
the spaces at the beginning of a code line
4 spaces
score = 80
if score >=60 :
print("Pass")

43
Python Variables

# Python Variables
x = 2
price = 2.5
word = 'Hello'

word = 'Hello'
word = "Hello"
word = '''Hello'''
44
Python Variables

x = 2
y = x + 1

45
python_version()

# comment
from platform import python_version
print("Python Version:", python_version())

Python Version: 3.10.12

46
Python Data Types

x = "Hello World" #str


x = 2 #int
x = 2.5 #float
x = 7j #complex

47
Python Data Types

x = ["apple", "banana", "cherry"] #list


x = ("apple", "banana", "cherry") #tuple
x = range(6) #range
x = {"name" : "Tom", "age" : 20} #dict
x = {"apple", "banana", "cherry"} #set
x =
frozenset({"apple", "banana", "cherry"})
#frozenset

48
Python Data Types

x = True #bool
x = b"Hello" #bytes
x = bytearray(5) #bytearray
x = memoryview(bytes(5)) #memoryview
x = None #NoneType

49
Python Casting
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
print(x, type(x))
print(y, type(y))
print(z, type(z))
3 <class 'str’>
3 <class 'int’>
3.0 <class 'float'>
50
Python Numbers
x = 2 # int
y = 3.4 # float
z = 7j #complex
print(x, type(x))
print(y, type(y))
print(z, type(z))
2 <class 'int’>
3.4 <class 'float’>
7j <class 'complex'>
51
Python Arithmetic Operators
Operator Name Example
+ Addition 7 + 2 = 9
- Subtraction 7 - 2 = 5
* Multiplication 7 * 2 = 14
/ Division 7 / 2 = 3.5
// Floor division 7 // 2 = 3 (Quotient)
% Modulus 7 % 2 = 1 (Remainder)
** Exponentiation 7 ** 2 = 49

52
Python Basic Operators

print('7 + 2 =', 7 + 2) 7 + 2 = 9
print('7 - 2 =', 7 - 2) 7 - 2 = 5
print('7 * 2 =', 7 * 2) 7 * 2 = 14
print('7 / 2 =', 7 / 2) 7 / 2 = 3.5
print('7 // 2 =', 7 // 2) 7 // 2 = 3
print('7 % 2 =', 7 % 2) 7 % 2 = 1
print('7 ** 2 =', 7 ** 2) 7 ** 2 = 49

53
Python Booleans:
True or False
# Python Booleans: True or False
print(3 > 2)
print(3 == 2)
print(3 < 2)

54
Python BMI Calculator
# BMI Calculator in Python
height_cm = 170
weight_kg = 60
height_m = height_cm/100
BMI = (weight_kg/(height_m**2))

print("Your BMI is: " + str(round(BMI,1)))

Your BMI is: 20.8


55
Future value
of a specified
principal amount,
rate of interest, and
a number of years
Source: [Link] 56
How much is your $100 worth
after 7 years?
# How much is your $100 worth after 7 years?
fv = 100 * 1.1 ** 7
print('fv = ', round(fv, 2))
# output = 194.87

fv = 194.87

57
Future Value

# Future Value
pv = 100
r = 0.1
n = 7
fv = pv * ((1 + (r)) ** n)
print(round(fv, 2))

194.87
58
Future Value

# Future Value
amount = 100
interest = 10 #10% = 0.01 * 10
years = 7

future_value = amount * ((1 + (0.01 * interest)) ** years)


print(round(future_value, 2))
194.87
59
Python
Data Structures

60
Python Data Structures
• Python Lists []
• Python Tuples ()
• Python Sets {}
• Python Dictionaries {k:v}

61
Python Data Structures
fruits = ["apple", "banana", "cherry"] #lists []
colors = ("red", "green", "blue") #tuples ()
animals = {'cat', 'dog'} #sets {}
person = {"name" : "Tom", "age" : 20} #dictionaries {}

62
Python Data Types

x = ["apple", "banana", "cherry"] #list


x = ("apple", "banana", "cherry") #tuple
x = {"name" : "Tom", "age" : 20} #dict
x = {"apple", "banana", "cherry"} #set

63
Python Collections
• There are four collection data types in the Python programming language
• List []
• a collection which is ordered and changeable. Allows duplicate members.
• Tuple ()
• a collection which is ordered and unchangeable. Allows duplicate
members.
• Set {}
• a collection which is unordered, unchangeable, and unindexed. No
duplicate members.
• Dictionary {k:v}
• a collection which is ordered and changeable. No duplicate members.
64
Python Dictionaries {k:v}
• As of Python version 3.7, dictionaries are ordered.
• In Python 3.6 and earlier, dictionaries are unordered.

65
Lists []
x = [60, 70, 80, 90]
print(len(x)) 4
print(x[0]) 60
print(x[1]) 70
print(x[-1]) 90

66
Lists []
• len(): how many items
• type(): data type
• list() constructor: creating a new list

67
Python List Methods
• Method Description
• append() Adds an element at the end of the list
• clear() Removes all the elements from the list
• copy() Returns a copy of the list
• count() Returns the number of elements with the specified value
• extend() Add the elements of a list (or any iterable), to the end of the current list
• index() Returns the index of the first element with the specified value
• insert() Adds an element at the specified position
• pop() Removes the element at the specified position
• remove() Removes the item with the specified value
• reverse() Reverses the order of the list
• sort() Sorts the list

[Link] 68
Tuples ()
A tuple in Python is a collection that cannot be modified.
A tuple is defined using parenthesis.
x = (10, 20, 30, 40, 50)
print(x[0]) 10
print(x[1]) 20
print(x[2]) 30
print(x[-1]) 50

Source: [Link] 69
Sets {}
animals = {'cat', 'dog'}
print('cat' in animals) True
print('fish' in animals) False
[Link]('fish')
print('fish' in animals) True
print(len(animals)) 3
[Link]('cat’)
print(len(animals)) 3
[Link]('cat')
print(len(animals)) 2
Source: [Link] 70
Dictionary {key : value}
Python Dictionary
Key à Value
'EN’ à 'English’
'FR’ à 'French'

k = { 'EN':'English', 'FR':'French' }
print(k['EN'])
English

71
Source: [Link]
Python Data Structures
fruits = ["apple", "banana", "cherry"] #lists []
colors = ("red", "green", "blue") #tuples ()
animals = {'cat', 'dog'} #sets {}
person = {"name" : "Tom", "age" : 20} #dictionaries {}

[Link] 72
Python
Control Logic
and
Loops
73
Python Control Logic and Loops
• Python if else
• if elif else
• Booleans: True, False
• Operators: ==, !=, >, <, >=, <=, and, or, not
• Python for Loops
• for
• Python while Loops
• While
• break
• continue
74
Python if...else
• Python if...else
• if elif else
• Booleans: True, False
• Operators: ==, !=, >, <, >=, <=, and, or, not

75
Python Conditions and If statements
• Python supports the usual logical conditions from mathematics:
• Equals: a == b
• Not Equals: a != b
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b

76
Python Comparison Operators
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Source: [Link] 77
Python Logical Operators
Operator Description Example
Returns True if both
and x < 5 and x < 10
statements are true

Returns True if one of the


or x < 5 or x < 4
statements is true

Reverse the result, returns


not not(x < 5 and x < 10)
False if the result is true
Source: [Link] 78
Python if
# Python if
score = 80
if score >=60 :
print("Pass")
[Link] 79
Python if else
# Python if else
score = 80
if score >=60 :
print("Pass")
else:
print("Fail")

[Link] 80
Python if elif else
score = 95
if score >= 90 :
print("A")
elif score >=60 :
print("Pass")
else:
print("Fail")
81
Python if elif else
# Python if elif else
score = 90
grade = ""
if score >=90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "E"
print(grade)
82
Python for Loops

for i in range(1,6):
print(i)
1
2
3
4
5
[Link] 83
Python for loops
# for loops
for i in range(1,10):
for j in range(1,10):
print(i, ' * ' , j , ' = ', i*j)

[Link] 84
Python while Loops
• while
• break
• continue

85
Python while loops

# while loops
age = 10
while age < 20:
print(age)
age = age + 1

[Link] 86
Python
Functions
and
Modules
87
Python Functions and Modules
• Python Functions
• def myfunction():
• Python Classes/Objects
• class MyClass:
• Python Modules
• [Link]
• import mymodule
88
Python
Functions

89
Python Functions
• A function is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a function.
• A function can return data as a result.
• Creating a Function
• In Python a function is defined using the def keyword:

90
Python Function def
# Python Function def
# indentation for blocks. four spaces
def getfv(pv, r, n):
fv = pv * ((1 + (r)) ** n)
return fv
fv = getfv(100, 0.1, 7)
print(round(fv, 2))
194.87
[Link] 91
Future value
of a specified
principal amount,
rate of interest, and
a number of years
Source: [Link] 92
How much is your $100 worth
after 7 years?
# How much is your $100 worth after 7 years?
fv = 100 * 1.1 ** 7
print('fv = ', round(fv, 2))
# output = 194.87

fv = 194.87

93
Future Value

# Future Value
pv = 100
r = 0.1
n = 7
fv = pv * ((1 + (r)) ** n)
print(round(fv, 2))

194.87
94
Future Value

# Future Value
amount = 100
interest = 10 #10% = 0.01 * 10
years = 7

future_value = amount * ((1 + (0.01 * interest)) ** years)


print(round(future_value, 2))
194.87
95
Python Function
def getfv() define get future value function
# Python Function def
# indentation for blocks. four spaces
def getfv(pv, r, n):
fv = pv * ((1 + (r)) ** n)
return fv
fv = getfv(100, 0.1, 7)
print(round(fv, 2))
194.87
[Link] 96
Python
Classes/Objects
class MyClass:
97
Python Classes/Objects
• Python is an object oriented programming language.
• Almost everything in Python is an object, with its properties
and methods.
• A Class is like an object constructor, or a "blueprint" for creating
objects.
• Create a Class:
• To create a class, use the keyword class:

98
Python Classes/Objects
class MyClass:
# Python class
class MyClass:
x = 5

c1 = MyClass()
print(c1.x)

[Link] 99
Python Classes/Objects
class Person:
def __init__(self, name, age):
[Link] = name
[Link] = age

p1 = Person("Alan", 20)

print([Link]) Alan
print([Link]) 20
[Link] 100
Python Classes/Objects
class Person:
def __init__(self, name, age):
[Link] = name
[Link] = age

def myfunc(self):
print("Hello my name is " + [Link])

p1 = Person("Alan", 20)
[Link]()
[Link] 101
Python Classes/Objects
class Person:
def __init__(self, name, age):
[Link] = name
[Link] = age

def myfunc(self):
print("Hello my name is " + [Link])

p1 = Person("Alan", 20)
[Link]() Hello my name is Alan
print([Link]) Alan
print([Link]) 20
[Link] 102
Python Classes and Obects
class Vehicle:
name = ""
kind = "car"
color = ""
value = 100.00
def description(self):
desc_str = "%s is a %s %s worth $%.2f." %
([Link], [Link], [Link], [Link])
return desc_str

[Link] 103
Python Classes and Objects
car1 = Vehicle() class Vehicle:
[Link] = "Fer" name = ""
[Link] = "red" kind = "car"
[Link] = "convertible" color = ""
value = 100.00
[Link] = 60000.00
def description(self):
desc_str = "%s is a %s %s
car2 = Vehicle() worth $%.2f." % ([Link], [Link],
[Link] = "Jump" [Link], [Link])
[Link] = "blue" return desc_str
[Link] = "van"
[Link] = 10000.00

print([Link]()) Fer is a red convertible worth $60000.00.


print([Link]) Fer
print([Link]()) Jump is a blue van worth $10000.00.
print([Link]) Jump

[Link] 104
Python
Modules

105
Python Modules
• Consider a module to be the same as a code library.
• A file containing a set of functions you want to include in your
application.
• Create a Module
• To create a module just save the code you want in a file with the file
extension .py:
• Use a Module
• import module
106
Python Modules
# [Link]
def greeting(name):
print("Hello, " + name)

import mymodule
[Link]("Alan")
[Link]
def greeting(name):
print("Hello, " + name)

[Link] 107
Python File Input / Output

# Python File Input / Output


with open('[Link]', 'w') as file:
[Link]('Hello World\nThis is Python File Input Output')

with open('[Link]', 'r') as file:


text = [Link]()
print(text)

Hello World This is Python File Input Output


[Link] 108
Python File Input / Output
# Python File Input / Output
filename = '[Link]'
with open(filename, 'w') as file:
text = '''def greeting(name):
print("Hello, " + name)
'''
[Link](text)

with open(filename, 'r') as file:


text = [Link]()
print(filename)
print(text)
[Link]
def greeting(name):
print("Hello, " + name)
[Link] 109
Python Modules
import mymodule
# [Link]
def greeting(name):
print("Hello, " + name)

import mymodule
[Link]("Alan")

Hello, Alan

[Link] 110
Python main() function
#Python main() function
def main():
print("Hello World!")

if __name__ == "__main__":
main()

[Link] 111
Files
and
Exception Handling
112
Files and Exception Handling
• Python Files (File Handling)
• open()
• f = open("[Link]")
• Python Try Except (Exception Handling)
• try:
except:
else:
finally:
113
File Handling
• The key function for working with files in Python is the open()
function.
• The open() function takes two parameters; filename, and mode.
• There are four different methods (modes) for opening a file:
• "r" - Read - Default value. Opens a file for reading, error if the file does not
exist
• "a" - Append - Opens a file for appending, creates the file if it does not
exist
• "w" - Write - Opens a file for writing, creates the file if it does not exist
• "x" - Create - Creates the specified file, returns an error if the file exists

114
Python Files (File Handling)
f = open("[Link]", "w")
[Link]("Hello World")
[Link]()

f = open("[Link]", "r")
text = [Link]()
print(text)
[Link]()
Hello World
[Link] 115
Python Files (File Handling)
# Python File Input / Output
with open('[Link]', 'w') as file:
[Link]('Hello World')

with open('[Link]', 'r') as file:


text = [Link]()
print(text)

Hello World
[Link] 116
Python Files
# Python File Input / Output
with open('[Link]', 'w') as file:
[Link]('Hello World\nPython File IO')

with open('[Link]', 'r') as file:


text = [Link]()
print(text)
Hello World
Python File IO
[Link] 117
Python Files
# Python File Input / Output
with open('[Link]', 'a+') as file:
[Link]('\n' + 'New line')

with open('[Link]', 'r') as file:


text = [Link]()
print(text)
Hello World
Python File IO
New line [Link] 118
Python Files
# !ls list files
!ls

[Link] sample_data

[Link] 119
Python OS, IO, files, and Google Drive
import os

cwd = [Link]()
print(cwd)

/content

[Link] 120
[Link]()
[Link](cwd)

['.config',
'[Link]',
'sample_data']

[Link] 121
[Link]()
path = [Link](cwd, 'sample_data')
print(path)
[Link](path)

/content/sample_data
['[Link]', '[Link]',
'mnist_train_small.csv',
'mnist_test.csv',
'california_housing_train.csv',
'california_housing_test.csv']
[Link] 122
from [Link] import files
from [Link] import files

with open('io_file_myday.txt', 'w') as f:


[Link]('Google Colab File Write Text some content Myday')

import time
[Link](1) # time sleep 1 second

[Link]('io_file_myday.txt')
print('downloaded')

downloaded
[Link] 123
Python Files
from [Link] import files
uploaded = [Link]()

for fn in [Link]():
print('User uploaded file "{name}"
with length {length} bytes'.format(
name=fn, length=len(uploaded[fn])))

User uploaded file "io_file_myday2.txt" with length 47 bytes

[Link] 124
[Link]()
import os
if [Link]("[Link]"):
[Link]("[Link]")
print("[Link] removed")
else:
print("The file does not exist")

[Link] removed

[Link] 125
[Link]("myfolder1")
[Link]("myfolder1")
import os
[Link]()
[Link]("myfolder1")
[Link]()
[Link]("myfolder1")
[Link]()

[Link] 126
Python Try Except
• The try block lets you test a block of code for errors.
• The except block lets you handle the error.
• The else block lets you execute code when there is
no error.
• The finally block lets you execute code, regardless
of the result of the try- and except blocks.

127
Python Try Except (Exception Handling)
try: except:
#Python try except
try:
print(x)
except:
print("Exception Error")

[Link] 128
Python try: except: finally:
#Python try except finally
try:
print("Hello")
except:
print("Exception Error")
finally:
print("Finally process")
Hello
Finally process

[Link] 129
Python try: except: else:
#Python try except else
try:
print("Hello")
except:
print("Exception Error")
else:
print("No exception")
Hello
No exception
[Link] 130
Python try: except: else: finally:
try:
print("Hello")
except:
print("Exception Error")
else:
print("No exception")
finally:
print("Finally process")
Hello
No exception
Finally process
[Link] 131
Python try: except: else: finally:
try:
price = float(input("Enter the price of the stock (e.g. 10):"))
shares = int(input("Enter the number of shares (e.g. 2):"))
total = price * shares
except Exception as e:
print("Exception error:", str(e))
else:
print("The total value of the shares is:", total)
finally:
print("Thank you.")
Enter the price of the stock (e.g. 10):10
Enter the number of shares (e.g. 2):2
The total value of the shares is: 20.0
Thank you.
[Link] 132
Python try: except: else: finally:
try:
file = open("[Link]")
[Link]("Python write file")
print("file saved")
except:
print("Exception file Error")

Exception file Error

[Link] 133
Python try: except: else: finally:
try:
file = open("[Link]")
[Link]("Python write file")
print("file saved")
except:
print("Exception file Error")
finally:
[Link]()
print("Finally process")
Exception file Error
Finally process

[Link] 134
Python try: except: else: finally:
try:
file = open("[Link]", 'w’)
[Link]("Python write file")
print("file saved")
except:
print("Exception file Error")
finally:
[Link]()
print("Finally process")
file saved
Finally process
[Link] 135
Data Analytics
and
Visualization
with Python
136
Data Analytics and Visualization
with Python
• NumPy
• Numerical Python N-dimensional array
• Pandas
• Data Analytics
• Matplotlib
• Basic Data Visualization
• Seaborn
• Advanced Visualization 137
W3Schools Python Numpy

[Link] 138
W3Schools Python Pandas
Pandas Tutorial

[Link] 139
W3Schools Python

[Link] 140
Pandas: Data Analytics and Visualization

[Link] 141
Wes McKinney (2022), "Python for Data Analysis: Data Wrangling with pandas, NumPy,
and Jupyter", 3rd Edition, O'Reilly Media.

[Link] 142
Numpy
NumPy
Base
N-dimensional array
package
143
NumPy
is the
fundamental package
for
scientific computing
with Python.
Source: [Link] 144
NumPy
NumPy
•NumPy provides a
multidimensional array object
to store homogenous or heterogeneous
data;
it also provides
optimized functions/methods to operate
on this array object.

Source: Yves Hilpisch (2014), Python for Finance: Analyze Big Financial Data, O'Reilly 145
NumPy ndarray
One-dimensional Array
NumPy

(1-D Array)
0 1 n-1

1 2 3 4 5

Two-dimensional Array
(2-D Array)
0 1 n-1
0 1 2 3 4 5
1 6 7 8 9 10
11 12 13 14 15
m-1 16 17 18 19 20
146
NumPy
NumPy

v = list(range(1, 6))
v
2 * v
import numpy as np
v = [Link](1, 6)
v
2 * v
Source: Yves Hilpisch (2014), Python for Finance: Analyze Big Financial Data, O'Reilly 147
NumPy
Base
N-dimensional
array package

148
Python Data Structures
fruits = ["apple", "banana", "cherry"] #lists []
colors = ("red", "green", "blue") #tuples ()
animals = {'cat', 'dog'} #sets {}
person = {"name" : "Tom", "age" : 20} #dictionaries {}

[Link] 149
Lists []
x = [60, 70, 80, 90]
print(len(x)) 4
print(x[0]) 60
print(x[1]) 70
print(x[-1]) 90

150
NumPy
NumPy Create Array
import numpy as np
a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])
c = a * b
c

Source: Yves Hilpisch (2014), Python for Finance: Analyze Big Financial Data, O'Reilly 151
NumPy
NumPy

Source: [Link] 152


import numpy as np
a = [Link](15).reshape(3, 5)

[Link]
[Link]
[Link]

Source: [Link] 153


Matrix

Source: [Link] 154


NumPy ndarray:
Multidimensional Array Object

155
NumPy ndarray
One-dimensional Array
(1-D Array)
0 1 n-1

1 2 3 4 5

Two-dimensional Array
(2-D Array)
0 1 n-1
0 1 2 3 4 5
1 6 7 8 9 10
11 12 13 14 15
m-1 16 17 18 19 20
156
import numpy as np
a = [Link]([1,2,3,4,5])
One-dimensional Array
(1-D Array)
0 1 n-1

1 2 3 4 5

157
a = [Link]([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20]])

Two-dimensional Array
(2-D Array)
0 1 n-1
0 1 2 3 4 5
1 6 7 8 9 10
11 12 13 14 15
m-1 16 17 18 19 20

158
import numpy as np
a = [Link]([[0, 1, 2, 3],
[10, 11, 12, 13],
[20, 21, 22, 23]])
a
0 1 2 3
10 11 12 13
20 21 22 23
159
a = [Link]
([[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23]])

0 1 2 3
10 11 12 13
20 21 22 23
160
NumPy Basics:
Arrays and Vectorized
Computation

Source: [Link] 161


NumPy Array

Source: [Link] 162


Numpy Array

Source: [Link] 163


Tensor
•3
• a rank 0 tensor; this is a scalar with shape []
• [1. ,2., 3.]
• a rank 1 tensor; this is a vector with shape [3]
• [[1., 2., 3.], [4., 5., 6.]]
• a rank 2 tensor; a matrix with shape [2, 3]
• [[[1., 2., 3.]], [[7., 8., 9.]]]
• a rank 3 tensor with shape [2, 1, 3]

[Link] 164
Scalar 80

Vector [50 60 70]

Matrix 50 60 70
55 65 75

[50 60 70] [70 80 90]


Tensor [55 65 75] [75 85 95]
165
pandas
Python Data Analysis
Library
providing high-performance, easy-to-use
data structures and data analysis tools
for the Python programming language.
Source: [Link] 166
pandas:
powerful Python data analysis toolkit
• Tabular data with
heterogeneously-typed columns,
as in an SQL table or Excel spreadsheet
• Ordered and unordered (not necessarily fixed-frequency) time
series data.
• Arbitrary matrix data (homogeneously typed or
heterogeneous) with row and column labels
• Any other form of observational / statistical data sets. The data
actually need not be labeled at all to be placed into a pandas
data structure
Source: [Link] 167
Series
DataFrame
•Primary data structures of pandas
• Series (1-dimensional)
• DataFrame (2-dimensional)
•Handle the vast majority of typical use cases in
finance, statistics, social science, and many
areas of engineering.

Source: [Link] 168


pandas DataFrame
•DataFrame provides everything that R’s
[Link] provides and much more.
•pandas is built on top of NumPy and is intended
to integrate well within a scientific computing
environment with many other 3rd party
libraries.

169
pandas
Comparison with SAS
pandas SAS
DataFrame data set
column variable
row observation
groupby BY-group
NaN .
Source: [Link] 170
Python Pandas Cheat Sheet

Source: [Link]
171
Creating [Link]
a b c
1 4 7 10
2 5 8 11
3 6 9 12

import pandas as pd
df = [Link]({"a": [4, 5, 6],
"b": [7, 8, 9],
"c": [10, 11, 12]},
index = [1, 2, 3])
Source: [Link]
172
Pandas DataFrame

type(df)

173
import numpy as np
import pandas as pd
import [Link] as plt
print('pandas imported')

s = [Link]([1,3,5,[Link],6,8])
s

dates = pd.date_range('20181001',
periods=6)
dates
Source: [Link] 174
175
df = [Link]([Link](6,4),
index=dates, columns=list('ABCD'))
df

176
df = [Link]([Link](3,5),
index=['student1','student2','student3']
, columns=list('ABCDE'))
df

177
df2 = [Link]({ 'A' : 1.,
'B' : [Link]('20181001'),
'C' : [Link](2.5,index=list(range(4)),dtype='float32'),
'D' : [Link]([3] * 4,dtype='int32'),
'E' : [Link](["test","train","test","train"]),
'F' : 'foo' })
df2

178
[Link]

179
Python Data Analysis and Visualization

180
Python
Pandas

[Link] 181
Python
matplotlib

Source: [Link] 182


Python
seaborn

Source: [Link] 183


Python
plotly

Source: [Link] 184


Python
bokeh

Source: [Link] 185


Python matplotlib

[Link] 186
Python Seaborn

[Link] 187
Python Plotly Graphing Library

[Link] 188
Python Plotly Graphing Library

[Link] 189
Python Plotly Graphing Library

[Link] 190
Python Plotly Graphing Library

[Link] 191
Python Plotly Graphing Library

[Link] 192
Python Plotly Graphing Library

[Link] 193
Python Bokeh

[Link] 194
Iris flower data set
setosa versicolor virginica

Source: [Link]
Source: [Link] 195
Iris Classfication

Source: [Link]
196
[Link]
[Link]
5.1,3.5,1.4,0.2,Iris-setosa setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
5.4,3.9,1.7,0.4,Iris-setosa
4.6,3.4,1.4,0.3,Iris-setosa
5.0,3.4,1.5,0.2,Iris-setosa
4.4,2.9,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.4,3.7,1.5,0.2,Iris-setosa virginica
4.8,3.4,1.6,0.2,Iris-setosa
4.8,3.0,1.4,0.1,Iris-setosa
4.3,3.0,1.1,0.1,Iris-setosa
5.8,4.0,1.2,0.2,Iris-setosa
5.7,4.4,1.5,0.4,Iris-setosa
5.4,3.9,1.3,0.4,Iris-setosa
5.1,3.5,1.4,0.3,Iris-setosa
5.7,3.8,1.7,0.3,Iris-setosa versicolor
5.1,3.8,1.5,0.3,Iris-setosa
5.4,3.4,1.7,0.2,Iris-setosa
5.1,3.7,1.5,0.4,Iris-setosa
4.6,3.6,1.0,0.2,Iris-setosa
5.1,3.3,1.7,0.5,Iris-setosa
4.8,3.4,1.9,0.2,Iris-setosa
5.0,3.0,1.6,0.2,Iris-setosa 197
Iris Data Visualization

Source: [Link] 198


Data Visualization in Google Colab

[Link] 199
import seaborn as sns
[Link](style="ticks", color_codes=True)
iris = sns.load_dataset("iris")
g = [Link](iris, hue="species")

Source: [Link] 200


import numpy as np
import pandas as pd
%matplotlib inline
import [Link] as plt
import seaborn as sns
from [Link] import scatter_matrix

201
url = "[Link]
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
df = pd.read_csv(url, names=names)
print([Link](10))

202
[Link](10)

203
[Link]()

204
print([Link]())
print([Link])

205
[Link]('class').size()

206
[Link]["[Link]"] = (10,8)
[Link](kind='box', subplots=True, layout=(2,2), sharex=False, sharey=False)
[Link]()

207
[Link]()
[Link]()

208
scatter_matrix(df)
[Link]()

209
[Link](df, hue="class", size=2)

210
Wes McKinney (2022), "Python for Data Analysis: Data Wrangling with pandas, NumPy,
and Jupyter", 3rd Edition, O'Reilly Media.

[Link] 211
Wes McKinney (2022), "Python for Data Analysis: Data Wrangling with pandas, NumPy,
and Jupyter", 3rd Edition, O'Reilly Media.

Source: [Link] 212


Python in Google Colab (Python101)
[Link]

[Link] 213
Python in Google Colab (Python101)
[Link]

[Link] 214
Kaggle Datasets
for
Data Science 215
Kaggle Datasets for Machine Learning

Source: [Link] 216


Kaggle Datasets for Machine Learning

Source: [Link] 217


Kaggle Datasets: Credit Card Fraud Detection

Source: [Link] 218


Kaggle Code: Credit Card Fraud Detection

Source: [Link] 219


Kaggle Code: Credit Card Fraud Detection

Source: [Link] 220


Papers with Code
State-of-the-Art (SOTA)

[Link] 221
Summary
• NumPy
• Numerical Python N-dimensional array
• Pandas
• Data Analytics
• Matplotlib
• Basic Data Visualization
• Seaborn
• Advanced Visualization
222
References
• Wes McKinney (2022), "Python for Data Analysis: Data Wrangling with pandas, NumPy, and Jupyter", 3rd Edition, O'Reilly Media.
• Aurélien Géron (2023), Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems,
3rd Edition, O’Reilly Media.
• Steven D'Ascoli (2022), Artificial Intelligence and Deep Learning with Python: Every Line of Code Explained For Readers New to AI and New to Python,
Independently published.
• Stuart Russell and Peter Norvig (2020), Artificial Intelligence: A Modern Approach, 4th Edition, Pearson.
• Denis Rothman (2024), Transformers for Natural Language Processing and Computer Vision - Third Edition: Explore Generative AI and Large Language
Models with Hugging Face, ChatGPT, GPT-4V, and DALL-E 3, 3rd ed. Edition, Packt Publishing
• Ben Auffarth (2023), Generative AI with LangChain: Build large language model (LLM) apps with Python, ChatGPT and other LLMs, Packt Publishing.
• Varun Grover, Roger HL Chiang, Ting-Peng Liang, and Dongsong Zhang (2018), "Creating Strategic Business Value from Big Data Analytics: A Research
Framework", Journal of Management Information Systems, 35, no. 2, pp. 388-423.
• Junliang Wang, Chuqiao Xu, Jie Zhang, and Ray Zhong (2022). "Big data analytics for intelligent manufacturing systems: A review." Journal of
Manufacturing Systems 62 (2022): 738-752.
• Ramesh Sharda, Dursun Delen, and Efraim Turban (2017), Business Intelligence, Analytics, and Data Science: A Managerial Perspective, 4th Edition,
Pearson
• Python Programming, [Link]
• Python, [Link]
• Python Programming Language, [Link]
• Numpy, [Link]
• Pandas, [Link]
• Skikit-learn, [Link]
• W3Schools Python, [Link]
• Learn Python, [Link]
• Google’s Python Class, [Link]
• Harvard University (2024), CS50x 2024 - Lecture 6 - Python, [Link]
• Min-Yuh Day (2024), Python 101, [Link]
223

You might also like