Professional Documents
Culture Documents
"""Untitled5.ipynb
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.model_selection import train_test_split, cross_val_score
#from statistics import meanimport pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.model_selection import train_test_split, cross_val_score
from statistics import mean
X=np.arange(9).reshape(9,1)
Y=np.array([2.5,4.2,2,1,4.4,0.9,1.7,5.1,12])
plt.title("Matplotlib plot")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(X,Y)
plt.show()
model = LinearRegression().fit(X, Y)
r_sq = model.score(X, Y)
print('coefficient of determination:', r_sq)
"""#1.3 Plot the coefficient and plot the model curve from 0 to 10 at
x=np.arrange(0,10,0.01)
"""
"""
x_new=np.arange(0,8,0.01).reshape(800,1)
y_new = model.predict(x_new)
print (y_new)
"""#1.5 Use the normal equation to solve the model paramters. Print the solution.
x_new
"""
theta_best_values = np.linalg.inv(x_new.T.dot(x_new)).dot(x_new.T).dot(Y)
"""
#poly order2
poly = PolynomialFeatures(degree = 2)
X_poly = poly.fit_transform(X)
poly.fit(X_poly, Y)
lin2 = LinearRegression(fit_intercept=True)
lin2.fit(X_poly, Y)
w_0 = lin2.intercept_
w_1 = lin2.coef_[1]
w_2 = lin2.coef_[2]
print (lin2.intercept_)
print (lin2.coef_)
x_new=np.arange(0,8,0.01).reshape(800,1)
x_new_poly=poly.fit_transform(x_new)
prediction = lin2.predict(x_new_poly)
plt.scatter(x_new, prediction)
plt.show()
#poly order8
poly = PolynomialFeatures(degree = 8)
X_poly = poly.fit_transform(X)
lin = LinearRegression()
poly.fit(X_poly, Y)
lin3 = LinearRegression(fit_intercept=True)
lin3.fit(X_poly, Y)
w_0 = lin3.intercept_
w_1 = lin3.coef_[1]
w_2 = lin3.coef_[2]
print(lin3.intercept_)
print(lin3.coef_)
x_new=np.arange(0,8,0.01).reshape(800,1)
x_new_poly=poly.fit_transform(x_new)
prediction = lin3.predict(x_new_poly)
plt.scatter(x_new, prediction)
plt.show()
"""
"""
print ("\n\n\n\n\n")
return theta
def predict(X):
return preds
"""
theta = find_theta(X, Y)
print ("The best values presented is as shown below: ", theta)
"""
preds = predict(X)
# Plotting the predictions.
fig = plt.figure(figsize=(8,6))
plt.plot(X, Y, 'b.')
plt.plot(X, preds, 'c-')
plt.xlabel('X - Input')
plt.ylabel('y - target / true')
plt.show()
"""#2.4 Convert data to polynomial order 8. Report L2 regularized test accuracy and
Report none regularized test accuracy
"""