Python basics for machine learning!

Basic machine learning programs can be realized with a few lines of code. To do this one has to understand how one can save numbers, text to a variable and go through this array. The Python basics for machine learning consists of lists, dictionaries and how to go through theses lists or dictionairies using loops or list comprehensions. Once you installed Python you can start with this code.

a= 1
list_of_numbers = [0,1,2,3,4,5]
list_of_characters=['My', 'name', 'is', 'Lorenz']
list_characters_numbers=['I am ', 12, 'years old']

print a, b, list_of_numbers, list_of_characters

The result will be:

1 Loernz [0, 1, 2, 3, 4, 5] [‘My’, ‘name’, ‘is’, ‘Lorenz’]

At first we save a number under the variable a. Next we assign  the string “lorenz” to the variable b. Then we create a list of six numbers and call this list list_of_numbers. It is basically an array. You can also save strings in a list, which is done next under the variable name list_of_characters. Lastly we create a mixed list containing characters and numbers. At the end we print you the content of all the variables. In contrast to the programming language C, we don’t have to tell the PC if the variable saves it as a string or a number, Python does that automatically for you. List are always inside brackets. You can also reassign a new number or string to a variable.

print a
a="Now I am a string"
print a
print a

Next we will discuss so called dictionairies. Dictionairies are basically two lists with the same number of items, where one item from the one list is always connected to another item from the other list. Lets create a dictionaire where we connect the age to the corresponding names and print out of element from this dictionairy.

print ages[Julian]

In this example we connected the ages 24, 28 and 31 to different names and asked Python to print out the age of the dictionary element Julian.

Lastly we will learn how to go through lists or dictionairies. The classical way in Python would be to create a for loop. In this example we go through every item in the list list_of_numbers and print only the variables, which are bigger than 2.

for i in list_of_numbers:
    if i>2: print i

However, this is a lot of text and space and it can be realized in so called list comprehensions, which are much shorter!

for i in list_of_numbers:
 if i>2: print i
#Shorter version as list comprehension
[i for i in list_of_numbers if i>2]

These are all the Python basics you need for machine learning! If you want to know now, how to do machine learning, go to next page and check out how to find new movies based on your movie rating and other movie ratings.

Leave a Reply

Your email address will not be published. Required fields are marked *