我正在嘗試使用optimize.minimize來自 SciPy 庫的 Python 中的正則化來實作邏輯回歸。這是我的代碼:
import pandas as pd
import numpy as np
from scipy import optimize
l = 0.1 # lambda
def sigmoid(z):
return 1 / (1 np.exp(-z))
def cost_function_logit(theta, X, y, l):
h = sigmoid(X @ theta)
# cost
J = -1 / m * (y.T @ np.log(h)
(1 - y).T @ np.log(1 - h)) \
l / (2 * m) * sum(theta[1:] ** 2)
# gradient
a = 1 / m * X.T @ (h - y)
b = l / m * theta
grad = a b
grad[0] = 1 / m * sum(h - y)
return J, grad
data = pd.read_excel('Data.xlsx')
X = data.drop(columns = ['healthy'])
m, n = X.shape
X = X.to_numpy()
X = np.hstack([np.ones([m, 1]), X])
y = pd.DataFrame(data, columns = ['healthy'])
y = y.to_numpy()
initial_theta = np.zeros([n 1, 1])
options = {'maxiter': 400}
res = optimize.minimize(cost_function_logit,
initial_theta,
(X, y, l),
jac = True,
method = 'TNC',
options = options)
在我使用optimize.minimize. 錯誤的最后兩行如下:
grad = a b
ValueError: operands could not be broadcast together with shapes (17,90) (17,)
我檢查了 X、y 和 theta 的型別和尺寸,它們對我來說似乎是正確的。
>>> type(X)
<class 'numpy.ndarray'>
>>> type(y)
<class 'numpy.ndarray'>
>>> type(theta)
<class 'numpy.ndarray'>
>>> X.shape
(90, 17)
>>> y.shape
(90, 1)
>>> theta.shape
(17, 1)
錯誤說 a 是 (17,90) 矩陣,但根據我的計算,它應該是 (17,1) 向量。有誰知道我哪里出錯了?
uj5u.com熱心網友回復:
我找到了解決方案。顯然,optimize.minimize不喜歡 y 和 theta 分別具有形狀 (90,1) 和 (17,1)。我將它們的形狀轉換為 (90,) 和 (17,) 并且錯誤訊息消失了。
在代碼方面,我改變了
initial_theta = np.zeros([n 1, 1])
僅此:
initial_theta = np.zeros([n 1])
我添加了以下行:
y = np.reshape(y, [m])
感謝那些試圖幫助我的人。
uj5u.com熱心網友回復:
的元素a是 90 維向量,而 的元素b是數字。我不完全確定您要做什么,但是如果您想添加矢量,它們需要具有相同的形狀。如果您想按行將事物添加b到每個元素中,您可以這樣做a
grad = a np.stack((b,) * a.shape[1], axis=-1)
但我假設你只是在搞砸構建a.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/433632.html
