Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. import numpy as np
  2. from sklearn.linear_model import LogisticRegression
  3. from sklearn.cross_validation import train_test_split
  4. from sklearn.metrics import accuracy_score
  5. from sklearn.datasets import load_breast_cancer
  6. import matplotlib.pyplot as plt
  7.  
  8. data = load_breast_cancer()
  9. np.random.seed(0)
  10. X = data.data
  11. Y = data.target
  12. X = X / X.max(axis=0)
  13. X_train, X_test, Y_train, Y_test = train_test_split(X, Y.ravel(), test_size=0.33)
  14.  
  15.  
  16. class LogReg(object):
  17.     def __init__(self):
  18.         pass
  19.  
  20.     def sigmoid(self, X):
  21.         return 1 / (1 + np.exp(-X))
  22.  
  23.     def hypothesis(self, Xp):
  24.         return Xp.dot(self.theta)
  25.  
  26.     def cost(self):
  27.         r = (self.hypothesis(self.x))
  28.         r=r/r.max(axis=0)
  29.         r=np.absolute(r)
  30.         p1=0
  31.         p2=0
  32.         for oy in range(len(self.y)):
  33.             if self.y[oy]==1:
  34.                 p1+=float(np.log(r[oy]))
  35.             else:
  36.                 p2+=float(np.log(1-np.log(r[oy])))
  37.         r=-(p1+p2)/len(self.y)
  38.         return r
  39.     def fit(self, X, Y, alpha, itr):
  40.         self.x = np.array(X)
  41.         self.y = np.array(Y).reshape(-1, 1)
  42.         self.alpha = alpha
  43.         self.cst = []
  44.         self.theta = np.zeros([self.x.shape[1], self.y.shape[1]])
  45.         for i in range(itr):
  46.             self.error = self.hypothesis(self.x) - self.y
  47.             self.theta = self.theta - (self.alpha / len(self.x)) * self.x.T.dot(self.error)
  48.             self.cst.append(self.cost())
  49.  
  50.     def costgraph(self):
  51.         plt.plot(range(len(self.cst)), self.cst)
  52.         plt.grid(True)
  53.         plt.show()
  54.  
  55.     def preditc(self, X):
  56.         output = self.hypothesis(X)
  57.         output[output>=0.5]=1
  58.         output[output <0.5] = 0
  59.         return output
  60.  
  61.  
  62. a = LogReg()
  63. a.fit(X_train, Y_train, alpha=.07, itr=5000)
  64. output = a.preditc(X_test)
  65.  
  66. print("Scratch Accuracy : ",accuracy_score(Y_test, output))
  67.  
  68.  
  69. model = LogisticRegression()
  70. model.fit(X_train, Y_train)
  71.  
  72. print("Scikit Accuracy (With Default Parameters: ",accuracy_score(Y_test, model.predict(X_test)))
  73.  
  74. a.costgraph()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement