2017-08-26 35 views
0

我目前在关于机器学习书的python的第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: Integers to negative integer powers are not allowed.
[Finished in 6.1s]

+1

开始[这里](https://github.com/numpy/numpy/issues/8917)。 – sascha

+0

@sascha我试着改变'for np.arange(-5,5):'to for c in np.arange(5,-5):'。我开始得到一个IndexError。 'plt.plot(params,weights [:,0],label ='花瓣长度') IndexError:array'的索引太多。 –

+0

那该怎么办?阅读链接。在范围(-5,5)'''中使用'''作为c。你的错误是一些其他的错误只发生在你的修改发生时,当c变为负值时。所以不要使用你的MOD并修复其他无关的错误。 – sascha

回答

-1
for c in np.arange (-5, 5,dtype = float): 
+0

如果你不解释原因,这不是一个非常有用的答案。 –

相关问题