PythonTraining MD Saiful Azad UMP
PythonTraining MD Saiful Azad UMP
Contact Details
Cell phone: +601124153527
Email: [email protected], [email protected]
Website: https://sites.google.com/view/saifulazad (new website)
http://saifulazad.weebly.com (old website)
Skype: sazad_m684
Introduction to Python
Demand on 20191
https://hackernoon.com/how-big-the-demand-for-python-in-2019-is-or-why-python-has-suddenly-become-so-popular-0va3n7m
1
https://data-flair.training/blogs/why-is-python-in-demand/
2
Why Python???
The unreasonable effectiveness of Deep Learning (CNNs)
Performance of deep learning systems over time:
Human performance
5.1% error
2015
x = [1, 2, 3]
y = [4, 5, 6]
z = x + y # z is [1,2,3,4,5,6]; x is unchanged.
• List unpacking (multiple assignment)
x, y = [1, 2] # x is 1 and y is 2
[x, y] = 1, 2 # same as above
x, y = [1, 2] # same as above
x, y = 1, 2 # same as above
_, y = [1, 2] # y is 2, didn't care about the first element
List - 3
• Modify content of list
x = [0, 1, 2, 3, 4, 5, 6, 7, 8]
x[2] = x[2] * 2 # x is [0, 1, 4, 3, 4, 5, 6, 7, 8]
x[-1] = 0 # x is [0, 1, 4, 3, 4, 5, 6, 7, 0]
x[3:5] = x[3:5] * 3 # x is [0, 1, 4, 9, 12, 5, 6, 7, 0]
x[5:6] = [] # x is [0, 1, 4, 9, 12, 7, 0]
del x[:2] # x is [4, 9, 12, 7, 0]
del x[:] # x is []
del x # referencing to x hereafter is a NameError
• Strings can also be sliced. But they cannot modified (they are immutable)
s = 'abcdefg'
a = s[0] # 'a'
x = s[:2] # 'ab'
y = s[-3:] # 'efg'
s[:2] = 'AB' # this will cause an error
s = 'AB' + s[2:] # str is now ABcdefg
The range() function
for i in range(5):
print (i) # will print 0, 1, 2, 3, 4 (in separate lines)
for i in range(2, 5):
print (i) # will print 2, 3, 4
for i in range(0, 10, 2):
print (i) # will print 0, 2, 4, 6, 8
for i in range(10, 2, -2):
print (i) # will print 10, 8, 6, 4
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
... print(i, a[i])
...
0 Mary
1 had
2 a
3 little
4 lamb
Control flow - 1
• if-else
if 1 > 2:
message = "if only 1 were greater than two..."
elif 1 > 3:
message = "elif stands for 'else if'"
else:
message = "when all else fails use else (if you want
to)"
print (message)
parity = "even" if x % 2 == 0 else "odd"
Truthiness
• any any(a)
Out[135]: True
• all
all(a)
Out[136]: False
Comparison
Operatio a = [0, 1, 2, 3, 4]
Meaning
n
b = a
< strictly less than c = a[:]
Bitwise operators: & (AND), | (OR), ^ (XOR), ~(NOT), << (Left Shift), >>
(Right Shift)
Control flow - 2
• loops
x = 0
while x < 10:
print (x, "is less than 10“)
x += 1
subtract(10, 5) # returns 5
subtract(0, 5) # returns -5
subtract(b = 5) # same as above
subtract(b = 5, a = 0) # same as above
Sorting list
# add a title
plt.title("Nominal GDP")
Line graph.
• Good for showing trend.
• Type plt.plot? to see more options, such as
different marker and line styles, colors, etc.
import matplotlib.pyplot as plt
xs = range(len(movies)) # xs is range(5)
# plot bars with left x-coordinates [xs],
# heights [num_oscars]
plt.bar(xs, num_oscars)
# label x-axis with movie names at bar centers
plt.xticks(xs, movies)
# alternatively, use the following to replace
# the two lines above
#plt.bar(xs, num_oscars, tick_label=movies)
• import pandas as pd
• import numpy as np
• df =
pd.DataFrame(np.random.ra
nd(10, 5), columns=['A', 'B',
'C', 'D', 'E'])
• df.plot.box(grid='True')
Download a dataset
• download_url =
"https://raw.githubusercontent.com/fivethirtyeight/data/master/nba-
elo/nbaallelo.csv"
• target_csv_path = "nba_all_elo.csv"
• response = requests.get(download_url)
• response.raise_for_status() # Check that the request was
successful
• with open(target_csv_path, "wb") as f:
• f.write(response.content)
• print("Download ready.")
Dataset
• When you execute the script, it will save the file nba_all_elo.csv in
your current working directory.
• Now you can use the Pandas Python library to take a look at your
data:
• import pandas as pd
• nba = pd.read_csv("nba_all_elo.csv")
• type(nba)
• Here, you follow the convention of importing Pandas in Python
with the pd alias. Then, you use .read_csv() to read in your
dataset and store it as a DataFrame object in the variable nba.
Dataset
• You can see how much data nba contains:
• len(nba)
• nba.shape
• Now you know that there are 126,314 rows and 23 columns in
your dataset. But how can you be sure the dataset really contains
basketball stats? You can have a look at the first five rows
with .head():
• nba.head()
• nba.tail()
Dataset: Displaying Data Types
• The first step in getting to know your data is to discover the
different data types it contains. While you can put anything into a
list, the columns of a DataFrame contain values of a specific data
type. You can display all columns and their data types with .info():
• nba.info()
• Showing Basics Statistics
• Now that you’ve seen what data types are in your dataset, it’s time
to get an overview of the values each column contains. You can
do this with .describe():
• nba.describe()
Displaying Data Types
• Exploratory data analysis can help you answer questions about
your dataset. For example, you can examine how often specific
values occur in a column:
• nba["team_id"].value_counts()
• nba["fran_id"].value_counts()
• It seems that a team named "Lakers" played 6024 games, but
only 5078 of those were played by the Los Angeles Lakers. Find
out who the other "Lakers" team is:
• nba.loc[nba["fran_id"] == "Lakers", "team_id"].value_counts()
• nba.loc[nba["team_id"] == "MNL", "date_game"].min()
• nba.loc[nba["team_id"] == "MNL", "date_game"].max()
• nba.loc[nba["team_id"] == "MNL", "date_game"].agg(("min",
"max"))