如何使用scipy优化来优化成本Logistic回归成本函数?

时间:2018-12-31 12:18:32

标签: python python-3.x machine-learning scipy

scipy.optimize.fmin_tnc无法正常工作。有关尺寸不匹配的错误。

我正在优化Logistic回归成本函数。我还计算了梯度。我已经实现了矢量化。任何人都可以帮忙。

import numpy as np    
import pandas as pd    
import matplotlib.pyplot as plt    
import scipy.optimize as opt    

data = pd.read_csv('ex2data1.txt',header = None)    
X = data.iloc[:,:-1]    
y = data.iloc[:,2]    
data.head()    

mask = (y==1)    
adm = plt.scatter(X[mask][0].values,X[mask][1].values)    
not_adm = plt.scatter(X[~mask][0].values,X[~mask][1].values)    
plt.xlabel('exam 1 score')    
plt.ylabel('exam 2 score')    
plt.legend((adm,not_adm),('admitted','not admitted'))    
plt.show()    

m,n = X.shape    
theta = np.zeros((n+1,1))    
X = np.hstack((np.ones((m,1)),X))    
y = y[y,np.newaxis]    
#only data reading and preparation so far.    

def sigmoid(z):    
    return 1/(1+np.exp(-z))    

def computeCost(X,y,theta):    
    h = sigmoid(np.dot(X,theta))    
    J = (-1/m)*(np.dot(y.T,np.log(h))+np.dot((1-y).T,np.log(1-h)))    
    return J

def gradient(X,y,theta):    
    h = sigmoid(np.dot(X,theta))    
    beta = h-y    
    return np.dot(X.T,beta)*(1/m)    

temp = opt.fmin_tnc(func = computeCost,x0 = theta.flatten(),fprime =gradient, args = (X, y.flatten()))       

这将导致尺寸不匹配错误。我已经检查了computeCost和渐变函数,它们是否正常工作。 X是100x3,y是100x1,θ是3x1。 https://i.stack.imgur.com/0hlKA.jpg这是错误图像的链接

0 个答案:

没有答案