Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3.  
  4. def calculate_w(x,y):
  5.    col_ones = np.ones((11800,1))
  6.    x_modified = np.hstack((x,col_ones))
  7.    
  8.    x_pseudo_inversed = np.linalg.pinv(x_modified)
  9.    w = np.matmul(x_pseudo_inversed, y)
  10.    return w    
  11.  
  12.  
  13. def predict(w,new_x):
  14.     predicted_value = np.matmul(w,new_x)
  15.     if predicted_value > 0:
  16.         print ("c1")
  17.     else:
  18.         print ("c2")    
  19.        
  20.        
  21.        
  22. def main():
  23.     y = []
  24.     x = []
  25.     dataset = pd.read_csv('mnist_train.csv')
  26.     dataset = dataset.as_matrix()
  27.    
  28.     for i in range(len(dataset)):
  29.         if dataset[i][0] == 8 or dataset[i][0] == 9:
  30.             y.append(dataset[i][0])
  31.             x.append(dataset[i][1:].tolist())
  32.            
  33.     x = np.array(x)
  34.     y = np.array(y)
  35.     w = calculate_w(x,y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement