diff options
author | Camil Staps | 2015-09-13 18:14:38 +0200 |
---|---|---|
committer | Camil Staps | 2015-09-13 18:40:07 +0200 |
commit | db2309142981324c5ff8917e9f0cc95b5529dfe5 (patch) | |
tree | 7ad88a1dbfe125ec5f131868284423d0d380c80b /Assignment 1 | |
parent | Initial commit (diff) |
Assignment 1, 1.1 & 1.2 start
Diffstat (limited to 'Assignment 1')
-rw-r--r-- | Assignment 1/Data/nanonose.xls | bin | 0 -> 37376 bytes | |||
-rw-r--r-- | Assignment 1/ex11.py | 72 | ||||
-rw-r--r-- | Assignment 1/ex12.py | 49 |
3 files changed, 121 insertions, 0 deletions
diff --git a/Assignment 1/Data/nanonose.xls b/Assignment 1/Data/nanonose.xls Binary files differnew file mode 100644 index 0000000..6d6011e --- /dev/null +++ b/Assignment 1/Data/nanonose.xls diff --git a/Assignment 1/ex11.py b/Assignment 1/ex11.py new file mode 100644 index 0000000..b3c3683 --- /dev/null +++ b/Assignment 1/ex11.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Sep 9 16:03:05 2015 + +@author: camilstaps +""" + +from __future__ import print_function +import numpy as np + +# 1.1.1 + +vector_x = np.array([6,7,8,9,10,11,12]) +vector_y = np.array([3,7,11,15,19,23,27]) +vector_w = np.array([1,1,0,0.5,1,1.5,2,0,0]) +vector_s = np.array([100,98.8,97.6,96.4,95.2]) +vector_z = np.array([0.7 + 0.3 * i for i in range(0,7)]) + +# 1.1.1 a +vector_v = np.multiply(3, vector_x) + vector_y +print("1.1.1 a:", vector_v) + +# 1.1.1 b +dot_product = np.dot(vector_x, vector_y) +print("1.1.1 b:", dot_product) + +# 1.1.1 c +vector_t = np.multiply(np.pi, vector_s + 4) +print("1.1.1 c:", vector_t) + +# 1.1.1 d +vector_z = vector_z - 1 +print("1.1.1 d:", vector_z) + +# 1.1.1 e +vector_x = np.concatenate((vector_x[:4], [4,4,4])) +print("1.1.1 e:", vector_x) + +# 1.1.1 f +vector_r = np.multiply(2, vector_w) - 5 +print("1.1.1 f:", vector_r) + +# 1.1.2 + +matrix_m = np.asmatrix([[1,2,3],[6,8,4],[6,7,5]]) +matrix_n = np.asmatrix([[4,6],[7,2],[5,1]]) +matrix_p = np.asmatrix([[2,5],[5,5]]) + +# 1.1.2 a +matrix_a = matrix_m * matrix_n + matrix_n +print("1.1.2 a:", matrix_a) + +# 1.1.2 b +matrix_b = np.dot(np.transpose(matrix_n), matrix_m) +print("1.1.2 b:", matrix_b) + +# 1.1.2 c +matrix_c = np.invert(matrix_p) + matrix_p +print("1.1.2 c:", matrix_c) + +# 1.1.2 d +#matrix_d = matrix_a * matrix_c * (matrix_c + matrix_b) +print("1.1.2 d: C and B cannot be added") + +# 1.1.2 e +# Only square matrices have eigenvalues (see for example +# http://math.stackexchange.com/q/583938/63495), so N is omitted +val_m, vec_m = np.linalg.eig(matrix_m) +val_p, vec_p = np.linalg.eig(matrix_p) +print("1.1.2 e (M):", val_m, vec_m) +print("1.1.2 e (N): non-square matrices don't have eigenv*s") +print("1.1.2 e (P):", val_p, vec_p) diff --git a/Assignment 1/ex12.py b/Assignment 1/ex12.py new file mode 100644 index 0000000..3db49e1 --- /dev/null +++ b/Assignment 1/ex12.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +""" +Created on Fri Sep 11 13:12:03 2015 + +@author: camilstaps +""" + +from __future__ import print_function +import xlrd +import numpy as np +import matplotlib.lines as pltlines +import matplotlib.patches as pltpatches +import matplotlib.pyplot as plt + +# 1.2.1 a + +xls = xlrd.open_workbook(filename='Data/nanonose.xls') +xls = xls.sheet_by_index(0) + +fst_col, fst_row = 3, 2 +data = np.asmatrix([xls.col_values(i)[fst_row:] + for i in range(fst_col,fst_col + 8)]) + +# 1.2.1 b +colors = {'Water': '#61d4fa', + 'Ethanol': '#ff3333', + 'Acetone': '#549900', + 'Heptane': '#d9910d', + 'Pentanol': '#990096'} +graph_colors = [colors[r] for r in xls.col_values(0)[fst_row:]] + +xs = xls.col_values(1)[fst_row:] + +fig = plt.figure(figsize=(12,6)) +ax = plt.gca() +ax.set_xscale('log') +ax.set_yscale('symlog') +plt.xlim([80,10 ** 5]) +plt.ylim([-1, 500]) + +ax.scatter(xs, data.tolist()[0], s=60, c=graph_colors, alpha=0.4, marker='s') +ax.scatter(xs, data.tolist()[1], s=60, c=graph_colors, alpha=0.4, marker='o') + +line_a = pltlines.Line2D([], [], ls=' ', marker='s', label='A', c='w') +line_b = pltlines.Line2D([], [], ls=' ', marker='o', label='B', c='w') +handles = [pltpatches.Patch(label=k, color=v) for k, v in colors.iteritems()] + [line_a, line_b] +ax.legend(handles=handles, numpoints=1, loc=2) + +plt.show() |