Added new codes
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.preprocessing import PolynomialFeatures
|
||||
from sklearn.linear_model import LinearRegression
|
||||
|
||||
steps=250
|
||||
|
||||
distance=0
|
||||
x=0
|
||||
distance_list=[]
|
||||
steps_list=[]
|
||||
while x<steps:
|
||||
distance+=np.random.randint(-1,2)
|
||||
distance_list.append(distance)
|
||||
x+=1
|
||||
steps_list.append(x)
|
||||
plt.plot(steps_list,distance_list, color='green', label="Random Walk Data")
|
||||
|
||||
steps_list=np.asarray(steps_list)
|
||||
distance_list=np.asarray(distance_list)
|
||||
|
||||
X=steps_list[:,np.newaxis]
|
||||
|
||||
#Polynomial fits
|
||||
|
||||
#Degree 2
|
||||
poly_features=PolynomialFeatures(degree=2, include_bias=False)
|
||||
X_poly=poly_features.fit_transform(X)
|
||||
|
||||
lin_reg=LinearRegression()
|
||||
poly_fit=lin_reg.fit(X_poly,distance_list)
|
||||
b=lin_reg.coef_
|
||||
c=lin_reg.intercept_
|
||||
print ("2nd degree coefficients:")
|
||||
print ("zero power: ",c)
|
||||
print ("first power: ", b[0])
|
||||
print ("second power: ",b[1])
|
||||
|
||||
z = np.arange(0, steps, .01)
|
||||
z_mod=b[1]*z**2+b[0]*z+c
|
||||
|
||||
fit_mod=b[1]*X**2+b[0]*X+c
|
||||
plt.plot(z, z_mod, color='r', label="2nd Degree Fit")
|
||||
plt.title("Polynomial Regression")
|
||||
|
||||
plt.xlabel("Steps")
|
||||
plt.ylabel("Distance")
|
||||
|
||||
#Degree 10
|
||||
poly_features10=PolynomialFeatures(degree=10, include_bias=False)
|
||||
X_poly10=poly_features10.fit_transform(X)
|
||||
|
||||
poly_fit10=lin_reg.fit(X_poly10,distance_list)
|
||||
|
||||
y_plot=poly_fit10.predict(X_poly10)
|
||||
plt.plot(X, y_plot, color='black', label="10th Degree Fit")
|
||||
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
|
||||
#Decision Tree Regression
|
||||
from sklearn.tree import DecisionTreeRegressor
|
||||
regr_1=DecisionTreeRegressor(max_depth=2)
|
||||
regr_2=DecisionTreeRegressor(max_depth=5)
|
||||
regr_3=DecisionTreeRegressor(max_depth=7)
|
||||
regr_1.fit(X, distance_list)
|
||||
regr_2.fit(X, distance_list)
|
||||
regr_3.fit(X, distance_list)
|
||||
|
||||
X_test = np.arange(0.0, steps, 0.01)[:, np.newaxis]
|
||||
y_1 = regr_1.predict(X_test)
|
||||
y_2 = regr_2.predict(X_test)
|
||||
y_3=regr_3.predict(X_test)
|
||||
|
||||
# Plot the results
|
||||
plt.figure()
|
||||
plt.scatter(X, distance_list, s=2.5, c="black", label="data")
|
||||
plt.plot(X_test, y_1, color="red",
|
||||
label="max_depth=2", linewidth=2)
|
||||
plt.plot(X_test, y_2, color="green", label="max_depth=5", linewidth=2)
|
||||
plt.plot(X_test, y_3, color="m", label="max_depth=7", linewidth=2)
|
||||
|
||||
plt.xlabel("Data")
|
||||
plt.ylabel("Darget")
|
||||
plt.title("Decision Tree Regression")
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
|
||||
"""new_dist=distance_list[-1]
|
||||
step_max=2500
|
||||
new_x=steps
|
||||
new_dist_list=[]
|
||||
new_steps_list=np.arange(steps,step_max)
|
||||
while new_x>=steps and new_x<step_max:
|
||||
dist_prediction=clf.predict(new_x)
|
||||
new_dist_list.append(dist_prediction)
|
||||
new_x+=1
|
||||
plt.plot(new_steps_list,new_dist_list, color='red')
|
||||
plt.show()"""
|
||||
Reference in New Issue
Block a user