<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 12-752
Step1:  Short Introduction to Python and Jupyter
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
Step2:  Task #3 [10%]
Step3:  Plotting
Jupyer notebooks allow you to interactively explore data. Let's plot a sine-wave.
Step4:  Task #4 [10%]
Step5:  Data structures
The two most widely used data structures in Python are lists and dictionaries.
Lists
Here are some simple examples how to use lists.
If you want to learn more about Python lists, check out https
Step6:  Accessing data in list
Step7:  Dictionaries
Dictionaries are key-value pairs. We will give some short examples on how to used dictionaries. For a more thorough introduction, see https
Step8:  Loops
A couple of example on how to use loops. For more info see https
Step9:  Quick exercises
Step10:  Loading data
Since you will be dealing with data, you need to know how to read and parse data. Numpy can automatically parse some csv files. Let's assume however, that we need to parse a file that numpy cannot parse out-of-the-box.
Step11:  Numpy arrays
Numpy array is a data structure very suitable to store matrices. A list of list (like parsed_lines) can be converted to a numpy array by
Step12:  Quick exercises
Step13:  Broadcasting over axis
Step14:  Quick exercises
<ASSISTANT_TASK:>
Python Code:
%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__)
Explanation: 12-752: Data-Driven Building Energy Management
Fall 2016, Carnegie Mellon University
Assignment #1
Task 1 [0%]: Making sure everything is installed correctly
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
End of explanation
#This is a code cell
#Jupyter allows you to run code within the browser
#try running this cell
x = 5+15+1000
print(x)
Explanation: Short Introduction to Python and Jupyter
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$
Task #2 [10%]: Quick exercise
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.
$e^{ix}=\text{cos}x+ i\text{sin}x$
End of explanation
print(np.sum(np.arange(1,21)))
Explanation: Task #3 [10%]: Another quick exercise
Create a cell under this. Make sure the type of the cell is 'Code'.
Compute the sum of 1 to 20 and print it to the console. Hint: np.sum and np.arange will be your friends.
End of explanation
x = np.arange(0,2*np.pi,2*np.pi/80.0)
y = np.sin(x)
plt.plot(x,y)
Explanation: Plotting
Jupyer notebooks allow you to interactively explore data. Let's plot a sine-wave.
End of explanation
n = 2 # Number of periods
x = np.arange(0,n*2*np.pi,n*2*np.pi/(80.0*n))
y = np.sin(x)
plt.plot(x,y)
Explanation: Task #4 [10%]: Plotting exercise
Write numpy code that plots two periods of a cosine-wave in the cell below.
End of explanation
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)
Explanation: Data structures
The two most widely used data structures in Python are lists and dictionaries.
Lists
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:
End of explanation
print('Printing fourth 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:])
Explanation: Accessing data in list:
End of explanation
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)
Explanation: Dictionaries
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
End of explanation
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)
Explanation: Loops
A couple of example on how to use loops. For more info see https://www.tutorialspoint.com/python/python_for_loop.htm or Google.
End of explanation
# Task #4:
mean_of_numbers = 0.
for i in range(len(list_of_numbers)):
    number = list_of_numbers[i]
    mean_of_numbers += number
mean_of_numbers = mean_of_numbers/len(list_of_numbers)
print("The mean with the first method is:",mean_of_numbers)
# Task #5:
print([n**2 for n in list_of_numbers])
# Task #6:
print([k for k in d])
# Task #7:
print([d[k] for k in d])
Explanation: Quick exercises:
In the cell below, complete the following tasks:
* Task #4 [5%]: Using a for-loop and len(), compute the mean of $list_of_numbers$
* Task #5 [10%]: Using an in-line for-loop, create a list that contains each number squared
* Task #6 [5%]: Using an in-line for-loop, create a list containing all keys of $d$
* Task #7 [10%]:using an in-line for-loop, create a list containing all values of $d$
End of explanation
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])
plt.imshow(np.array(parsed_lines).T)
Explanation: Loading data
Since you will be dealing with data, you need to know how to read and parse data. Numpy can automatically parse some csv files. Let's assume however, that we need to parse a file that numpy cannot parse out-of-the-box.
End of explanation
data_matrix = np.array(parsed_lines)
print(data_matrix[:12,:10]) #print the 10 first columns of the 12 first rows
plt.plot(data_matrix[0,:] - data_matrix[-1,:]) #plots the difference between the first and last row
print(data_matrix.shape) #shows the dimensions of the data_matrix, 200 rows, 80 columns
Explanation: Numpy arrays
Numpy array is a data structure very suitable to store matrices. A list of list (like parsed_lines) can be converted to a numpy array by:
End of explanation
# Task #8
plt.figure()
plt.plot(data_matrix[25,:])
# Task #9
plt.figure()
plt.plot(data_matrix[:,25])
Explanation: Quick exercises:
Task #8 [10%]: Plot the 25th row of data_matrix in the cell below. (The rows are very similar to each other)
Task #9 [10%]: Plot the 25th column of data_matrix in the cell below. Note: the columns don't have as much structure as the rows so don't expect a pretty plot)
End of explanation
plt.plot(np.mean(data_matrix, axis=0)) #mean row
plt.plot(np.mean(data_matrix, axis=1)) #mean column
Explanation: Broadcasting over axis
End of explanation
# Task #10
plt.figure()
plt.plot(np.max(data_matrix,axis=1))
# Task #11
plt.figure()
plt.plot(np.min(data_matrix,axis=0))
Explanation: Quick exercises:
Task #10 [10%]: Plot the maximum value (np.max) over columns
Task #11 [10%]: Plot the minimum value (np.min) over rows
End of explanation
<END_TASK>