【发布时间】:2016-02-11 09:39:03
【问题描述】:
以下 Python 代码可以很好地找到梯度下降:
def gradientDescent(x, y, theta, alpha, m, numIterations):
xTrans = x.transpose()
for i in range(0, numIterations):
hypothesis = np.dot(x, theta)
loss = hypothesis - y
cost = np.sum(loss ** 2) / (2 * m)
print("Iteration %d | Cost: %f" % (i, cost))
gradient = np.dot(xTrans, loss) / m
theta = theta - alpha * gradient
return theta
这里,x = m*n(m = 样本数据数量,n = 总特征)特征矩阵。
但是,如果我的特征是“2”电影的非数字特征(例如导演和类型),那么我的特征矩阵可能如下所示:
['Peter Jackson', 'Action'
Sergio Leone', 'Comedy']
在这种情况下,如何将这些特征映射到数值并应用梯度下降?
【问题讨论】:
标签: machine-learning linear-regression gradient-descent