DSci-Lecture 02-w2-20240929-type of data - python
DSci-Lecture 02-w2-20240929-type of data - python
Lecture 2
Dr. Mohammed Marey
1
Agenda
Type of data
Python review 1
Python review 2
2
Types of data
• Categorical data
• Measurement data
3
Categorical Data
4
Examples: Categorical Data
• Hair color
– blonde, brown, red, black, etc.
• Opinion of students about riots
– ticked off, neutral, happy
• Smoking status
– smoker, non-smoker
5
Categorical
data
classified as
Nominal,
Ordinal,
Categorical data
and/or
Binary
Nominal Ordinal
data data
6
Nominal Data
7
Examples: Nominal Data
• Hair color
– blonde, brown, red, black, etc.
• Race
– Caucasian, African-American, Asian, etc.
• Smoking status
– smoker, non-smoker
8
Ordinal Data
9
Examples: Ordinal Data
• Class
– fresh, sophomore, junior, senior, super senior
• Degree of illness
– none, mild, moderate, severe, …, going, going,
gone
• Opinion of students about riots
– ticked off, neutral, happy
10
Binary Data
11
Examples: Binary Data
• Smoking status
– smoker, non-smoker
• Attendance
– present, absent
• Class
– lower classman, upper classman
12
Measurement Data
13
Examples: Measurement Data
• Cholesterol level
• Height
• Age
• SAT score
• Number of students late for class
• Time to complete a homework assignment
14
Measurement
data classified as
Discrete or
Continuous
Measurement
data
Discrete Continuous
15
Discrete Measurement Data
Only certain values are possible (there are
gaps between the possible values).
16
Discrete data -- Gaps between possible
values
0 1 2 3 4 5 6 7
Continuous data -- Theoretically,
no gaps between possible values
0 1000
17
Examples: Discrete
Measurement Data
• SAT scores
• Number of students late for class
• Number of crimes reported to SC police
• Number of times the word number is used
18
Exa
mples:
Continuous
Measurement
• Cholesterol level Data
• Height
• Age
• Time to complete a homework assignment
19
Who Cares?
20
For example ...
21
And for example …
22
Agenda
Type of data
Python review 1
Python review 2
23
Python review 1
Introduction to Python
24
Agenda
25
IDEs
• PyCharm
• Vim
• Eclipse with PyDev
• Sublime Text
• Emacs
• Komodo Edit
• Visual Code
• Visual Studio
26
Libraries
27
Libraries Installation
1. Open CMD
2. Write ‘Conda’ to make sure that the environment installed successfully.
3. Activate the environment ‘ Activate ml’
4. Write the following commands
I. Conda install numpy
II. Conda install matplotlib
III. Conda install -c anaconda pip
IV. Pip install pandas
V. Pip install scikit-learn
28
Agenda
29
BasicSyntax
Error! 30
BasicSyntax
33
BasicSyntax
Python accepts single ('), double (") and triple (''' or """) quotes
to denote string literals.
34
BasicSyntax
T = “Python”
T[-4] = ??
35
BasicSyntax
String Formatting
age = 15
myStr = “Hello, I am {age} years old”.format(age=age)
print(myStr)
36
BasicSyntax
Common String Operators
Assume string variable a holds 'Hello' and variable b holds 'Python’
[ :] Range Slice - Gives the characters from the given range a[1:4] ell
in Membership - Returns true if a character exists in the givenstring ‘H’ in a True
37
BasicSyntax
Common String Methods
Method Description
str.count(sub, beg= Counts how many times sub occurs in string or in a substring of string if
0,end=len(str)) starting index beg and ending index end are given.
str.isalpha() Returns True if string has at least 1 character and all charactersare
alphanumeric and False otherwise.
str.isdigit() Returns True if string contains only digits and Falseotherwise.
str.lower() Converts all uppercase letters in string to lowercase.
str.upper() Converts lowercase letters in string to uppercase.
str.replace(old, new) Replaces all occurrences of old in string with new.
In Python, True and False are Boolean objects of class 'bool' and
they are immutable.
Python assumes any non-zero and non-null values as True,
otherwise it is False value.
Python does not provide switch or case statements as in other
languages.
Syntax: if..elif..else statement
if statement if..else statement
39
Basic Syntax (Conditions)
Example:
40
Basic Syntax (Conditions)
41
Basic Syntax (Loops)
for i in range(5):
print (i)
42
Basic Syntax (Loops)
43
Basic Syntax (Loops)
44
Basic Syntax (Loops)
Loop ControlStatements
break:terminates the loop statement and transfers execution to
the statement immediately following the loop.
45
Basic Syntax (Loops)
Loop ControlStatements
46
Agenda
47
Lists
49
Lists
50
Lists
Function Description
cmp(list1, list2) Compares elements of both lists.
len(list) Gives the total length of the list.
max(list) Returns item from the list with max value.
min(list) Returns item from the list with min value.
list(tuple) Converts a tuple into list.
51
Lists
List Comprehensions
Each list comprehension consists of an expression
followed by a for clause.
List comprehension
53
Tuples
Python Tuples are Immutable objects that cannot be changed once they have been
created.
A tuple contains items separated by commas and enclosed in parentheses instead of
square brackets.
access
Noupdate
You can update an existing tuple by (re)assigning a variable to another tuple.
Tuples are faster than lists and protect your data against accidental changes to thesedata.
The rules for tuple indices are the same as for lists and they have the same operations,
functions as well.
Towrite a tuple containing a single value, you have to include a comma, even though there
is only one value. e.g. t = (3, )
54
Dictionary
55
Dictionary
Example:
ages_dict = {“Ramy”:1992, “Mona”:1996, “Ahmed”:2001}
56
Dictionary
This example shows how to access, update and delete dictionaryelements:
57
Dictionary
The output:
58
Dictionary
59
Agenda
60
Functions
A function is a block of organized, reusable code that is used to perform a single, related action.
Functions provide better modularity for your application and a high degree of code reusing.
Defining a Function
• Function blocks begin with the keyword def followed by the function name and parentheses (
( ) ).
• Any input parameters or arguments should be placed within these parentheses. You can also
define parameters inside these parentheses.
• The first statement of a function can be an optional statement - the documentation string of
the function or docstring.
• The code block within every function starts witha colon (:) and is indented.
• The statement return [expression] exits a function, optionally passing back an expression to
the caller. A return statement with no arguments is the same as returnNone.
61
Functions
Function Syntax
62
Functions
Function Arguments
You can call a function by using any of the
following types of arguments:
• Required arguments: the arguments passed to
the function in correct positional order.
• Keyword arguments: the function call identifies
the arguments by the
parameter names.
• Default arguments: the argument has a default
value in the function declaration used when the
value is not provided in the function call. 63
Functions
• Variable-length arguments: This used when you need to process unspecified additional
arguments. An asterisk (*) is placed before the variable name in the function declaration.
64
Agenda
65
Python File Handling
66
File Handling
File Reading
file_path = "filename.txt"
67
File Handling
File Reading
file_path = "filename.txt“
68
Agenda
69
Python Classes
class Box:
def __init__(self, x1, x2, y1, y2, label):
self.x1 = x1
self.x2 = x2
self.y1 = y1
self.y2 = y2
self.label = label
def calc_area(self):
return abs(self.x2-self.x1)*abs(self.y2-
self.y1)
Classvariable
Classconstructor
71
Python Classes
Data Hiding You need to name attributes with a double underscore prefix, and those
attributes then are not be directly visible to outsiders.
72
ClassInheritance
73
Python Reserved Words
A keyword is one that means something to the language. In other words, you can’t use a
reserved word as the name of a variable, a function, a class, or a module.All the Python
keywords contain lowercase letters only.
Type of data
Python review 1
Python review 2
75
Python review 2
76
Installing Python 3.X
77
Install Python
Run the exe for install Python then click on Install Now. When it finishes, you can
see a screen that says the Setup was successful. click on "Close".
78
Install PyCharm
Run the exe for install PyCharm. The setup wizard should have started. Click “Ne
79
How to create a new project?
80
Sample Run
81
How to install a Package
82
How to install a package using Pycharm
Wizard
Choose your Python Version
83
How to install Package using CMD
1) Run CMD and redirect to python path
2) Type CD Scripts
3) Type pip install PackageName
84
A Code Sample
85
Enough to
understand the
code
• Assignment uses = and comparison uses ==
• For numbers + - / % are as expected.
Special use of + for string concatenation.
Special use of % for string formatting (as with printf in C).
• Logical operators are words (and, or, not).
• The basic printing command is print(parameter).
• The first assignment to a variable creates it.
Variable types don’t need to be declared.
Python figures out the variables types on its own.
86
Basic Data-Types
• Integers
Z=5
• Floats
X = 3.456
• Strings
Can use “” or ‘’ to specify.
“abc” or ‘abc’ (Same thing).
87
Whitespace - Indentation
88
Whitespace - Indentation
#Works Fine
if 5 > 2:
print("Five is greater than two!")
89
Comments
90
Assignment Operator
91
Example
92
Example
x = 3
y = x
y = 4
print (x) # prints 3, as X is not
affected
93
Casting
• int()
constructs an integer number from an integer literal, a float
literal (by rounding down to the previous whole number), or a
string literal (providing the string represents a whole number)
• Example:
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
94
Casting
• float()
constructs a float number from an integer literal, a float
literal or a string literal (providing the string represents a
float or an integer)
• Example:
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2
95
Casting
• str()
constructs a string from a wide variety of data types, including strings, integer
literals and float literals
Example:
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'
96
String
• For example:
print("hello").
97
String functions
b = "Hello, World!"
print(b[2:5]) # Prints llo
print(len(b)) # Prints 13
print(b.replace("H", "J")) # Prints Jello World
print("Enter your name:")
x = input() # Gets input from user
print("Hello, " + x)
98
Operators
99
Arithmetic
operators
x to the power y
100
Assignment
operators
101
Comparison operators
102
Logical Operators
103
Identity
Operators
104
Membership
Operators
Membership operators are used to test if a sequence is presented in an
object:
105
Bitwise
Operators
106
If ... Else
a = 200
b = 33
if b > a or b >= a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
107
While Loops
i = 1
while i < 6:
print(i)
i += 1
if(i == 3):
break
else:
continue
108
For Loops
109
Python Data-Structure
There are four data structure in the Python:
• List is a collection which is changeable (Mutable).
Allows duplicate members.
• Tuple is a collection which is unchangeable (Immutable).
Allows duplicate members.
• Set is a collection which is unindexed and changeable
(Mutable).
No duplicate members.
Create a=List:
thislist ["John", "David", "Chris"]
print(thislist) #Prints ["John", "David", "Chris"]
print(thislist[1]) # Accessing an element and Printing David
thislist[1] = "Sam" # Replaces David with Sam
#Loop through list
for x in thislist:
print(x)
if "Chris" in thislist:
print("Yes, 'Chris' is in the list")
print(len(thislist)) #Prints 3
thislist.append("Smith")
thislist.insert(1, "Jack")
thislist.remove("John")
111
List Built in Functions
112
Tuple ()
thistuple[1] = "Salma"
# Error The values will remain the same:
print(thistuple)
113
Tuple Built in Functions
114
Sets {}
thisset = {"Sally", "Sama", "Sara"}
print(thisset)
#Once a set is created, you cannot change its items, but you can
add new items.
#To add more than one item to a set use the update() method.
thisset.add("Salma")
115
Sets Built in Functions
116
Dictionary
Dictionaries are written with curly brackets, and they have keys and values.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
118
Functions
Example of function without parameters:
def my_function():
print("Hello from a function")
my_function()
my_function("Emil")
119
Functions
If we call the function without parameter, it uses the default
value:
def my_function_1(country = "Norway"):
print("I am from " + country)
my_function_1("Sweden")
print(my_function_2(3))
120
Yield
To understand what yield does, you must understand what
generators are. And before generators come iterables.
Iterables: When you create a list, you can read its items one
by one, and it’s called iteration.
mylist = [1, 2, 3]
for i in mylist:
print(i)
1
2
3
121
Yield
• Everything you can use “for… in…” is an iterable: lists, strings,…
and all the values are stored in memory and it’s not always what
you want when you have a lot of values.
122
Yield
• Generators are iterators, but you can only iterate over
them once.
• It’s because they do not store all the values in memory, they
generate the values on the fly.
• It is just the same except you used () instead of []. BUT, you can not
perform for i in mygenerator a second time since generators
can only be used once: they calculate 0, then forget about it and
calculate 1, and end calculating 4, one by one.
mygenerator = (x*x for x in range(3))
for i in mygenerator:
print(i)
0
1
4
123
Yield
Yield is a keyword that is used like return, except the function will return
a generator.
def createGenerator():
mylist = range(3)
for i in mylist:
yield i*i
mygenerator = createGenerator() # create a generator
for i in mygenerator:
print(i)
124
Classes/Objects
125
The __init__() Function
• All classes have a function called __init__(), which is always
executed when the class is being initiated.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
“self” parameter
def myfunc(self): is not passed
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()
126
Self
127
Try - Except
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")
128
Hands On
Write a program to compute:
f(n)=f(n-1)+100 when n>0
and f(0)=0
with a given n input by console (n>0).
Example:
If the following n is given as input to the program:
5
Then, the output of the program should be:
500
129
Solution
def func(n):
if n == 0:
return 0
else:
return func(n - 1) + 100
n = int(input())
print(func(n))
130
Hands On
Write a class named Circle constructed by a radius and
two methods which will compute the area and the perimeter
of a circle.
Example:
Circle Radius: 8
Circle Area: 200.96
Circle Perimeter: 50.24
131
Solution
class Circle():
def __init__(self, r):
self.radius = r
def area(self):
return (self.radius ** 2) * 3.14
def perimeter(self):
return 2 * self.radius * 3.14
NewCircle = Circle(8)
print(NewCircle.area())
print(NewCircle.perimeter())
132
Hands on
Solution 2:
num = [[1,2,3], [4,5,6], [10,11,12],
[7,8,9]]
l = []
maxSum = 0
for x in num:
sum = 0
for n in x:
sum+=n
if(sum > maxSum):
maxSum=sum
l = x
print(l)
134