Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 44

Network Analysis (Lab 1.

1)
(MA214-5/7)
Dr Joe Bailey
jbailef@essex.ac.uk
Dr Andrew Harrison
harry@essex.ac.uk
Software installation
Python
• windows https://www.python.org/downloads/windows/
• macOS https://www.python.org/downloads/
Software installation
Python IDEs: Spyder, PyCharm,
Jupyter Notebook etc.
Software installation
R: https://www.r-project.org
Download: https://www.stats.bris.ac.uk/R/ or https://cran.ma.imperial.ac.uk
Software installation
R IDE:
Rstudio
https://www.rstudio.com/products/rstudio/download/
R and Python are installed but you don’t want to install so many IDEs
separately.
--- You can install Anaconda instead of searching and installing those
IDEs
Software installation Important note: If you use
Anaconda Anaconda, please make sure your
installed python match the required
Download and installation: https://www.anaconda.com/products/individual
version.
Software Installation
Get started with Python
In this module we will mainly use Jupyter Notebook for demonstration.

• Open Anaconda
• Click the “launch”
button under
“Jupyter
Notebook”.
Get started with Python
Build a file for coding.
Files go to the
folder you want to
use  “New” 
Python 3

Then, a new file


named as
“Untitled.ipynb” is
built.
Get started with Python
Rename your “.ipynb” file

Click your new file


“Untitled.ipynb”.
Then, “File”
“Rename”

Type the new


name e.g.,
“MA214-lab1-p1”
Python basic: basic data types
A list of Python built-in data types:

See https://www.w3schools.com/python/python_datatypes.asp
Python basics: basic data types
Numbers:
Operators: +, -, *, / e.g., 3+4, 3-4, 3*4, 3/4
Division: //, % e.g., 17//3, 17%3
Power operator: ** e.g., 5**2, 3**7
Assign a value to a variable: e.g.,
Price=2.5
Number=22
Total_cost=Price*Number
Python basics: basic data types
Remark that one “cell” in Jupter notebook gives only one output, enable multiple outputs
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'
Python basics: basic data types
Strings
You can use both single quote ‘’ or double quote “”

e.g., ‘MA214 network analysis’, “MA214 network analysis”


You will get the same output:
Python basics: basic data types
How to type “I’m”, “isn’t” etc.?
Use ‘\’’ or “ ’ ”

print() function --- more often used


Numbers and strings can put together to print. It is usually used
for presenting the output.
Python basics: basic data types
List:
A list of items between square brackets.
• The simplest usage of list is similar to the
vector in mathematics with the starting
index of the element to be 0.
e.g., a=[1,2,5,9]
• Negative index is allowed to access an
element from a list: e.g., a[-1], a[-3:]
• Slice operator “:” returns a list
(list[start:stop:step]): e.g., a[1:3:1],
a[0:3:1] , a[:]
Python basics: basic data types
• List support operator “+”: e.g., a=[1,2,5,9],
a+[3,6]

• Replace an item of a list: e.g., a=[1,2,5,9],


a[1]=3, a

• Remove an item from a list using


“del”: e.g., a=[1,2,5,9], del a[1], a;
a=[1,2,5,9], del a[1:3], a
Python basics: basic data types
• Get the length of the list using “len()”:e.g., a=[1,2,5,9], len(a)

• Nested list: e.g., a=['a', 'b', 'c’], x=[1,3,7], y=[a,x], y, y[0], y[1], y[0][1], y[1][2]

More?
list.append(x), list.remove(x), list.clear(), etc.
See
https://docs.python.org/3/tutorial/datastructures.html
Python basics: basic data types
Tuple
A tuple consists of a number of values separated by commas:
e.g., a=1,2,'MA214’, nested tuple: e.g., b=a,(3,4,5)

Tuple does not support item assignment…


Python basics: basic data types
Dictionary: It stores data using keys. It is indexed by “keys”, which are of
immutable data types such as tuples, numbers, strings. “list” can not be
used as keys, because it is changeable.

e.g., define a dictionary, print the dictionary, and print using the key
Python basics: basic data types
Set
A set of objects
e.g., basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket)

Verify the membership:


'pear' in basket
'mongo' in basket

Create an empty set: set()


Python basics: while, for, if statement
While
Do something until the condition breaks
Python basics: while, for, if statement
For
Iterate over the items or indices

Iterate over items Iterate over indices of a sequence


Python basics: while, for, if statement
If
Take different actions when different conditions hold.
If…elif (it stands for ‘else if’) … else
Python basics: function
Define a function using def function_name(variable):
A function “myfun” with A function “myfun” with Using return to give
one single variable multiple variables the function output
Python basics: installing a package
It is almost unavoidable to use instant packages for programming.
• Check whether you can run Python in from command line

• Check whether you can run pip in from command line

• Install a package using pip (in command line)

$ pip install networkx


Any difficulties? Please visit https://packaging.python.org/en/latest/tutorials/installing-packages/
Python basics: installing a package
• Import an installed package

• Import a sub-module from a package

from package_name import module_name


R Basics
Now open Rstudio!
R basic: basic data types
A list of basic R data types and frequently used R objects

See https://www.w3schools.com/r/r_variables_global.asp
and https://www.tutorialspoint.com/r/r_data_types.htm
R basic: basic data types
Numbers:
Operators: +, -, *, / e.g., 3+4, 3-4, 3*4, 3/4
Power operator: ^ e.g., 5^2, 3^7
Assign a value to a variable: e.g.,
Price=2.5, more often we use Price<-2.5
R basic: basic data types
Character:
It is quite similar to strings in python
Quoted using single quote ‘’, or double quote “”
R basic: basic data types
Vectors:
Can have different modes such as numeric, character, complex or logical
a<-c(1,3,4,7,8)
b<-c(“apple”, “pear”, “banana”)
a[1]; a[5]

Note that:
• “a[-1]” is remove the first element from a vector
• Different from Python, the index begins from 1
instead of 0.
R basic: basic data types
Get the length of a vector: length(vector_name)
Using print() to print results: print()

Generate some simple vectors:


R basic: basic data types
List: The length of each item is quite flexible
a<-list(num=c(1,2,3), char=c("apple", "pear"))
a
a$num
a$char

Get the length of the list:


length(list_name)

Use a[[1]][1] instead of


a[1][1]
R basic: basic data types
Matrix
matrix(c(1,2,3,4,5,6), nrow=3, ncol=2)
matrix(c(“apple”, “banana”, ”orange”, ”pear”), nrow=2, ncol=2)
R basic: basic data types
Array:
Matrix is a two-dimensional data, an array can be in any dimensions.
R basic: basic data types
Factor:
Created using vectors but contain different levels.
R basic: basic data types
Data frame: something similar to a table with data and row and column
names (each item will appear as the same length in data frame)
a <- 1:4; M <- c(10, 35, 1, 30); b<-data.frame(a, M)
R basic: For, while loops and if statement
While

while(condition){
statement
}
R basic: For, while loops and if statement
For Iterate over items

for (val in sequence){


statement
}

Iterate over indices


Note that you need to use
“nchar()” to compute the
length of a string.
R basic: For, while loops and if statement
If, else if, else

If (condition 1){
statement 1
} else if (condition 2) {
statement 2
} else{
statement 3
}
R basic: Function
Define a function:
Note that:
Myfun<-function(val1, val2, …, valn){ For multiple return, “a” should be a
write the function here list which lists the returned variables.
return (a)
}
R basic: Install and use a package
Install a package: Windows and Mac are the same
install.packages(“package_name”)
Use an installed package:

library(package_name)

You might also like