# Crosscompute
data_table_path = 'data.csv'
target_folder = '/tmp'
%pylab inline
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from os.path import join
data_table = pd.read_csv(data_table_path)
# data_table = np.loadtxt(data_table_path, skiprows=1)
l = len(data_table)
y = data_table.values.reshape((l,))
y
x = np.arange(1, l + 1)
x
# coefficient matrix
A = np.vstack([x, np.ones(l)]).T
A
arr = np.linalg.lstsq(A, y)[0]
m, c = arr
arr
y_new = m * x + c
y_new
fig = plt.figure()
plt.plot(x, y, 'o', label='original data', markersize=10)
plt.plot(x, m*x + c, 'r', label='fitted line')
plt.legend()
fitted_line_path = join(target_folder, 'fitted_line.png')
fig.savefig(fitted_line_path)
fig = plt.figure()
plt.plot(data_table)
image_path = join(target_folder, 'data.png')
fig.savefig(image_path)
print('fitted_line_image_path = %s' % fitted_line_path)
print('data_image_path = %s' % image_path)
print('mean = %f' % data_table.mean())
print('stdev = %f' % data_table.std())