apl-mhd

linear regression

Oct 26th, 2019
195
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. dataframe = datasets.load_diabetes()
  2. print(dataframe)
  3.  
  4. X = dataframe.data[:,:]
  5. Y = dataframe.target
  6.  
  7. X_train, X_test, Y_train, Y_test = train_test_split(X, Y,test_size=0.30, random_state=42)
  8.  
  9. train_size = X_train.shape[0]
  10. test_size  = X_test.shape[0]
  11. dt = dataframe.data.shape[1]
  12. #print(X_train)
  13.  
  14. w= np.zeros((1,dt))
  15. print(w)
  16. b=0
  17.  
  18. alpha=0.000001
  19.  
  20.  
  21. for i in range (500):
  22.     yht = np.dot(X_train, w.T)
  23.     yht += b
  24.     diff = yht - Y_train.reshape((yht.shape))
  25.     b = b - alpha*((1/X_train.shape[0])*(np.sum(diff)))
  26.     m = diff.shape[0]
  27.     #print(diff.shape)
  28.  
  29.     for j in range(10):
  30.        
  31.         w[0,j]=w[0,j]-(alpha*((1/m)*(np.dot(X_train[:, j].reshape(1,m), diff))))
  32.         #print(w)
  33.         df = yht - Y_train.reshape(m,1)
  34.         sum=0
  35.        
  36.         for i in range (df.shape[0]):
  37.             sum+=df[i, 0]**2
  38.    
  39.        print(sum/2*m)
  40.  
  41.        
  42. #print(w.shape)
RAW Paste Data