简体   繁体   English

IndexError:数组的索引太多......运行此代码时出错

[英]IndexError: too many indices for array ... an error when running this code

I get the following error我收到以下错误

IndexError: too many indices for array IndexError:数组的索引太多

when running this code:运行此代码时:

import numpy as np
import matplotlib.pyplot as plt

y = np.array([[208500, 181500,      
               223500, 
           140000, 250000, 143000, 
           307000, 200000, 129900, 
           118000]])

X = np.array([[1710, 1262, 1786, 
           1717, 2198, 1362, 
           1694, 2090, 1774, 
           1077], 
           [2003, 1976, 2001, 
            1915, 2000, 1993, 
            2004, 1973, 1931, 
            1939]])

data=[X, y]

# print data
print(data)

input= np.array(data)
print(input)

# Setup matrices
m = np.shape(input)[0]
X=np.matrix([np.ones(m),input[:,0]]).T
y = np.matrix(input[:,1]).T

Could someone help me figure out what the issue is?有人可以帮我弄清楚问题是什么吗?

First, your variable named input is a 1D array.首先,名为input的变量是一维数组。 You can't use two indexes to access an 1D array.您不能使用两个索引来访问一维数组。 A good example is that you can't access a matrix only with one index or you can't access a vector with two indexes.一个很好的例子是你不能只用一个索引访问一个矩阵,或者你不能用两个索引访问一个向量。

You can check the size of input if you execute input.shape如果您执行input.shape您可以检查input的大小

As I see, you have in the input variable a 1D array with two nested arrays.如我所见,您在input变量中有一个带有两个嵌套数组的一维数组。 If you want to access the first array, you should execute input[0] and the append the index if the array in that position, for example input[0][0,0]如果要访问第一个数组,则应执行 input[0] 并在该位置的数组中附加索引,例如input[0][0,0]

The first array is 2D shape, you can check it if you execute input[0].shape So, you should use 2 indexes to access it.第一个数组是二维形状,你可以通过执行input[0].shape来检查它所以,你应该使用 2 个索引来访问它。

Let me know if you need further help.如果您需要进一步的帮助,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM