【发布时间】:2018-02-04 14:16:05
【问题描述】:
我目前正在阅读 Python for Machine Learning 一书的第 3 章。在计算机上实现书中的算法时,我收到一个 ValueError 指出“不允许将整数转换为负整数”,即使我以与书中描述的相同方式实现代码。这里有人知道为什么我会收到 valueerror 并帮助我修复它吗?
代码:
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression(C=1000.0, random_state = 0)
lr.fit(X_train_std, y_train)
plot_decision_regions(X_combined_std, y_combined, classifier = lr, test_idx = range(105,150))
plt.xlabel('petal length [standardized]')
plt.ylabel('petal width [standardized')
plt.legend(loc = 'upper left')
plt.show()
weights, params = [], []
for c in np.arange(-5,5):
lr = LogisticRegression(C=10**c, random_state = 0)
lr.fit(X_train_std, y_train)
weights.append(lr.coef_[1])
params.append(10**c)
weights = np.array(weights)
plt.plot(params, weights[:,0], label = 'petal length')
plt.plot(params, weights[:,1], linestyle = '--', label = 'petal width')
plt.ylabel('weight coefficient')
plt.xlabel('C')
plt.legend(loc = 'upper left')
plt.xscale('log')
plt.show()
错误:
lr = LogisticRegression(C=10**c, random_state = 0)
ValueError:不允许使用负整数幂的整数。
【6.1s完成】
【问题讨论】:
-
开始here。
-
@sascha 我尝试将'for c in np.arange(-5,5):' 更改为 for c in np.arange(5,-5):'。我开始收到 IndexError。 'plt.plot(params, weights[:,0], label = 'petal length') IndexError: too many indices for array'.
-
那应该怎么做?阅读链接。使用
for c in range(-5,5)。您的错误是仅在 c 变为负数时您的修改中断之前发生的其他错误。所以不要使用你的模组并修复其他不相关的错误。 -
@sascha 谢谢,现在可以使用了。
标签: python machine-learning valueerror