Python for machine learning – Collections

Welcome to another post about python for machine learning. In this post we will see Collections in python. There are four collections in python

  1. List
  2. Tuple
  3. Set
  4. Dictionary

Let us discuss about them one by one

List

A list in python is a collection which is ordered and changeable. A basic list is shown below. Lists are defined by square brackets ” [ ] “

thislist = ["apple", "banana", "cherry"]
print(thislist)

The output of the program is

['apple', 'banana', 'cherry']

It is interesting to know how to retrieve the data in python

index and range of indexes

see how to access the element using index

thislist = ["apple", "banana", "cherry"]
print(thislist[1])

The output of the above program will be banana. list is indexed from 0

let us see range of indexes by using the same program

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])

The output of the above program will be [‘cherry’, ‘orange’, ‘kiwi’]

Since we are only covering some important topics in the post,You refer other websites for learning python completely. I am just focusing on the aspects that are needed for machine learning

Tuple

The second collection we are going to see is tuple. The major difference between list and tuple is that list is changeable and tuple is unchangeable but both are ordered.

In python,Tuples are defined by Curve brackets ” ( ) ” as shown below

thistuple = ("apple", "banana", "cherry")
print(thistuple)

The output of the program is same as the list

(‘apple’, ‘banana’, ‘cherry’)

We can use the same method as in list to access the elements in tuple.

Sets

Set is a collection in python which is unordered and unindexed. sets are defined by curly brackets ” { } “

Let us see a basic set program

thisset = {"apple", "banana", "cherry"}
print(thisset) 

The output is the same as the tuple and list. Since sets are unindexed we cannot access the elements by index. So, we need to loop through the set to retrieve the elements. We will see a program to access the data in the set.

thisset = {"apple", "banana", "cherry"}

for x in thisset:
  print(x) 

The output is

cherry
apple
banana

Dictionary

A dictionary is a unordered and indexed and changeable. Dictionary is written in python by same curly brackets ” { } ” but has keys and values.

We will see a basic program about dictionary

thisdict =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict)

The output of the program is

{‘brand’: ‘Ford’, ‘model’: ‘Mustang’, ‘year’: 1964}

Now we shall see how to access data from dictionary

we can access the data in a dictionary using the associated key. The program below will demonstrate how to access the data.

x = thisdict["model"]

The output of the above program will be “Mustang”. How to change the data in a dictionary is as shown below:

thisdict["year"] = 2018

now the data in the dictionary will be changed and year value is changed to 2018.

We are not focusing on this much because we will be using numPy array and Pandas Dataframe for most of our tasks. So we won’t be using python list, tuple, sets, dictionaries. Since it is important to know these basics I’ve posted it.

In the next post we will see about numPy and what we can do with numPy.

Happy Coding

Python வழி machine learning தமிழில்: Variables and Datatypes

நாம் இந்த post இல் python இல் உள்ள variables and datatypes பற்றி பார்ப் போம். 

பொருளடக்கம் 

1. Variables & Datatypes 

2. Basic program

Variables & Datatypes 

ஒரு programming language இல் ஒரு program ஐ உருவாக்குவதற்கு  உதவுவது Variables ஆகும். Variables, data வை memory இல் வைத்துக்கொள்ளும். Python இல் variables மற்ற programming language களை விட வித்தியாசமாக இருக்கும்.  எப்படி என்றால்  python இல் variable declaration செய்யும் பொழுது datatype ஐ குறிப்பிட வேண்டிய அவசியமில்லை.  உதாரணத்திற்கு c ++ இல் variable declare செய்யும்போது கீழே உள்ளது போல declare செய்வோம்.

int a = 10;

ஆனால் python இல்

a = 10

என்று declare செய்வோம். மற்றொரு வித்தியாசம் என்னவென்று பார்த்தால் நாம் python program இல் “;” use செய்ய மாட்டோம்.

நாம் மேலும் சில உதாரணங்களை கீழே பார்ப்போம்.

x = 5         # assign variable x the value 5
y = x + 10     # assign variable y the value of x plus 10
z = y         # assign variable z the value of y

மற்ற programming language களில் உள்ளது போல python variables case sensitive ஆகும். மற்றும் variables களில் எழுத்துகள், எண்கள் மற்றும் ( _ ) கலந்து இருக்கலாம். ஆனால் variable கள் எண்களில் தொடங்கக்கூடாது.


python ஒரு dynamically typed programming மொழி ஆகும்.  அதனால் இது datatype ஐ variable இல் store ஆகும் value வை வைத்து முடிவு செய்து கொள்ளும்.


நாம் கீழே python எப்படி Datatype ஐ முடிவு செய்கிறது என்று பார்ப் போம்.

x = 1
print(type(x)) # outputs: <class 'int'>

x = 1.0
print(type(x)) # outputs: <class 'float'>

முதல் வரியில்


x=1


என்று இருக்கிறது. இது ஒரு integer value ஆகும் .அதனால் python


<class ‘int’>


என்ற output ஐ வெளியிடுகிறது.


இரண்டாவது variable ஒரு decimal value ஆகும்.  அதனால் python

<class ‘float’>

என்ற output ஐ வெளியிடுகிறது.
இந்த முறை மற்ற Data type களுக்கும் பொருந்தும்.

Basic program

நண்பர்களே, நாம் இப்பொழுது ஒரு variables and datatypes இன் basic program ஐ பார்க்கலாம்

x=10
y=x+12
print(y)
print("Data type of variable x and y" + str(type(x)) + str(type(y)))

d=1.2
print(d)
print("Data type of variable d is " + str(type(d)))

s = 'Arvin Education'
print(s)
print("Data type of variable s is " + str(type(s)))

இதன் output கீழே உள்ளது போல் இருக்கும்.

22
Data type of variable x and y<class 'int'><class 'int'>
1.2
Data type of variable d is <class 'float'>
Arvin Education
Data type of variable s is <class 'str'>

நண்பர்களே, நாம் அடுத்த பதிவில் python இல் உள்ள Control Statement பற்றி பார்ப்போம்.

நன்றி.  அடுத்த பதிவில் பார்க்கலாம்.

Python for machine learning – Control Structures

Prerequisite for this course

In my previous posts, I forgot to mention this, the prerequisite for learning python for machine learning will be a basic understanding of basic programming concepts from any programming language like C, C++, Java etc.

In this post we will see the most important part of a programming language control structure

  1. Selection
    1. if
    2. if…else
    3. if….elif…else
  2. Repetition
    1. while
    2. for

Selection

used for making decisions in a program that branches the program in 2 or more ways let us see the selection statements in python and one example program for each statement. It will have the same logic as the other programming languages like C and C++

if – statement

The first selection statement we are going to look at is if statement, the basic syntax is the same as the other programming languages. Let us see the syntax of if statement in python

if (expression):
    statements

if you see the syntax of python you will notice there is no parenthesis ” { ” instead of parenthesis python uses ” : ” and indentation if you see the syntax the statement is indented to let python know that we are inside the if statement.

Other than the simple change the logic of if statement works as the same as the other programming languages. now let us see an example program with if statement.

a = 10
if a==10:
    print('it is ten')

the output will be “it is ten

if…else – statement

The second selection statement is if…else statement if you look at the if…else statement we will see the syntax and basic program in python.

if (expression):
    statements
else:
    statements

Example program

a = 11
if a==10:
    print('it is ten')
else:
    print('it is not ten')

the output will be “it is not ten”

if…elif…else – statement

The third statement we are going to see is if…elif…else statement in python elseif is mentioned as elif that is why i am mentioning it as elif not as elseif. let us see the syntax of if…elif…else statement.

if (expression):
    statements
elif (expression):
    statements
else:
    statements

Now we will see the program.

a = 11
if a==10:
    print('it is ten')
elif a==11:
    print('it is eleven')
else:
    print('it is not ten')

the output will be “it is eleven”

Next we will see repetition statements

while – statement

The while statement functions as the same as in any other programming language we will see the syntax of while statement below.

while(expression):
    statements

We will see a example program

i = 1
while i < 6:
  print(i)
  i += 1

The above code will output ” 1 2 3 4 5 6 “.

for – statement

The next and last control statement we will see is for statement we will see the example below.

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

The output of the program is “apple banana cherry”

We will also see another basic program below.

for i in range(1,5):
    print(i)

The output will be ” 1 2 3 4″

That’s all for this post friends we will see Python collections in the next post

Happy Coding

Jupyter notebook மற்றும் Python இல் machine learning படிக்க தொடங்குவதற்கு இந்த post ஐ படிக்கவும்

Machine learning ஐ படிக்க தொடங்குவதற்கு முதலில் நாம் Jupyter notebook ஐ தெரிந்துக் கொள்ள வேண்டும். நீங்கள் இதை Arvin Education YouTube Channel வாயிலாகவும் தெரிந்து கொள்ளலாம்.

Jupyter notebook என்றால் என்ன ?

Jupyter notebook என்பது ஒரு Open source web application ஆகும். இது code, narrations மற்றும் equations எழுதுவதற்கும் மற்றும் visualizations செய்வதற்கும் பயன்படுகிறது.

இப்பொழுது Jupyter notebook ஐ எப்படி Start செய்வது என்பதை பார்க்கலாம்.

முதலில் Start button ஐ அழுத்தி Anaconda3 (64-bit) > Anaconda Navigator (Anaconda3) என்ற Option ஐ கீழே உள்ள Screenshot போல Select செய்யுங்கள்.

Anaconda Navigator Open ஆனவுடன் அதில் கீழே உள்ள Screenshot போல Launch button ஐ Click செய்யுங்கள். இப்பொழுது Jupyter notebook start ஆகி விடும்.

Jupyter notebook start ஆனவுடன் கீழே உள்ள Screenshot போல இருக்கும்.

இது Jupyter notebook இன் File explorer section.இதில் உங்களுக்கு பிடித்த Folder ஐ Select செய்து கொள்ளவும்.அதன் பிறகு New button ஐ Click செய்து python 3 என்ற option ஐ Select செய்யவும்.

இப்பொழுது ஒரு புது Notebook உருவாகிவிடும். அது கீழே உள்ள Screenshot போல இருக்கும்.

மேலே உள்ள screenshot இல் Jupyter interface பற்றி தெளிவாக ஆங்கிலத்தில் குறிப்பிடப்பட்டுள்ளது. இதில் உள்ள Shortcut களும் ஆங்கிலத்தில் குறிப்பிடப்பட்டுள்ளது. 

நாம் இப்பொழுது ஒரு Basic Python program ஐ பார்ப்போம். கீழே உள்ள Code ஐ Copy செய்து Cell இல் Paste செய்யுங்கள். 

print('hello world')

நமக்கு கீழே உள்ள Screenshot போல output கிடைக்கும்.

சரி நண்பர்களே அடுத்த post இல் variables, functions, machine learning specific packages numPy, Pandas, Sklearn, Tensorflow, Kares, பற்றி பார்போம்.

நீங்கள் இந்த post ஐ ஆங்கிலத்தில் படிக்க விரும்பினால் கீழே உள்ள Link ஐ Click செய்யவும்.

நன்றி நண்பர்களே 

இந்த Post இல் பிழை இருந்தால் மன்னிக்கவும்.

Python for machine learning: variables, datatypes

Contents

  1. Variables & Datatypes
  2. Basic program

Variables & Datatypes

Variables are basic building blocks of a programming language and it applies to python programs also. Variables hold data in memory. Python variables differ by a slight than the other programming languages. In python, variables can be declared without specifying the data type and the data type will be determined by the values assigned to the variable. for example in c++, declaring a variable “a” with the value assigned to it will be like the code below.

int a = 10;

But in python it is declared like

a = 10 #Assign the values 10 to variable a 

Note : in python “;” is not need to end the statement

Let see more examples

x = 5         # assign variable x the value 5
y = x + 10     # assign variable y the value of x plus 10
z = y         # assign variable z the value of y

As with other programming languages, variables in python is case-sensitive and can include letter, number, ( _ ) char but the variable name cannot start with a number.

Let us see how python assigns the datatype based on the values by an example program.

x = 1
print(type(x)) # outputs: <class 'int'>

x = 1.0
print(type(x)) # outputs: <class 'float'>

if we see the first line

x=1

we can see that it is a integer value and the output is displayed as

<class 'int'>

if we see the second variable declaration and ask it to print its type

x=1.0

we can see that it is a decimal number and its type is float

<class 'float'>

This applies to other data types too because python is a dynamically typed programming language and it assigns a data type to its variables based on the type of values assigned to it.

Basic Program

Let us see a basic program about declaring variables and datatypes.

x=10
y=x+12
print(y)
print("Data type of variable x and y" + str(type(x)) + str(type(y)))

d=1.2
print(d)
print("Data type of variable d is " + str(type(d)))

s = 'Arvin Education'
print(s)
print("Data type of variable s is " + str(type(s)))

The output is as follows

22
Data type of variable x and y<class 'int'><class 'int'>
1.2
Data type of variable d is <class 'float'>
Arvin Education
Data type of variable s is <class 'str'>

That is all for this post. In the next post we will see control statements in python.

Please see https://machinelearningtamil.blogspot.com/ for தமிழ் Version of this post.

Happy coding

Getting started with python and Jupyter notebook

To get started with machine learning we need to start with Jupyter notebook. You can also see this under our YouTube channel Arvin Education.

What is Jupyter notebook?

Jupyter notebook is an open-source web application which allows you to write code, use visualizations, write narrations, and write equations.

Now let us see how to work with Jupyter notebook. I will provide a screenshot for each step on how to start anaconda navigator to how to navigate Jupyter notebook and what are the features that are available in Jupyter notebook.

In windows start menu select Anaconda3 (64-bit) > Anaconda Navigator (Anaconda3) see previous posts to install Anaconda Distribution

Once you’ve clicked Anaconda Navigator you will see a windows that looks like the screenshot below

Click the launch button in the notebook section. see the screenshot above. Once you click the launch button Jupyter notebook will open in your default web browser ( Mozilla Firefox or Google Chrome).

This is the file explorer section in Jupyter. If you already have a notebook file you can browse and open it from here. The extension of the notebook is .ipynb.

Select the folder in which you want the new notebook file to be created.

In the new option, select python 3 under notebook it will open a new notebook with python 3 as the kernel.

In the screenshot above the different options are marked by red arrows and the functions of it are specified below. you can also see some useful shortcuts

Now let us write a small python program and check. Copy the below program and paste in the cell and press shift+enter to execute the current cell.

print('hello world')

you will see the result as below

In the next posts we will saw some basics of python like variables, functions, machine learning specific packages like NumPy, Pandas, Sklearn, Tensorflow, Kares, etc. This post is also available in Tamil in the below blog. Be sure to follow this blog if you want to learn machine learning in Tamil.

Blog address

https://machinelearningtamil.blogspot.com/

Essentials for learning Machine learning

To get started with machine learning we need some things.

  1. Python
  2. Pandas
  3. numPy
  4. Scikit-learn
  5. Jupyter notebooks or Jupyter Lab

You can get all these and install it manually or download Anaconda Distribution so it can set everything for you. to download anaconda click the link below

https://www.anaconda.com/distribution/

If you have any difficulty with the installation comment on the forum post below. I have setup a forum so that we can discuss all the things that are needed. To go to the forum click the link below

http://forums.arvinsoft.in/forum/viewtopic.php?f=3&t=2

Happy Learning