Python For Machine Learning
Python For Machine Learning
Python For Machine Learning
For
Machine Learning
2. Numpy
3. Pandas
1. Python
1. Introduction
2. Basic Syntax
3. Variable
4. Operators
5. Data Types
6. Control Flow
7. Function
8. Class
9. Module
1. Introduction
● Line Indentation is used in Python to delimit block codes. All statements within the same block
must be indented the same amount.
● The header line of compound statements, such as if, while, def, and class must be terminated
with a colon (:). The semicolon (;) is optional at the end of statement.
● In Python, the comments are followed by hash sign ”#”.
3. Variable
● A variable name starts with a letter (A to Z or a to z) or an underscore (_) followed by zero or more
letters, underscores and digits (0 to 9).
● The following are reserved words that cannot be used as constant or variable or any other identifier
names.
The operators are used to perform operations on variables and values. In Python, they are classified into
six groups:
○ Arithmetic Operators
○ Assignment Operators
○ Comparison Operators
○ Logical Operators
○ Identity Operators
○ Membership Operators
4. Operators
● Arithmetic Operators are used with numeric values to perform common mathematical operations.
+ Addition x+y
- Substraction x-y
* Multiplication x*y
% Modulus x%y
** Exponentiation x ** y
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
4. Operators
== Equal x == y
!= Not equal x != y
not Reverse the result, return False if not (x < 5 and x < 10)
the result is true
4. Operators
● Identity Operators are used to compare the objects whether they are equal.
● The variables which point to the same object, share the same reference location in memory.
● Membership Operators are used to test if an object is presented in a sequence (i.e. list, set).
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.
5. Number
● Common Functions
● Python Strings are immutable objects that cannot change their values.
● You can update an existing string by (re)assigning a variable to another string.
● Python accepts single ('), double (") quotes to denote string literals.
5. String
● Common Operators
● A list is an ordered group of items or elements whose elements do not have to be of the same type.
A list contains items separated by commas (,) and enclosed within square brackets ([]).
● List indexes start at 0 in the beginning of the list and working their way from -1 at the end. Similar
to strings, Lists operations include slicing ([] and [:]), concatenation (+), repetition (*) and
membership (in).
5. List
● Common Functions
Function Description
● Common Methods
Method Description
● A set is a collection which is unordered and unindexed, written with curly brackets ({}).
● Set cannot be accessed by referring to the index, because they are unordered, the items has no
index. However, you can loop through the set items using a for loop, or ask if a specified value is
present in a set, by using the in keyword.
5. Set
update(set) Update the set with the union of this set and another.
● 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.
● 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 these data. The
rules for tuple indices are the same as for lists and they have the same operations, functions as
well.
5. Dictionary
● Dictionaries are kind of hash table which consist of key-value pairs of unordered elements.
○ Keys : must be immutable data types, usually numbers or strings.
○ Values : can be any arbitrary Python object.
● Dictionaries are mutable objects that can change their values.
● A dictionary is enclosed by curly braces ({ }), the items are separated by commas (,), and each key is
separated from its value by a colon (:).
● Dictionary’s values can be assigned and accessed using square braces ([]) with a key to obtain its
value.
5. Dictionary
5. Dictionary
● Common Methods
Method Description
get(key, Return value with respect to the key or default if key not in
default=None) dictionary.
update(dict2) Add key-values pairs of another dictionary to this dictionary.
6. Control Flow
● In typical programs, there always been a series of statements faithfully executed by in exact top-
down order. What if you wanted to change the flow of how it works?
● As you might have guessed, this is achieved using control flow statements. There are three control
flow statements in Python - if, for and while.
● Python does not provide switch statements as in other languages (Java, PHP…).
6. If..elif..else
● If..else Statement
● If..elif..else Statement
6. Loop
● for Loop
● while Loop
6. Loop
● break: terminates the loop statement and transfers execution to the statement immediately
following the loop.
● continue: causes the loop to skip the remainder of its body and immediately retest its condition
prior to reiterating.
7. Function
● 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.
● Function Syntax
In Python, calling function is done by using any of the following types of arguments:
● Required arguments: the arguments must be given and 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.
● Data Hiding: the attributes named with a double underscore prefix (”__”) are not be directly visible to
outsiders.
9. Module
● A module is a file consisting of Python codes that can define functions, classes and variables.
● A module allows you to organize your code by grouping related code which makes the code easier
to understand and use.
● You can use any Python source file as a module by executing an import statement
● Python's from statement lets you import specific components from a module into the current
namespace.
9. Module
● import * is used to import all elements from a module into the current namespace.
2. Numpy
1. Introduction
2. Array
1. Introduction
● The most important object defined in NumPy is an N-dimensional array type called ndarray. It
describes the collection of items of the same type. Items in the collection can be accessed
using a zero-based index.
● Every item in an ndarray takes the same size of block in the memory. Each element in ndarray is
an object of data-type object (called dtype).
2. Array
● Basic Attributes
2. Array
● Selecting Data
2. Array
● Arithmetic Functions
2. Array
● Statistical Functions
2. Array
1. Introduction
2. Data Structure
3. Import/Export Data
4. Data Visualization
1. Introduction
● Basic Attributes
Attribute Description
● Basic Methods
Method Description
● Selecting Data
Method Description
● Selecting Data
Method Description
df[df[col] > 0.6] Select rows where the column col > 0.6
df[(df[col] > 0.6) & (df[col] < 0.8)] Select rows where the column 0.8 > col > 0.6
● Statistics
Method Description
● Statistics
Method Description
append(df2) Add the rows in df2 to the end of df1 (columns should be
identical)
pd.concat([df1, df2], Add the columns in df1 to the end of df2 (rows should be
axis=1) identical)
join(df2, on=col, SQL-style join the columns with the columns on df2
how='inner') where the rows for col have identical values. The 'how'
can be 'left', 'right', 'outer' or 'inner'
● You can read more on selecting, grouping, merge, reshaping… in the document.
3. Import & Export Data
● Write file
Method Description
● Data Visualization is an important task which help us to understand the nuances of the data.
● With Panda, we have a friendly a plot method, which wraps matplotlib library, to visualize the
data in the form of a histogram, line chart, pie chart, scatter chart etc.
● This following data will be used in the following parts.
4. Data Visualization
● Bar Chart is a graph that is used to present the categorical data with rectangular bars
with heights or lengths proportional to the number of values that they represent.
4. Data Visualization
● Stack Bar Chart is used to break down and compare parts of a whole. Each bar represents a whole,
and segments in the bar represent different parts or categories of that whole. Different colors are
used to illustrate the different categories in the bar.
4. Data Visualization
● Pie Chart (Circle Chart) is a circular statistical graphic, which is divided into slices to illustrate
numerical proportion. The arc length of each slice is proportional to the quantity it represents.
4. Data Visualization
● Histogram differs from a bar chart, that relates only one continue variable. It is constructed by
first defining the bin (the range of values) and then count how many values fall into each bin.
4. Data Visualization
● Line Chart is a type of graph which displays information as a series of data points called
markers connected by straight line segments. It is a basic type of chart common in many fields.
4. Data Visualization
● Scatter Plot is a type of mathematical diagram which uses Cartesian coordinates to display the
continue values for typically two variables.
4. Data Visualization
● Boxplot is a standardized way of displaying the distribution of data based on a five number
summary (“minimum”, first quartile (Q1), median (Q2), third quartile (Q3), and “maximum”).
● It can identify outliers and can tell you if the data is symmetrical, how tightly your data is
grouped, and if and how your data is skewed.
Q2= Median
Min Max
Q1 Q
3
● CatPlot visualizes the relationship between a numerical and one or more categorical variables.
4. Data Visualization - Seaborn
● CatPlot visualizes the relationship between a numerical and one or more categorical variables.
4. Data Visualization - Seaborn
● Heat Map is a data visualization technique that shows magnitude of a phenomenon as color in
two dimensions. The variation in color may be by hue or intensity, giving obvious visual cues to
the reader about how the phenomenon is clustered or varies over space.
Q&A