Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP809  Data Mining and Machine Learning

Lab 1  Python Basics  Task 1

Objective of this lab is to give students an understanding about Python environment and basics of Python programming.

Introduction

1.  Python Version

To check the Python version installed on your computer open the command line and type Python:

2.  Getting started with Jupyter

Various Python IDE (Integrated Development Environment) and code editors such as PyCharm, Spyder,  Sublime Text  etc, are available. In this course you will be using an open-source web application named Jupyter notebook maintained by Project Jupyterteam. Jupyter’s core supported programming languages are Julia, Python, and R. Jupyter ships with the IPython kernel in Jupyter allows you to write your programs in Python.

Visitthis linkto install Anancoda (then launch Jupyter notebook) on your personal computer. To start it on AUT machines follow below steps:

o Run Jupyter via the shortcut Anaconda adds to your start menu. A new tab will open in your default web browser, showing your Notebook Dashboard as below. Note that the default setting provides access only to the files and sub-folders contained within Jupyter’s start-up directory. You can change the directory if required.

o Browse to the folder in which you would like to create your first notebook (e.g., I navigated to my Lab folder under my AUT’s OneDrive), click the New” drop-down button in the top-right and select Python 3”:

Your first Jupyter Notebook (.jpynb) will open in new tab:

o Switch back to the dashboard (it is already opened in your browser). You will find your new Jupyter  Notebook  Untitled.ipynb  created  under  your  dashboard.  Note  the  Running  status showing the notebook is running:

o Type print('Hello World!') in the code cell (the first cell in your notebook) and hit the Run button:

o Click on the Save Asbutton under the ‘File’ and give a proper name to your notebook file and  hit the “Save’ button:

 

Note: ‘%20’ indicates space in your folder’s name. To successfully save your file, replaced them with ‘ ‘ to match with the correct path.

o Switch back to the dashboard and note your notebook file with correct name:

3.  Python Libraries and modules:

Python uses minimum libraries hence less memory in order to fast and efficientrunning of the program. We need to install and import libraries as we work. To do so use pip installer to install libraries:

pip install pandas # panads is one of the python’s open-source libraries #for analysis and manipulation

Type the following code segments in your file and run your code to see the output.You are expected to get and understanding about syntax of python and basic concepts of python.

Getting Started with Coding

1.  Basic Arithmetic Operations

print(2 + 2)

print(50 - 5*6)

print((50 - 5*6) / 4)

print(17%3) # the % operator returns the remainder of the division print(2 ** 7) # 2 to the power of 7

2.   Assignment ofvalues to variables

width = 20

height = 5 * 9

print (width * height)

3.   String Operations

print('spam eggs') # single quotes

print(r'C:\some\name') # note the r before the quote

print(3 * 'un' + 'ium')

Strings can be indexed (subscripted), with the first character having index 0. There is noseparate character type; a character is simply a string of size one:

word = 'Python'

print(word[0]) # character in position 0

print(word[5]) # character in position 5

Indices may also be negative numbers, to start counting from the right:

print(word[-1])# Last character

print(word[-2]) # Second-last character

print(word[-6])

print('J' + word[1:])

4.   Lists

squares = [1, 4, 9, 16, 25]

print(squares)


Like strings (and all other built-insequencetypes), lists can be indexed and sliced:

print(squares[0]) # indexing returns the item

print(squares[-1])

print(squares[-3:]) # slicing returns a new list

Lists also support operations like concatenation:

print(squares + [36, 49, 64, 81, 100])

Unlike strings, which areimmutable, lists are amutabletype, i.e. it is possible to change theircontent:

cubes = [1, 8, 27, 65, 125]  # something's wrong here

4 ** 3                           # the cube of 4 is 64, not 65!

cubes[3] = 64                 # replace the wrong value

print(cubes)

You can also add new items at the end of the list, by using the append() method

cubes.append(216) # add the cube of 6

cubes.append(7 ** 3) # and the cube of 7

print(cubes)

5.   Loop in Python

Example: A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5… . The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms. This means to say the nth term is the sum of (n- 1)th and (n-2)th term. In other words, the sum of two elements defines the next.

a, b = 0, 1

while a < 10:

print(a) # Note and respect the indent

a, b = b, a+b # Note and respect the indent

Experiment with the following:

i)          Instead of the single statement (a, b = b, a+b) use two different statements and examine the output.

ii)         Explain why the result different from the original output.

iii)       Modify ii) in order to obtain the original output.


6.   If statements


7.   For statement

# Measure strings:

 

If you do need to iterate over a sequence of numbers, the built-in functionrange()comes inhandy. It generates arithmetic progressions:

 

8.   Defining Functions:

8.1. Example1: Create a function that uses while loop to print the integer numbers up to certain limit:

 

To execute the function, you can either

o call the function and hard code the limitvalue:

# Prints numbers up to 25

 o Or pass the value from keyboard:

8.2. Example2: Work with Fibonacci Sequence

Below function prints the nth Fibonacci number:

9.   To Do:

9.1. Write a function (Fib()) to get a value (n) and print a series (following below example's logic) UP TO value n.

For example:

9.2. Write a function that returns either a string ‘Found’ or ‘NotFound’ depending on whether or not the input number is located in a list of numbers of a given size (say 10).