Facebook
From Burly Elephant, 4 Years ago, written in Plain Text.
This paste is a reply to kod from Scanty Lechwe - view diff
Embed
Download Paste or View Raw
Hits: 206
  1. from helpers import visualize_cost_function
  2. import numpy as np
  3.  
  4. def loss_function(sizes, prices, weight0, weight1):
  5.     predictions = [weight0 + weight1*sizes[i] for i in range(len(prices))]
  6.     return sum([(predictions[i] - prices[i])**2 for i in range(len(prices))])/(2*len(prices))
  7.  
  8.  
  9. def loss_function2(sizes, prices, weight0, weight1):
  10.     predictions = [weight0 + weight1*sizes[i] for i in range(len(prices))]
  11.     return sum([(np.sin(predictions[i]/prices[i]**2) * (predictions[i]*prices[i]**3)) for i in range(len(prices))])/(2*len(prices))
  12.  
  13.  
  14. #Poniższy kod wyznacza wartości funkcji kosztu w dziedzinie wartości atrybutów w0 i w1
  15. w0_values = np.arange(-10, 10, 0.5)
  16. w1_values = np.arange(-10, 10, 0.5)
  17.  
  18. sizes_transformed = ((sizes - np.mean(sizes)) / np.std(sizes)).reshape(-1)
  19. prices_transformed = ((prices - np.mean(prices)) / np.std(prices)).reshape(-1)
  20.  
  21. w0_values = np.arange(-100, 100, 5)
  22. w1_values = np.arange(-100, 100, 5)
  23.  
  24. visualize_cost_function(sizes_transformed, prices_transformed, w0_values, w1_values, loss_function)
  25. visualize_cost_function(sizes_transformed, prices_transformed, w0_values, w1_values, loss_function2)