Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. import numpy as np
  2. from matplotlib import pyplot as plt
  3. import matplotlib
  4. import seaborn as sns
  5. import pandas
  6.  
  7. font = {'family' : 'Helvetica',
  8. 'size' : 18}
  9.  
  10. matplotlib.rc('font', **font)
  11.  
  12. np.random.seed(222)
  13. DPPD = 200
  14. X = np.random.normal(0,2, (DPPD, 2))
  15.  
  16.  
  17. # target w
  18. w_target = np.random.normal(0,1, (2,1))
  19.  
  20. # constraints (overdetermined for multiple solutions)
  21. d = np.random.normal(0,1, (1,1))
  22. C = np.random.normal(0,1, (1,2))
  23.  
  24. # objective
  25. y = X@w_target + np.random.normal(0, 0.2, (DPPD,1))
  26.  
  27. Y_tilde = [y, d]
  28. X_tilde = [X, C]
  29.  
  30. # objective weights
  31. lambd = np.asarray([1.0, 10000.0])
  32.  
  33.  
  34. A_tilde = np.bmat([[np.sqrt(l)*x] for l,x in zip(lambd, X_tilde)])
  35. y_tilde = np.bmat([[np.sqrt(l)*y] for l,y in zip(lambd, Y_tilde)])
  36.  
  37. # least squares
  38. w_estimate = np.linalg.inv(A_tilde.T@A_tilde)@A_tilde.T@y_tilde
  39.  
  40.  
  41. y_estimate = X@w_estimate
  42.  
  43. # This import registers the 3D projection, but is otherwise unused.
  44. from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
  45.  
  46. import matplotlib.pyplot as plt
  47. import numpy as np
  48.  
  49.  
  50.  
  51. x1 = y_estimate.flatten()
  52. x2 = X[:, 0].flatten()
  53. y_estimate = y_estimate.flatten()
  54. y_true = y.flatten()
  55.  
  56. fig = plt.figure(figsize=(20,10))
  57. ax = fig.add_subplot(111, projection='3d')
  58. ax.scatter(x1,x2,y_estimate, label="Estimate")
  59. ax.scatter(x1,x2,y_true, label="Ground Truth")
  60. ax.view_init(elev=10., azim=20)
  61.  
  62. ax.set_xlabel('X0')
  63. ax.set_ylabel('X1')
  64. ax.set_zlabel('Y')
  65.  
  66. plt.legend()
  67. plt.tight_layout()
  68. plt.savefig("3d.png")
  69. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement