In [1]:
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pylab as plt
df_arac = pd.read_csv(u'../data/arac.csv',sep=';')
In [48]:
df_arac
Out[48]:
In [59]:
BaseYear = 1966
x = np.matrix(df_arac.Year[0:-1:5]).T-BaseYear
#y = np.matrix(df_arac.Car[0:]).T/1000000.0
y = np.matrix(df_arac.Minibus[0:-1:5]).T/1000000.0
plt.plot(x+BaseYear, y, 'o-')
plt.xlabel('Year')
plt.ylabel('Number of Cars (Millions)')
plt.show()
In [13]:
df_arac.Year[0:]
Out[13]:
In [62]:
# Setup the vandermonde matrix
N = len(x)
degree = 6
#A = np.hstack((np.power(x,0), np.power(x,1), np.power(x,2)))
A = np.hstack((np.power(x,i) for i in range(degree+1)))
# Solve the least squares problem
w_ls,E,rank,sigma = np.linalg.lstsq(A, y)
#w_ls = (A.T*A).I*A.T*y
plt.plot(x+BaseYear, y, 'o-')
plt.xlabel('Years')
plt.ylabel('Number of Cars')
# Prediction
#TargetYear = np.matrix([2017, 2018, 2019])
TargetYear = np.arange(1960,2021,1/12.0)
x_test = np.matrix(TargetYear - BaseYear).T
A2 = np.hstack((np.power(x_test,i) for i in range(degree+1)))
f = A2*w_ls
plt.plot(x_test+BaseYear, f, 'r-')
plt.show()
In [29]:
np.vstack((np.hstack([1,1]), np.hstack([3,2])))
s = [(i,i*i,i*i*i) for i in range(3)]
s
Out[29]:
In [43]:
A = np.hstack((np.power(x[0:4],i) for i in range(degree+1)))
A2 = np.hstack((np.power(x_test,i) for i in range(degree+1)))
A2*w_ls
Out[43]: