100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Network Science - Samenvatting + uitwerkingen colleges $11.32   Add to cart

Summary

Network Science - Samenvatting + uitwerkingen colleges

 9 views  0 purchase
  • Course
  • Institution

Alles wat je moet weten voor het vak Network Science (en Python in het algemeen). Dit bestand bestaat uit zowel een samenvatting als de meest belangrijke concepten (met uitwerkingen) die gehanteerd worden in het vak network science. Het fijne aan dit bestand is dat het uitleg geeft bij de gegeven c...

[Show more]

Preview 4 out of 36  pages

  • January 18, 2024
  • 36
  • 2023/2024
  • Summary
avatar-seller
network-science

January 18, 2024


1 Summary
[1]: %matplotlib inline
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas import DataFrame
import scipy
from nose.tools import assert_equal
from numpy.testing import assert_almost_equal, assert_array_almost_equal
import random
from collections import Counter


1.1 Python Data Types
Float: real numbers
Int: integer numbers
Str: string
Bool: True, False

1.1.1 List vs. tuple vs. set vs. dict
Lists[] are used to store multiple items in a single variable. List items are ordered, changeable and
allow duplicate values.
Sets{} are used to store multiple items in a single variable. A set is a collection which is unordered,
unchangeable* and do not allow duplicate values.
Frozenset is just an immutable version of a Python set object. While elements of a set can be
modified at any time, elements of the frozen set remain the same after creation. Due to this, frozen
sets can be used as keys in Dictionary or as elements of another set.
*Note: Set items are unchangeable, but you can remove items and add new items.
Tuples() are used to store multiple items in a single variable. A tuple is a collection which is
ordered, unchangeable and allow duplicate values



1

, Dict{} are used to store data values in key:value pairs. A dictionary is a collection which is ordered*,
changeable and do not allow duplicate values.
*Note: As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries
are unordered.

1.2 Python Objects/Classes
Almost everything in Python is an object, with its properties and methods. Objects get their
variables and functions from classes. Classes are essentially a template to create your objects. A
Class is like an object constructor, or a “blueprint” for creating objects.
[2]: class MyClass:
x = 5

y = MyClass()
print(y.x)

5

1.3 Subsetting / List slicing
[3]: names = ["Jan", "Piet", "Klaas", "Kees", "Anne"]
# index 4 is uitgesloten
print(names[2:4], names[3:])

['Klaas', 'Kees'] ['Kees', 'Anne']

1.4 For-loops
[4]: # Voeg een lijst toe van de gewichten
weights = [70, 80, 90, 75, 65]

# Maak een lege lijst voor de namen EN gewichten
name_weight_list = []

# Voeg willekeurige namen en gewichten toe aan de lijst
for i in range(5):
name = random.choice(names)
weight = random.choice(weights)
name_weight_list.append((name, weight)) # Tuples (Een paartje)

# Als list comprehensie:
# name_weight_list = [(random.choice(names), random.choice(weights)) for i in␣
↪range(5)]

print(name_weight_list)

[('Kees', 80), ('Piet', 75), ('Piet', 75), ('Anne', 70), ('Klaas', 80)]



2

, 1.5 Conditionals
1.5.1 if-statement (in loop)

[5]: # Om ervoor te zorgen dat elke naam slechts één keer voorkomt in de lijst:
chosen_names = []

name_weight_list = []

# Voeg willekeurige namen en gewichten toe aan de lijst
for i in range(5):
# Kies een naam die nog niet is gekozen
name = random.choice([name for name in names if name not in chosen_names])
# Voeg de gekozen naam toe aan de lijst van gekozen namen
chosen_names.append(name)
weight = random.choice(weights)
name_weight_list.append((name, weight))

print(name_weight_list)

[('Anne', 70), ('Kees', 90), ('Piet', 65), ('Jan', 75), ('Klaas', 65)]

1.5.2 elif-statement
[6]: number_of_apples = 3

if number_of_apples < 1:
print('You have no apples')
elif number_of_apples == 1:
print('You have one apple')
elif number_of_apples < 4:
print('You have a few apples')
else:
print('You have many apples!')

You have a few apples

1.6 Functions
A function is a piece of reusable code, aimed at solving a particular task. You can call functions
instead of having to write code yourself.
output = function_name(input)

[87]: # Handige functies:
# len()
# max()
# min()
# round()



3

, # sorted()
dieren = ['hond', 'kat', 'paard', 'vogel']
aantal_dieren = len(dieren)

getallen = [5, 2, 8, 1, 3]
gesorteerde_getallen_aflopend = sorted(getallen, reverse=True)

print(gesorteerde_getallen_aflopend, aantal_dieren)
type(aantal_dieren)

[8, 5, 3, 2, 1] 4

[87]: int


1.6.1 Methods
Methods are functions that belong to specific objects.
[8]: # Handige methodes
# .index()
# .append() end of the list
# .insert() specific place
# .remove("appel") of # del fruit[0]
fruit = ["appel", "banaan", "kiwi"]
fruit.insert(1, "peer")
fruit

[8]: ['appel', 'peer', 'banaan', 'kiwi']


1.6.2 Iterable
An iterable is any Python object capable of returning its members one at a time, permitting it to
be iterated over in a for-loop. Familiar examples of iterables include lists, dicts, and strings.

1.7 Comprehensions
1.7.1 List comprehension
List comprehension offers a shorter syntax when you want to create a new list based on the values
of an existing list.
Components: - Output expression - Iterator variable (represent members of iterable) - Iterable

[9]: # [ output expression for iterator variable in iterable if predicate expression␣
↪]



# Based on a list of fruits, you want a new list, containing only the fruits␣
↪with the letter "a" in the name. Without list comprehension you will have to␣

↪write a for statement with a conditional test inside:




4

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller koenverhoeff. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $11.32. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

62491 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$11.32
  • (0)
  Add to cart