Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import numpy.linalg as linalg
- import pandas as pd
- import csv as csv
- # Read in data to a pandas dataFrame. This file has 3 columns, 'x1', 'x2' and 'y'
- data = pd.read_csv(r'.logistic_xy.csv')
- # Convert to a numpy array
- data = np.array(data)
- # Define data matricies
- X = np.array(data[0::,0:3])
- y = np.array(data[0::,3])
- y = y.reshape((99,1))
- W = np.zeros([99,99],dtype=float)
- p = np.zeros([99,1],dtype=float)
- z = np.zeros([99,1], dtype=float)
- # Define hypothesis function
- def hTheta(x):
- g = np.exp(x)/(1+np.exp(x))
- return g
- # Define convergence variable and initialize theta
- theta = np.zeros([3,1],dtype=float)
- thetaNew = theta
- conv = 1
- while conv > 0.01:
- for i in xrange(0,int(data[0::,0].sum())):
- theta = thetaNew
- x = np.dot(np.transpose(theta),data[i,0:3])[0]
- h = hTheta(x)
- W[i,i] = h*(1-h)
- p[i] = h
- z = np.add(np.dot(X,theta),np.dot(np.linalg.inv(W),np.subtract(y,p)))
- A = np.linalg.inv(np.dot(X.transpose(),np.dot(W,X)))
- B = np.dot(X.transpose(),np.dot(W,z))
- thetaNew = np.dot(A,B)
- conv = np.linalg.norm(np.subtract(thetaNew,theta))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement