Guest User

Untitled

a guest
May 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from sklearn import linear_model
  4. from sklearn.linear_model import Ridge
  5. from sklearn.preprocessing import PolynomialFeatures
  6. from sklearn.pipeline import make_pipeline
  7.  
  8. # 1) Generate training data
  9. TOTAL_SAMPLES = 1 # faking data collected (age, distange_to_feet) for TOTAL_SAMPLES people
  10.  
  11. train_X = []
  12. train_Y = []
  13.  
  14. for _ in range(TOTAL_SAMPLES):
  15. # noise to add to the data
  16. mu, sigma = 0, 0.3 # mean and standard deviation
  17. noise_x = np.random.normal(mu, sigma, 100)
  18. noise_y = np.random.normal(mu, sigma*1.5, 100)
  19. train_X += [i + 1 + noise_x[i] for i in range(1,100,2)]
  20. train_Y += [abs(np.log(i+1)*2 + noise_y[i]) for i in range(1,100,2)]
  21.  
  22. # Plotting the training data and the ground truth curve
  23. fig, ax = plt.subplots()
  24. ax.set_title('Distance to reach the feet depeding on age')
  25. ax.set_ylabel('Distance to feet (cm)')
  26. ax.set_xlabel('Age')
  27. fig.set_size_inches(20,10)
  28. plt.scatter(train_X, train_Y, color='g', label='"Real" data points')
  29. plt.plot(range(100), [np.log(i+1)*2 for i in range(100)], label='Ideal curve')
  30. plt.legend(loc='lower right')
  31.  
  32. # 2) Create linear regression object and train it
  33. regr = linear_model.LinearRegression()
  34. train_X = np.array(train_X).reshape(-1, 1) # reshaping the data because we have only 1 dimension in the features
  35. regr.fit(train_X, train_Y)
  36.  
  37. # 3) Predict a few values with the new model
  38. X_test = [[0], [10], [20], [30], [40], [50], [60], [70], [80], [100]]
  39. Y_pred = regr.predict(X_test)
  40. print(Y_pred)
  41. # >> [ 4.5010285 5.06034505 5.61966161 6.17897817 6.73829473 7.29761128 7.85692784 8.4162444 8.97556095 10.09419407]
  42.  
  43. # Plot outputs
  44. fig, ax = plt.subplots()
  45. ax.set_title('Distance to reach the feet depeding on age')
  46. ax.set_ylabel('Distance to feet (cm)')
  47. ax.set_xlabel('Age')
  48. fig.set_size_inches(20,10)
  49. plt.scatter(train_X, train_Y, color='g', label='"Real" data points')
  50. plt.plot(X_test, Y_pred, color='blue', linewidth=5, label="Regression model")
  51. plt.legend(loc='lower right')
  52.  
  53. # 4) Polynomial regression, plot models with degree 3 and 11
  54. colors = ['orange', 'red']
  55. legends = ['Polynomial interpolation (degree=3)', 'Polynomial interpolation (degree=11)']
  56. lw = 2
  57.  
  58. x_plot = np.linspace(0, 100, 10000).reshape(-1, 1)
  59. for count, degree in enumerate([3, 11]):
  60. fig, ax = plt.subplots()
  61. fig.set_size_inches(20,10)
  62. ax.set_title('Distance to reach the feet depeding on age')
  63. ax.set_ylabel('Distance to feet (cm)')
  64. model = make_pipeline(PolynomialFeatures(degree), Ridge())
  65. model.fit(train_X, train_Y)
  66. y_plot = model.predict(x_plot)
  67. plt.scatter(train_X, train_Y, color='g', label='"Real" data points')
  68. plt.plot(x_plot, y_plot, color=colors[count], linewidth=lw, label=legends[count])
  69. plt.legend(loc='lower right')
Add Comment
Please, Sign In to add comment