【发布时间】:2015-07-12 22:26:25
【问题描述】:
我有以下代码sn-p:
def func1(self, X, y):
#X.shape = (455,13)
#y.shape = (455)
num_examples, num_features = np.shape(X)
self.weights = np.random.uniform(-1 / (2 * num_examples), 1 / (2 * num_examples), num_features)
while condition:
new_weights = np.zeros(num_features)
K = (np.dot(X, self.weights) - y)
for j in range(num_features):
summ = 0
for i in range(num_examples):
summ += K[i] * X[i][j]
new_weights[j] = self.weights[j] - ((self.alpha / num_examples) * summ)
self.weights = new_weights
此代码运行速度太慢。有什么优化,我可以做吗?
【问题讨论】:
-
什么是
condition? -
@unutbu,
count of iteration > 0. -
while-loop是无限的吗? -
@unutbu,没有。我只是从示例中删除
counter。 -
您真的要重置
summ=0内部for j循环吗?这样一来,您就丢弃了for j循环的每次迭代完成的所有工作,除了j等于num_features-1的最后一次迭代。
标签: python arrays performance numpy optimization