我正在学习Coursera上的Andrew Ng机器学习课程,想尝试对kaggle泰坦尼克号数据集使用逻辑回归,以确保我了解自己在做什么。
passengers.dropna(subset = ['Age'], inplace=True)
X = passengers[['Age']]
X = np.concatenate((np.ones((X.shape[0],1)), X), axis=1)
theta = np.zeros((X.shape[1],1))
Y = passengers['Survived'].values
def gradDesc(X,Y,theta,rate):
for i in range(1000):
h = 1/(1+np.e**(-np.matmul(X, theta)))
gradient = rate * np.dot(X.T, (h-Y)) / len(Y)
theta_temp = theta - gradient
theta = theta_temp
return theta
theta = gradDesc(X,Y,theta,0.000001)
我是numpy,线性代数等的新手,所以我确定自己犯了一些明显的错误。
我的问题是,由我的梯度下降函数返回的权重向量的形状为(2,714)而不是(2,1),其中714为行数。我正在尝试使用梯度下降算法的向量化版本:theta = theta - rate * (1/m) * X'*(g(X * theta) - y)
,但是X'*(g(X * theta) - y)
部分似乎并未执行非向量化版本的求和动作(希望这很有意义) 。就像我说的那样,我敢肯定这是一个愚蠢的错误,但希望有人能帮助我。