project1
import pandas as pd
import numpy as np
data_url='https://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data'
auto_mpg = pd.read_table(data_url,sep='\s+',header=None)
# Based on description, change col names
auto_mpg.columns=['mpg','cylinders','displacement','horsepower','weight','acceleration','model year','origin','car name']
auto_mpg2=auto_mpg[['mpg','displacement','acceleration']]
n_auto_mpg2=(auto_mpg2 - auto_mpg2.mean()) / auto_mpg2.std()
def gradient_decent(y,x,alpha,theta,n,n_it,lamb):
for i in range(0,n_it):
tmp=(1-alpha*lamb*(1/n))*theta-np.dot(x.T,(x.dot(theta)-y))*(alpha/n)
print("Iteration %d" % i)
theta=tmp
return theta
x=n_auto_mpg2[['displacement','acceleration']]
x.insert(0,'constant',np.full(398,1))
x=pd.DataFrame.as_matrix(x)
y=pd.DataFrame.as_matrix(n_auto_mpg2[['mpg']])
theta=np.array([1,1,1],dtype=np.float64).reshape(3,1)
alpha=0.01
n=398
lamb=1
n_it=10000
gradient_decent(y,x,alpha,theta,n,n_it,lamb)
import numpy.linalg as npl
theta=npl.inv(x.T.dot(x)).dot(x.T.dot(y))
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(x, y)
# display coefficients
print(regressor.coef_)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.2)
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
%matplotlib inline
fig = plt.figure()
ax = Axes3D(fig)
# Make data.
mesh_x1,mesh_x2=np.meshgrid(n_auto_mpg2[['displacement']], n_auto_mpg2[['acceleration']])
mesh_xx1=mesh_x1.flatten()
mesh_xx2=mesh_x2.flatten()
mesh_mpg=np.dot(np.c_[np.ones(mesh_xx1.shape),mesh_xx1, mesh_xx2], theta).reshape(mesh_x1.shape)
ax.plot_surface(mesh_x1,mesh_x2,mesh_mpg,cmap=cm.coolwarm,linewidth=0)
ax.scatter(n_auto_mpg2[['displacement']], n_auto_mpg2[['acceleration']], n_auto_mpg2[['mpg']], c='b', s=50)
ax.set_xlabel('displacement'),
ax.set_ylabel('acceleration')
ax.set_zlabel('mpg')
Last updated
Was this helpful?