Python for Machine Learning Pt. 2

Boolean’s Git repository: here

Refer to “intro to python-2” for this blog.

Functions

Functions in a coding language have the same meaning as they have in mathematics. Think of them like boxes, you have an input and it gives you an output. The kind of output depends on the functionality of the function.

They provide better modularity for your application and a high degree of code reusing. Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions.

Defining a function:

def function_name( parameters ):  “function_docstring”  function_suite  return [expression]

For more examples and information refer to “intro to python-2” in the Boolean git repository.

Lists

Very similar to arrays, the list is the most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets. Important thing about a list(that also seperates it from an array) is that items in a list need not be of the same type.

Real life example:

  • A list is used to hold all sorts of data. It is commonly used to hold data/indices to generate statistical graphs.
  • When calculating the cost of a function(when optimizing it) we use lists to hold all the cost values as they get appended one by one
>>> list1 = [‘physics’, ‘chemistry’, 1997, 2000];>>> list2 = [1, 2, 3, 4, 5 ];>>> list3 = [“a”, “b”, “c”, “d”]

Lists are one of the most used and basic data structures in python. They come very handy when dealing with storing and retrieving data as easily accessible.

For examples and more info(sorting, slicing, indexing etc) see “intro to python-2” here.

Dictionaries

Dictionary is a data type in python where each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}.

How are they useful? Real life examples:

  • When we use some API or download some data from the net from some website, it is usually present in a dictionary format(JSON).
  • When we make pandas data-frames, similar to excel sheets, we use dictionaries where the key is the column name and the values are the column data.
dict = {‘Name’: ‘Boolean’, ‘Age’: 1, ‘Class’: ‘First’}print(‘dict[‘Name’]: ’ , dict[‘Name’])print(‘dict[‘Age’]: ’ , dict[‘Age’])

Running the above code gives us:

dict[‘Name’]: Booleandict[‘Age’]: 1

For examples and more info see “intro to python-2” here.

Sets

A set is a collection which is unordered and not indexed. In Python sets are written with curly brackets.

Real life examples:

  • Whenever order isn’t an issue and duplicate entries have to be removed, sets are used. Example: Combining entries from two different excel sheets that have come from two different sources. It might happen that both of these sheets have some overlapping data. While combining these two sheets, to remove overlapping info and avoid duplicated entries we may use sets over the set of their rows and then combine them.
this_set = {“apple”, “cherry”, “banana”}
print(this_set)
len(this_set) #length of a set

Executing the above code we get:

{‘apple’, ‘banana’, ‘cherry’}3

For examples and more info see “intro to python-2” here.

Find the 1st and 2nd articles of the series here and here respectively

Written by Ashutosh Arya.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store