I am runnig a certain code and this error keeps coming up after executing the code shown below:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
X_train = []
y_train = []
for i in range(60, 2035):
X_train=np.append(training_set_scaled[i-60:i, 0])
y_train=np.append(training_set_scaled[i, 0])
X_train, y_train = np.array(X_train), np.array(y_train)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
error:
TypeError: _append_dispatcher() missing 1 required positional argument: 'values'
CodePudding user response:
numpy.append
has two required parameters, arr
and values
, but you only give it one on both of these lines:
X_train=np.append(training_set_scaled[i-60:i, 0])
y_train=np.append(training_set_scaled[i, 0])
You need to specify both the array you're appending to and the values to append.
CodePudding user response:
You seem to be confusing list and array operations. Your particular error occurs in np.append
. Did you (re)read its docs? That should be the first thing you do when getting an error like this (before running to SO for help!)
What do you mean by `keeps coming up'? Do you keep trying the same thing in the hope that the next time the error won't occur? Errors like this aren't random :)
First you create two lists:
X_train = []
y_train = []
Normally lists like this are used in a loop
for i in range(...):
X_train.append(i)
repeatedly adding a value to list. This works in-place.
But in this iteration you switch to arrays:
for i in range(60, 2035):
X_train=np.append(training_set_scaled[i-60:i, 0])
y_train=np.append(training_set_scaled[i, 0])
np.append
is not a list append clone. OK, you got that it returned a new value. But where's the 2nd value?
training_set_scaled[i-60:i, 0]
indexes a 2d array, a slice of rows, and the 1st column. Is that what you intended? Or is the 0
supposed to be outside []
, and be the value you add to that slice? Without info on the array and your intention we can't say.
Also you assign the result of the np.append
to X_train
. That variable is now an array, where as it started as a []
list. Why did you even initial X_train
to that list if you are just going throw that value away. Or, were you intending to do:
X_train.append(training_set_scaled[i-60:i, 0])
adding an array slice to the X_train
list?
Next you wrap the arrays in np.array
. Why? np.append
, if it works, returns an array. No need to "make" it an array again!
X_train, y_train = np.array(X_train), np.array(y_train)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
The last step presumably adds a trailing dimension to X_train
.
What happens in the next iteration? You again (attempt to) assign the np.append
result to X_train
. This throws away the X_train
you so laboriously created at the end of the previous iteration.
It looks like you've taken the list append model, and clumsily converted it to an array append. Not only doesn't it work, it is also inefficient. List append operates in-place adding references to the list. np.append
(and other np.concatenate/stack
functions), makes a new array each time, a much more expensive step. Usually we recommend sticking with list append in the loop, and make the array with one call at the end.
Fundamentally, when constructing a complex iteration like this, pay close attention to what happens to the variables as it goes through the loop. Don't make assumptions. Test pieces of code in an interactive session. A print
to check the variables, paying close attention to type and shape/dimension. If you like you can also use a debugger to test values in the loop. And again - READ the docs!