Я пытался выяснить проблему с регрессией логистики c. Поэтому проблема состоит в том, чтобы попросить найти некоторые параметры, чтобы разделить точки на две группы. После того, как я завершил функцию costFunction и функцию градиента, я решил использовать функцию fmin_tn c
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import scipy.optimize as opt
def sigmoid(x):
return 1/(1+np.exp(-x))
def costFunction(theta,X,y):
# print(X.shape,theta.shape)
A=(-y)*np.log(sigmoid(X @ theta))
# print(A.shape)
B=(1-y)*np.log(1-sigmoid(X.dot(theta)))
# print(B.shape)
return np.mean(A-B)
data=pd.read_csv("D:\Files\Coursera-ML-AndrewNg-Notes-master\code\ex2-logistic regression\ex2data1.txt",
names=['exam1','exam2','admitted'])
data.insert(0,'Ones',1)
cols=data.shape[1]
X=data.iloc[:,0:cols-1]
y=data.iloc[:,cols-1:cols]
X=np.array(X.values) #100*3
y=np.array(y.values) #100*1
theta=np.zeros(shape=[3,1])
#X:100*3 y:100*1 theta:3*1
def gradient(theta,X,y):
return (X.T @ (sigmoid(X @ theta) - y))/len(X)
theta.reshape([3,1])
res=opt.fmin_tnc(func=costFunction,x0=theta,fprime=gradient,args=(X,y))
positive = data[data.admitted.isin(['1'])]
negetive = data[data.admitted.isin(['0'])]
plt.scatter(positive['exam1'],positive['exam2'],c='r',label='Admitted')
plt.scatter(negetive['exam1'],negetive['exam2'],c='yellow',marker='x',label='NotAdmitted')
x1=np.linspace(30,100)
x2=-1*(final_theta[0]+final_theta[1]*x1)/final_theta[2]
plt.plot(x1,x2,c='blue')
plt.savefig('./fig2.png')
plt.show()
ошибка