def get_Xy(df):#读取特征
# """
# use concat to add intersect feature to avoid side effect
# not efficient for big dataset though
# """
ones = pd.DataFrame({'ones': np.ones(len(df))})#ones是m行1列的dataframe
data = pd.concat([ones, df], axis=1) # 合并数据,根据列合并 axis = 1的时候,concat就是行对齐,然后将不同列名称的两张表合并 加列
X = data.iloc[:, :-1] # 这个操作返回 ndarray,不是矩阵
X= X.values
y = data.iloc[:, -1]
y = y.values.reshape(len(y),1)
return X,y
X, y = get_Xy(data)
def sigmoid(z):
gz = 1/(1+np.exp(-z))
return gz
theta = np.zeros([3,1])
theta
def computeCost(theta, X, y):
''' cost fn is -l(theta) for you to minimize'''
# your code here (appro ~ 2 lines)
#theta = theta.reshape(3,1)
#y = y.reshape(100,1)
first = y*np.log(sigmoid(X@theta))
second = (1-y)*np.log(1 - sigmoid(X@theta))
costf = -np.sum(first+second) / (len(X))
return costf
computeCost(theta, X, y)
def gradient(theta, X, y):
# your code here (appro ~ 2 lines)
return X.T@(sigmoid(X@theta)-y)/len(X)
print(gradient(theta, X, y))
def gradientdescent(theta, X, y,alpha,iters):
theta = theta.reshape(3,1)
y = y.reshape(100,1)
temp = np.zeros(theta.shape)
for i in range(iters):
temp = theta -(alpha/len(X))*X.T@(sigmoid(X@theta)-y)
theta = temp
cost = computeCost(theta, X, y)
return theta, cost
alpha = 0.003
iters = 200000
g, cost= gradientdescent(theta, X, y,alpha,iters)
print(g, '\n',cost)
import scipy.optimize as opt
res = opt.minimize(fun=computeCost, x0=theta, args=(X, y),method='Newton-CG', jac=gradient)
#在这里开始就会报错说The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() Numpy对逻辑表达式判别不清楚
print(res)
就是以上代码因为使用.shape函数可以看到我的y是(3,1)但是如果我没用reshape去更改的话默认是(3,)但类型都是naddary,theta也是一样,问题就是我如果不做.reshape处理的话会在前面的梯度下降跟cost函数那里报错除非不使用@这个运算符但换成.T来计算的话输出结果又不对,但如果使用.reshape来提前处理保持全部参数维度一致的话最后这里会报错The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(),请问是为什么,有什么解决办法吗,我目前只能够在函数里面单独处理要调用的参数当做局部变量保持维度一致,一旦在函数外面当做全局变量设置维度一致的话就会出问题