python - PYTHON : IndexError: index 2 is out of bounds for axis 0 with size 2
问题描述
This was my piece of code initially :
Here X is the array of data points with dimensions (m x n) where m is number of data points to predict, and n is number of features without the bias term.
y is the data labels with shape (m,)
lambda_ is the regularization term.
from scipy import optimize
def oneVsAll(X,y,num_labels,lambda_):
#used to find the optimal parametrs theta for each label against the others
#X (m,n)
#y (m,)
#num_labels : possible number of labels
#lambda_ : regularization param
#all_theta : trained param for logistic reg for each class
#hence (k,n+1) where k is #labels and n+1 is #features with bias
m,n = X.shape
all_theta = np.array((num_labels,n+1))
X = np.concatenate([np.ones((m,1)),X],axis = 1)
for k in np.arange(num_labels):
#y == k will generate a list with shape of y,but 1 only for index with value same as k and rest with 0
initial_theta = np.zeros(n+1)
options = {"maxiter" : 50}
res = optimize.minimize(lrCostFunction,
initial_theta,args = (X,y==k,lambda_),
jac = True,method = 'CG',
options = options)
all_theta[k] = res.x
return all_theta
lambda_ = 0.1
all_theta = oneVsAll(X,y,num_labels,lambda_)
The error I got was :
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-45-f9501694361e> in <module>()
1 lambda_ = 0.1
----> 2 all_theta = oneVsAll(X,y,num_labels,lambda_)
<ipython-input-44-05a9b582ccaf> in oneVsAll(X, y, num_labels, lambda_)
20 jac = True,method = 'CG',
21 options = options)
---> 22 all_theta[k] = res.x
23 return all_theta
ValueError: setting an array element with a sequence.
Then after debugging, I changed the code to :
from scipy import optimize
def oneVsAll(X,y,num_labels,lambda_):
#used to find the optimal parametrs theta for each label against the others
#X (m,n)
#y (m,)
#num_labels : possible number of labels
#lambda_ : regularization param
#all_theta : trained param for logistic reg for each class
#hence (k,n+1) where k is #labels and n+1 is #features with bias
m,n = X.shape
all_theta = np.array((num_labels,n+1),dtype = "object")
X = np.concatenate([np.ones((m,1)),X],axis = 1)
for k in np.arange(num_labels):
#y == k will generate a list with shape of y,but 1 only for index with value same as k and rest with 0
initial_theta = np.zeros(n+1)
options = {"maxiter" : 50}
res = optimize.minimize(lrCostFunction,
initial_theta,args = (X,y==k,lambda_),
jac = True,method = 'CG',
options = options)
all_theta[k] = res.x
return all_theta
Now the error I am getting is :
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-47-f9501694361e> in <module>()
1 lambda_ = 0.1
----> 2 all_theta = oneVsAll(X,y,num_labels,lambda_)
<ipython-input-46-383fc22e26cc> in oneVsAll(X, y, num_labels, lambda_)
20 jac = True,method = 'CG',
21 options = options)
---> 22 all_theta[k] = res.x
23 return all_theta
IndexError: index 2 is out of bounds for axis 0 with size 2
How can I correct this?
解决方案
您创建all_theta运行:
all_theta = np.array((num_labels,n+1),dtype = "object")
该指令实际上创建了一个仅包含 2 个元素的数组(形状为(2,)),其中包含两个传递的值,而您可能打算传递要创建的数组的形状。
将此指令更改为:
all_theta = np.empty((num_labels,n+1))
dtype的规范(在我看来)不是必需的。
推荐阅读
- stored-procedures - 如何使用 Java 高效地准备包含大数据的映射表
- c# - Asp.net 核心:托管具有相同身份验证的 MVC 和 SPA。重定向到应用程序的 MVC 部分时客户端被注销
- r - 使用 rnoaa 包中的 lcd 函数时出现奇怪的 tibble 类
- python - 如何在 Python 中实现 col2im()?
- javascript - 我的 JS 代码在教程页面上可以正常工作......我自己不起作用
- python-3.x - 使用 QThread 的工作对象静默崩溃或阻塞主线程
- ruby-on-rails - dnsmasq 启动但忽略名称服务器 - 本地接口
- amazon-web-services - AWS Cognito - 在使用联合登录时添加自定义属性
- batch-file - 如何使用 for 语句去除 Windows Batch 中过多的 if 语句?
- c++ - 从 C++ 函数中删除 noexcept,如何处理调用它的 noexcept 函数?