Adding a number to an array gives an index problem

Asked

Viewed 89 times

0

I have tried everything and could not add the number 1 at the bottom of the list predictors. Gives the following error:

new_inputs[i] = np.append(inputs[i],one[i])

Indexerror: list assignment index out of range

I can’t use append normal because it is an array and using the library Numpy also does not work. I need to put the value 1 in the last column of each sample.

That is the code:

base = pd.read_excel("TSLA.xlsx", sheet_name=4)
base = base.dropna()
base_treinamento = base.iloc[:, 1:2].values

previsores = []
preco_real = []
for i in range(90, 753):
    previsores.append(base_treinamento[i-90:i, 0])
    preco_real.append(base_treinamento[i, 0])

inputs = np.array(previsores)

one =np.ones(len(previsores))
new_inputs=[]
for i in range(len(previsores)):
    new_inputs[i] = np.append(inputs[i], one[i] )

And the link to the file: TSLA

1 answer

0


inputs and one are arrays of different structures (inputs can be viewed as a list of lists and one just like a list), so your loop won’t work. By the way, you don’t even need the loop since you shouldn’t use append and yes concatenate. Do so:

one = np.ones((len(previsores), 1))
new_inputs = np.concatenate([inputs, one], axis = 1)
  • It worked perfect, thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.