<file_sep>from jinja2 import Environment
import mycsv

header, data = mycsv.readcsv(mycsv.getdata())

XML = """<?xml version="1.0"?>
<file>
  <headers>{% for name in header %}{{name}}{% if name != header[-1] %},{% endif %}{% endfor%}</headers>
  <data>{% for list in data %}
    <record> 
      {% for item in list -%}
      <{{header[loop.index0].replace(" ", "_")}}>{{item}}</{{header[loop.index0].replace(" ", "_")}}>
      {%- endfor %}
    </record>{% endfor %}
  </data>
</file>"""


print Environment().from_string(XML).render(header=header, data=data)<file_sep>"""
Function that finds the best fit line in the sense of least squares 
to a set of data consisting of paired observations in the form (x, y).
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def leastSquares(filename):

    #read in only columns 'x' and 'y'
    fields = ['x', 'y']

    fr = pd.read_csv(filename, usecols=fields)

    col1 = np.array(fr.x, dtype=float).reshape(len(fr.x), 1)
    col2 = np.ones(len(fr.x), dtype=float).reshape(len(fr.x), 1)

    b = np.array(fr.y, dtype=float).reshape(len(fr.y), 1)
    matrixA = np.concatenate((col1, col2), axis=1)
    Atranspose = np.transpose(matrixA)

    At_A = np.dot(Atranspose, matrixA)

    At_b = np.dot(Atranspose, b)

    xhat = np.linalg.solve(At_A, At_b)

    slope = xhat.item(0)
    intercept = xhat.item(1)

    print
    print "The slope of the " + filename + " data is..."
    print slope
    print "The intercept of the " + filename + " data is..."
    print intercept

    A_xhat = np.dot(matrixA, xhat)

    minimize = A_xhat - b
    square_minimize = minimize**2
    sum_square = np.sum(square_minimize)

    print "The sum of the squares of the distances for the " + filename + " data is..."
    print sum_square

    line = slope * col1 + intercept

    plt.plot(fr.x, fr.y, '*')
    plt.plot(fr.x, line)
    plt.show()

<file_sep>from jinja2 import Environment
import mycsv

header, data = mycsv.readcsv(mycsv.getdata())

HTML = """<html>
<body>
<table>
<tr>{% for item in header %}<th>{{item}}</th>{% endfor %}</tr>{% for list in data %}
<tr>{% for item in list %}<td>{{item}}</td>{% endfor %}</tr>{% endfor %}
</table>
</body>
</html>"""


print Environment().from_string(HTML).render(header=header, data=data)<file_sep>import numpy as np

"""
Define a 10x10 matrix A whose entries are the numbers from 1 to 100 ordered consecutively going across rows
(so a_1,1 = 1, a_2,1 = 11, a_10,1 = 91, etc)
Let v = (1, 2, ...10), and b = (1, 1, ...1).
Compute Av and solve Ax = b and Ax = v using linalg.solve.
Verify your solution by computing Ax.
"""

# 10 by 10 matrix A from 1 to 100 (Note: This is a Singular Matrix!)
A = np.arange(1,101).reshape(10,10)

# 10 by 1 vector from 1 to 10
v = np.arange(1,11).reshape(10,1)

# 10 by 1 vector, all ones
b = np.ones((10,), dtype=np.int).reshape(10,1)

# checking...
# print A
# print v
# print b

# compute Av
print "Computing Av..."
print np.dot(A, v)

# solve for Ax = b
# ERROR: matrix A is a Singular matrix, so x cannot be solved.
# Thus, the next two lines of code are commented out.
# x = np.linalg.solve(A, b)
# print x

# solve for Ax = v
# ERROR: matrix A is still a Singular matrix, so x cannot be solved.
# Thus, the next two lines of code are commented out.
# x2 = np.linalg.solve(A, v)
# print x2

# verify with Ax
# ERROR: cannot verify with Ax because x is still not solved.
# Thus, the next line of code is commented out.
# print np.dot(A, x)

"""
How do we solve this? Add some noise to the matrix!
"""

"""
Use the rand command to build a 10x10 matrix R(e).
Verify now that you can compute the questions above for various values of 1 >> e > 0 for the new matrix A + R(e). 
"""

# epsilon e
e = np.random.random_sample()
print "The epilson is: " + str(e)

# 10 by 10 matrix R(e)
Re = np.random.uniform(-e, e, size=(10, 10))

# 10 by 10 matrix A from 1 to 100 (NOTE: This is a Singular Matrix!)
A = np.arange(1, 101).reshape(10, 10)

# 10 by 1 vector v from 1 to 10
v = np.arange(1, 11).reshape(10, 1)

# 10 by 1 vector b filled with all ones
b = np.ones((10,), dtype=np.int).reshape(10, 1)

# new 10 by 10 matrix Noise or N = A + Re
N = A + Re
print "The new matrix N = A + Re is..."
print N

# solve for Nx = b using linalg.solve
x = np.linalg.solve(N,b)
print "Solution: using linalg.solve, vector x for Nx = b is..."
print x

# solve for Nx = v using linalg.solve
x2 = np.linalg.solve(N,v)
print "Solution: using linalg.solve, vector x2 for Nx = v is..."
print x2

# verify solution for x is correct
# should print vector b
print "Verifying the solution for x..."
print np.dot(N,x)

# verify solution for x2 is correct
# should print vector v
print "Verifying the solution for x2..."
print np.dot(N,x2)

<file_sep># USF MS in Data Science & Analytics Coursework

### Personal Codes & Lecture Notes

NOTE: Please note that not all of my notes and codes are up yet, but my goal is to work on getting all my stuff up and running by spending at least 5-10minutes every day updating my GitHub (: Thanks!

### 1. [Linear Algebra](https://github.com/t2liu/usf_personal/tree/master/linearalgebra) -
- **[Solving Linear Matrix Equations](https://github.com/t2liu/usf_personal/blob/master/linearalgebra/linalg.solve.py)**: how to use the `linalg.solve` function from NumPy
- **[Eliminate.py](ADD LINK)**
- **[leastSquares.py](https://github.com/t2liu/usf_personal/blob/master/linearalgebra/leastSquares.py)**: function that finds the best fit line in the sense of least squares to a set of data consisting of paired observations in the form (x, y)

### 2. [Data Translation Pipeline]() -  

### 3. [Distributed Data Systems](https://github.com/t2liu/usf_personal/tree/master/distributeddatasystems) - 
- **[Lecture Notes Day 1](https://github.com/t2liu/usf_personal/blob/master/distributeddatasystems/Lecture%20Notes%20Day%201-%20Distributed%20Data%20Systems.ipynb)**: notes from Day 1 of Distributed Data Systems course

### 4.

### 5.

### 6.

<file_sep>from jinja2 import Environment
import mycsv

header, data = mycsv.readcsv(mycsv.getdata())
header2 = ['"' + name + '"' for name in header]
rows = len(data)
cols = len(header)

JSON = """{
  "headers":[{% for name in header %}"{{name}}"{% if name != header[-1] %}, {% endif %}{% endfor %}],
  "data":[{% for i in range(rows) %}
    {
      {% for j in range(cols) %}{{header2[j]}}:"{{data[i][j]}}"{% if j != cols-1 %}, {% endif %}{% endfor %}
    }{% if i != rows-1 %},{% endif %}{% endfor %}
  ]
}"""


print Environment().from_string(JSON).render(header=header, data=data, header2=header2, rows=rows, cols=cols)