Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- # number of points
- num_points = 300
- # For the sample data
- # Input
- x = []
- # Output
- y = []
- # Answer values
- x_ans = [i * 0.01 for i in range(-200, 200)]
- W_ans = 0.1
- # Generate normal random values for input and output
- for i in range(num_points):
- _x = np.random.normal(0.0, 0.55)
- _y = W_ans * _x + np.random.normal(0.0, 0.03)
- x.append(_x)
- y.append(_y)
- # Draw input, output and answer line
- plt.plot(x, y, "ro")
- plt.plot(x_ans, [W_ans * _x for _x in x_ans], label="W=0.1")
- plt.xlabel("X")
- plt.ylabel("Y")
- plt.legend()
- plt.title("y = 0.1 * x")
- plt.show()
- # For the Mean Square Error
- # Weight from 0 to 0.2
- W = [i * 0.001 for i in range(0, 200)]
- # Cost
- cost = []
- for w in W:
- # hypothesis = W * x
- hypo = [w * _x for _x in x]
- diffSqrt = 0
- for i in range(num_points):
- _hypo = hypo[i]
- _y = y[i]
- # sum((W * x - y)^2)
- diffSqrt = diffSqrt + (_hypo - _y)**2
- # 1 / m * sum(W * x - y)^2
- cost.append(1 / (len(W)) * diffSqrt)
- # Draw cost function
- plt.plot(W, cost)
- plt.title("Cost Function")
- plt.xlabel("W")
- plt.ylabel("Cost(W)")
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement