Please double click the code cell below and click 'Cell' -> 'Run Cells' in the drop-down bar above.
You should get an output similar to this (the version numbers should be the same):
Python version:
3.5.2 |Anaconda 4.2.0 (x86_64)| (default, Jul 2 2016, 17:52:12)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)]
Numpy version:
1.11.1
Sklearn version:
0.17.1
In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import sys
print('Python version:')
print(sys.version)
print('Numpy version:')
print(np.__version__)
import sklearn
print('Sklearn version:')
print(sklearn.__version__)
Jupyter notebooks consist of cells. This cell is a Markdown cell. Try double-clicking this cell. You can write pretty text and even Latex is supported!
A short Latex example: $\sum_i x_i = 42$
Create a new cell under this cell. Change the cell type to 'Markdown'. Make the cell display Euler's formula (https://en.wikipedia.org/wiki/Euler%27s_formula) in Latex. Do not forget to run the cell you just created.
In [ ]:
#This is a code cell
#Jupyter allows you to run code within the browser
#try running this cell
x = 5+15+1000
print(x)
In [2]:
x = np.arange(0,2*np.pi,2*np.pi/80.0)
y = np.sin(x)
plt.plot(x,y)
Out[2]:
In [ ]:
#Your code goes here
The two most widely used data structures in Python are lists and dictionaries.
Here are some simple examples how to use lists. If you want to learn more about Python lists, check out https://www.tutorialspoint.com/python/python_lists.htm
Adding data to lists:
In [3]:
l = [] #creating an empty list
print('Empty list:')
print(l)
l.append(5) #appending 5 to the end of the list
print('List containing 5:')
print(l)
l = [1,2,3,'hello','world'] #creating a list containing 5 items
print('List with items:')
print(l)
l.extend([4,5,6]) #appending elements from another list to l
print('List with more items:')
print(l)
Accessing data in list:
In [4]:
print('Printing third element in list:')
print(l[3]) #counting starts at 0
print('Printing all elements up until third element in list:')
print(l[:3])
print('Print the last 3 elements in list:')
print(l[-3:])
Dictionaries are key-value pairs. We will give some short examples on how to used dictionaries. For a more thorough introduction, see https://www.tutorialspoint.com/python/python_dictionary.htm
In [5]:
d = {} #creating empty dictionary
print('Empty dictionary:')
print(d)
d['author'] = 'Shakespeare' #adding an item to the dictionary
print('Dictionary with one element')
print(d)
#adding more items:
d['year'] = 1596
d['title'] = 'The merchant of Venice'
#Accessing items in dictionary:
print_string = d['title'] + ' was written by ' + d['author'] + ' in the year ' + str(d['year'])
print(print_string)
A couple of example on how to use loops. For more info see https://www.tutorialspoint.com/python/python_for_loop.htm or Google.
In [7]:
list_of_numbers = [1.,2.,3.,4.,5.,4.,3.,2.,1.]
incremented_list_of_numbers = []
for i in range(len(list_of_numbers)):
number = list_of_numbers[i]
incremented_list_of_numbers.append(number+1)
print('Incremented list:')
print(incremented_list_of_numbers)
#More elegantly
incremented_list_of_numbers2 = []
for number in list_of_numbers:
incremented_list_of_numbers2.append(number+1)
print('Second incremented list:')
print(incremented_list_of_numbers2)
#We can express the for-loop above also so-called in-line:
#Most elegantly
incremented_list_of_numbers3 = [number + 1 for number in list_of_numbers]
print('Third incremented list:')
print(incremented_list_of_numbers3)
#looping over dictionaries
for key in d:
value = d[key]
print(key,value)
In the cell below, complete the following tasks:
In [ ]:
# your code goes here
In [8]:
f = open('testdata.txt')
parsed_lines = []
for line in f:
l = line.split(',') #create a list by splitting the string line at every ','
l = [float(x) for x in l] #in-line for-loop that casts strings to floats
parsed_lines.append(l)
plt.plot(parsed_lines[0])
Out[8]:
In [9]:
plt.imshow(np.array(parsed_lines).T)
Out[9]:
In [10]:
data_matrix = np.array(parsed_lines)
print(data_matrix[:12,:10]) #print the 10 first columns of the 12 first rows
In [11]:
plt.plot(data_matrix[0,:] - data_matrix[-1,:]) #plots the difference between the first and last row
Out[11]:
In [34]:
print(data_matrix.shape) #shows the dimensions of the data_matrix, 200 rows, 80 columns
In [12]:
plt.plot(np.mean(data_matrix, axis=0)) #mean row
Out[12]:
In [13]:
plt.plot(np.mean(data_matrix, axis=1)) #mean column
Out[13]:
In [2]:
# your code goes here
In [ ]: