Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Tue May 21 14:42:10 2019
  5. Regresija
  6. @author: dsp
  7. """
  8.  
  9. #%%
  10.  
  11. import numpy as np
  12. from matplotlib import pyplot as plt
  13. from sklearn import neural_network
  14. #%% data regression
  15. np.random.seed(42)
  16. xs = np.random.rand(10) * 2*np.pi
  17. xs=xs[:,np.newaxis]
  18. noise = np.random.normal(size = xs.shape) * .5
  19.  
  20. ys = np.sin(xs)
  21. ys += noise
  22. plt.figure()
  23. plt.scatter(xs,ys)
  24.  
  25.  
  26.  
  27.  
  28. #%% train neuron
  29. reg = neural_network.MLPRegressor(
  30. hidden_layer_sizes=(100,10),
  31. activation='tanh',
  32. solver='adam',
  33. alpha=0,
  34. learning_rate= 'adaptive',
  35. tol = 1e-15,
  36. max_iter=10000,
  37. random_state=42,
  38. early_stopping=False,
  39. validation_fraction=.2,
  40. verbose=1)
  41.  
  42. reg.fit(xs,ys)
  43.  
  44. #%% predict
  45.  
  46. xs_axis = np.linspace(0,2*np.pi,100)
  47. xs_axis = xs_axis[:,np.newaxis]
  48. ys_pred = reg.predict(xs_axis)
  49.  
  50. plt.figure()
  51. plt.scatter(xs,ys)
  52. plt.plot(xs_axis,ys_pred,lw=2,alpha=0.7)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement