Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. def fit_and_get_errors(X_train, X_test, y_train, y_test):
  2.  
  3. Train_err=[]
  4. Test_err=[]
  5. for d in range(1, 21):
  6. X_tilda,w = get_model(X_train, y_train, d)
  7. h1, E_train = get_prediction_and_error(X_tilda, y_train, w)
  8. Train_err.append(np.log(E_train))
  9.  
  10. X_temp, w1 = get_model(X_train, y_train, d)
  11. h2, E_test = get_prediction_and_error(X_temp, y_test, w)
  12. Test_err.append(np.log(E_test))
  13.  
  14. return Train_err, Test_err
  15.  
  16. #plotanje
  17. fig, axs = plt.subplots(3, 3, figsize=(15, 15))
  18. axs = axs.ravel()
  19.  
  20. degree=list(range(1, 21))
  21. noise = [100, 200, 500]
  22. # N = [50, 100, 500] #ovliko cemo uzmiat iz test/train skupa
  23. K = [1/3, 2/3, 1]
  24.  
  25. X = make_instances(-5,5,1000)
  26. X_train, X_test = train_test_split(X, test_size=0.5)
  27.  
  28.  
  29. for i, sigma in enumerate(noise):
  30. y = list(make_labels(X_train, f, sigma))
  31. y_train = np.array(y)
  32. y = list(make_labels(X_test,f,sigma))
  33. y_test = np.array(y)
  34.  
  35. for j, n in enumerate(K):
  36. number_of_examples=math.floor(n*500)
  37. index_list = np.random.choice(np.arange(500), number_of_examples, replace=False)
  38. X1_train = X_train[index_list]
  39. y1_train = y_train[index_list]
  40.  
  41.  
  42. # index_list = np.random.choice(np.arange(500), number_of_examples, replace=False)
  43. X1_test = X_test[index_list]
  44. y1_test = y_test[index_list]
  45.  
  46. #dobivanje train i test erora
  47. train,test=fit_and_get_errors(X1_train,X1_test,y1_train,y1_test)
  48.  
  49. axs[i*3+j].set_title("N = " + str(math.ceil(n*500)) + ", noise = " + str(sigma))
  50. axs[i*3+j].plot(degree,train,label="Train")
  51. axs[i*3+j].plot(degree,test,label="Test")
  52. axs[i*3+j].legend()
  53.  
  54.  
  55. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement