How to fix: index 122 is out of bounds for axis 0 with size 122 - index-error

I am new to coding and doing a project for my Uni. I keep on getting the following error:
index 122 is out of bounds for axis 0 with size 122
I think the error is somewhere here:
def univariate_data(dataset, start_index, end_index, history_size, target_size):
data = []
labels = []
start_index = start_index + history_size
if end_index is None:
end_index = len(dataset) - target_size
for i in range(start_index, end_index):
indices = range(i-history_size, i)
# Reshape data from (history_size,) to (history_size, 1)
data.append(np.reshape(dataset[indices], (history_size, 1)))
labels.append(dataset[i+target_size])
return np.array(data), np.array(labels)
Please let me know how to fix this.

Try this:
for i in range(start_index, end_index + 1)

Related

AttributeError: 'list' object has no attribute 'reshape'. for big frames

this is my code. and i don't know what is the problem. the my goal is read more number frames and then convert them to array and finally split them to train and test data.
def read_frames(PATH, num1, num2,seq_len,count,num):
directory = os.listdir(PATH)
for i in range(num1,num2):
source_folder = PATH + '/' +'{}'.format(directory[i])
print("appending the images is started for:", directory[i])
for item in sorted(os.listdir(source_folder), key = len):
img = Image.open(os.path.join(source_folder, item))
frames.append(item)
x.append(np.asarray(img))
count+=1
if count == seq_len:
break
this is a function that read the frames from the path and convert them to array. seq_len is the number of frames.i read the frames in 3 parts. 2 parts using the function that mentioned and other part is:
seq_len = 1624
frames = []
x = []
y = []
w=[]
count = 0
num = 0
PATH = ""
directory = os.listdir(PATH)
print("#.................reading and appending the frames for PART1 are started...........#","\n")
for i in range(0, len(directory)):
source_folder = "PATH/{}".format(directory[i])
print(num)
# print(source_folder)
print("appending the images is started for:", directory[i])
for item in sorted(os.listdir(source_folder), key = len):
# print(item)
# print(count)
img = Image.open(os.path.join(source_folder, item))
frames.append(item)
x.append(np.asarray(img))
count+=1
if count == seq_len:
break
the total number of frames that reading from 3 parts are 333060.
when running this cell:
w = np.array(x)
print('w_shape: ', np.array(w, dtype='uint8').shape)
i expected that the output be (333060,224,224,3) but it is not. and i face this error.
ValueError: setting an array element with a sequence.
when the number of frames are not more this error doesn't happening and when the number of frames are more i face with that error. i using this frames for my pre-trained cnn + lstm network.
please help me to solve that.

incompatible array types are mixed in the forward input (LinearFunction) in machine learning

I have trained a deep Q-Learning model using Chanier:
class Q_Network (chainer.Chain):
def __init__(self, input_size, hidden_size, output_size):
super (Q_Network, self).__init__ (
fc1=L.Linear (input_size, hidden_size),
fc2=L.Linear (hidden_size, hidden_size),
fc3=L.Linear (hidden_size, output_size)
)
def __call__(self, x):
h = F.relu (self.fc1 (x))
h = F.relu (self.fc2 (h))
y = self.fc3 (h)
return y
def reset(self):
self.zerograds ()
def train_dqn(env):
Q = Q_Network (input_size=env.history_t + 1, hidden_size=100, output_size=3)
Q_ast = copy.deepcopy (Q)
optimizer = chainer.optimizers.Adam ()
optimizer.setup (Q)
epoch_num = 50
step_max = len (env.data) - 1
memory_size = 200
batch_size = 20
# epsilon = 1.0
epsilon = 0.9
epsilon_decrease = 1e-3
epsilon_min = 0.1
start_reduce_epsilon = 200
train_freq = 10
update_q_freq = 20
# gamma = 0.97
gamma = 0.9
show_log_freq = 5
memory = []
total_step = 0
total_rewards = []
total_losses = []
start = time.time ()
for epoch in range (epoch_num):
pobs = env.reset ()
step = 0
done = False
total_reward = 0
total_loss = 0
while not done and step < step_max:
# select act
pact = np.random.randint (3)
if np.random.rand () > epsilon:
pact = Q (np.array (pobs, dtype=np.float32).reshape (1, -1))
pact = np.argmax (pact.data)
# act
obs, reward, done = env.step (pact)
# add memory
memory.append ((pobs, pact, reward, obs, done))
if len (memory) > memory_size:
memory.pop (0)
# train or update q
if len (memory) == memory_size:
if total_step % train_freq == 0:
shuffled_memory = np.random.permutation (memory)
memory_idx = range (len (shuffled_memory))
for i in memory_idx[::batch_size]:
batch = np.array (shuffled_memory[i:i + batch_size])
b_pobs = np.array (batch[:, 0].tolist (), dtype=np.float32).reshape (batch_size, -1)
b_pact = np.array (batch[:, 1].tolist (), dtype=np.int32)
b_reward = np.array (batch[:, 2].tolist (), dtype=np.int32)
b_obs = np.array (batch[:, 3].tolist (), dtype=np.float32).reshape (batch_size, -1)
b_done = np.array (batch[:, 4].tolist (), dtype=np.bool)
q = Q (b_pobs)
maxq = np.max (Q_ast (b_obs).data, axis=1)
target = copy.deepcopy (q.data)
for j in range (batch_size):
target[j, b_pact[j]] = b_reward[j] + gamma * maxq[j] * (not b_done[j])
Q.reset ()
loss = F.mean_squared_error (q, target)
total_loss += loss.data
loss.backward ()
optimizer.update ()
if total_step % update_q_freq == 0:
Q_ast = copy.deepcopy (Q)
# epsilon
if epsilon > epsilon_min and total_step > start_reduce_epsilon:
epsilon -= epsilon_decrease
# next step
total_reward += reward
pobs = obs
step += 1
total_step += 1
total_rewards.append (total_reward)
total_losses.append (total_loss)
if (epoch + 1) % show_log_freq == 0:
log_reward = sum (total_rewards[((epoch + 1) - show_log_freq):]) / show_log_freq
log_loss = sum (total_losses[((epoch + 1) - show_log_freq):]) / show_log_freq
elapsed_time = time.time () - start
print ('\t'.join (map (str, [epoch + 1, epsilon, total_step, log_reward, log_loss, elapsed_time])))
start = time.time ()
return Q, total_losses, total_rewards
if __name__ == "__main__":
Q, total_losses, total_rewards = train_dqn (Environment1 (train))
serializers.save_npz(r'C:\Users\willi\Desktop\dqn\dqn.model', Q)
After saved the model,I call the model again and feed data in it to let it predict:
load model:
model = Q_Network (input_size=91, hidden_size=100, output_size=3)
serializers.load_npz(r'C:\Users\willi\Desktop\dqn\dqn.model', model)
feed one row dataļ¼š
data = pd.read_csv (r'C:\Users\willi\Downloads\spyv.csv')
the data is looks like:
open high low close volume datetime
0 236.250 239.01 236.22 238.205 2327395 30600
1 238.205 240.47 238.00 239.920 1506096 30660
2 239.955 240.30 238.85 239.700 1357531 30720
3 239.690 243.33 239.66 241.650 1265604 30780
4 241.570 242.13 240.20 240.490 896000 30840
Now predict:
x = data.iloc[1].to_numpy()
y = model(x)
But the error says:
IndexError: tuple index out of range
The full error is:
IndexError Traceback (most recent call last)
<ipython-input-7-b745008aa965> in <module>
64
65 x = data.iloc[1].to_numpy()
---> 66 y = Q(x)
67
68
~\ddqn.ipynb in __call__(self, x)
~\Anaconda3\lib\site-packages\chainer\link.py in __call__(self, *args, **kwargs)
285 # forward is implemented in the child classes
286 forward = self.forward # type: ignore
--> 287 out = forward(*args, **kwargs)
288
289 # Call forward_postprocess hook
~\Anaconda3\lib\site-packages\chainer\links\connection\linear.py in forward(self, x, n_batch_axes)
181 in_size = utils.size_of_shape(x.shape[n_batch_axes:])
182 self._initialize_params(in_size)
--> 183 return linear.linear(x, self.W, self.b, n_batch_axes=n_batch_axes)
~\Anaconda3\lib\site-packages\chainer\functions\connection\linear.py in linear(x, W, b, n_batch_axes)
306 args = x, W, b
307
--> 308 y, = LinearFunction().apply(args)
309 if n_batch_axes > 1:
310 y = y.reshape(batch_shape + (-1,))
~\Anaconda3\lib\site-packages\chainer\function_node.py in apply(self, inputs)
305
306 if configuration.config.type_check:
--> 307 self._check_data_type_forward(in_data)
308
309 self.check_layout_forward(input_vars)
~\Anaconda3\lib\site-packages\chainer\function_node.py in _check_data_type_forward(self, in_data)
444 try:
445 with type_check.light_mode:
--> 446 self.check_type_forward(in_type)
447 return
448 except type_check.InvalidType:
~\Anaconda3\lib\site-packages\chainer\functions\connection\linear.py in check_type_forward(self, in_types)
27 x_type.ndim == 2,
28 w_type.ndim == 2,
---> 29 x_type.shape[1] == w_type.shape[1],
30 )
31 if type_check.eval(n_in) == 3:
IndexError: tuple index out of range
TypeError: incompatible array types are mixed in the forward input (LinearFunction).
Actual: <class 'pandas.core.frame.DataFrame'>, <class 'numpy.ndarray'>, <class 'numpy.ndarray'>
The error says that your input is a pandas.core.frame.DataFrame while your model parameters are numpy.ndarray.
You need to convert your pandas dataframe data to numpy using .to_numpy().
Probably you will face other issues with the format of the data and you will need to manipulate it to match your training examples.
x = data.iloc[1].to_numpy()
y = model(x)

IndexError: too many indices for array in numpy

I'm trying some exercise.
I looked for this problem before, but didn't find one for my problem.
This code seems to work with trainX, but not with trainY.
I have 1672 data for trainY in 1D for one neuron output.
batch_dim = trainX.shape[0]
input_dim = windowSize
hidden_dim = 6
output_dim = 1
O: batch_dim=1 with value "1672"
X = trainX[index:index+batch_dim,:]
Y = trainY[index:index+batch_dim,:]
index = index+batch_dim
The problem seems to be in the dimension. So I try to reshape it
Y = np.reshape(trainY[index:index+batch_dim,:],-1,1)
but it doesn't solve anything. The output still work, but error still there.
I just wanted the error to go away.
The variable size output:
batch_dim = 1 (value = 1672)
index = 1 (value = 0)
X : (1672,3)
Y : (1672,)
Y = trainY[index:index+batch_dim,:]
IndexError: too many indices for array

More efficient fillna(numpy)

I need an array version of a function similar to Pandas.fillna, in the forum I collected a lot of answers to create the following function, but it is still 3 times times slower than Pandas.fillna, I want to know if there is a better way to optimize, thank you.
def fillna(self,axis=None,mask=None,value=None,method='pad'):
""" array fillna
Parameters
----------
self : 1d/2d
axis : axis(0 or 1)
mask : Custom mask, or Built np.isfinite(x)
value : int
method : 'back', 'pad', 'mean'
--------
"""
x = np.asarray(self)
if mask is None: mask = np.isfinite(x)
if (not value is None)|(method=='mean'):
out = x.copy()
if x.ndim == 1:
if method=='mean':
out[~mask] = np.nanmean(x)
else: out[~mask] = value
else:
vask = ~mask * (np.nanmean(x,1)[:,None] if axis==1 else np.nanmean(x,0))
out[~mask] = vask[~mask]
else:
if axis is None: axis = 0
if x.ndim==1:
if method=='pad':
idx = np.where(mask,np.arange(mask.shape[0]),0)
np.maximum.accumulate(idx,axis=0,out=idx)
return x[idx]
elif method=='back':
idx = np.where(mask[::-1],np.arange(mask.shape[0]),0)
np.maximum.accumulate(idx,axis=0,out=idx)
return x[mask.shape[0]-idx[::-1]-1]
else: return x
if axis==1:
if method=='back': mask = mask[:, ::-1]
idx = np.where(mask,np.arange(mask.shape[1]),0)
else:
if method=='back': mask = mask[::-1,:]
idx = np.where(mask,np.arange(mask.shape[0])[:,None],0)
np.maximum.accumulate(idx,axis=axis,out=idx)
if axis==1:
if method=='back': idx = idx.shape[1]-idx[:, ::-1] - 1
out = x[np.arange(idx.shape[0])[:,None], idx]
else:
if method=='back': idx = idx.shape[0]-idx[::-1, :] - 1
out = x[idx,np.arange(idx.shape[1])]
return out

Is there any way to use bivariate colormaps in matplotlib?

In other words, I want to make a heatmap (or surface plot) where the color varies as a function of 2 variables. (Specifically, luminance = magnitude and hue = phase.) Is there any native way to do this?
Some examples of similar plots:
Several good examples of exactly(?) what I want to do.
More examples from astronomy, but with non-perceptual hue
Edit: This is what I did with it: https://github.com/endolith/complex_colormap
imshow can take an array of [r, g, b] entries. So you can convert the absolute values to intensities and phases - to hues.
I will use as an example complex numbers, because for it it makes the most sense. If needed, you can always add numpy arrays Z = X + 1j * Y.
So for your data Z you can use e.g.
imshow(complex_array_to_rgb(Z))
where (EDIT: made it quicker and nicer thanks to this suggestion)
def complex_array_to_rgb(X, theme='dark', rmax=None):
'''Takes an array of complex number and converts it to an array of [r, g, b],
where phase gives hue and saturaton/value are given by the absolute value.
Especially for use with imshow for complex plots.'''
absmax = rmax or np.abs(X).max()
Y = np.zeros(X.shape + (3,), dtype='float')
Y[..., 0] = np.angle(X) / (2 * pi) % 1
if theme == 'light':
Y[..., 1] = np.clip(np.abs(X) / absmax, 0, 1)
Y[..., 2] = 1
elif theme == 'dark':
Y[..., 1] = 1
Y[..., 2] = np.clip(np.abs(X) / absmax, 0, 1)
Y = matplotlib.colors.hsv_to_rgb(Y)
return Y
So, for example:
Z = np.array([[3*(x + 1j*y)**3 + 1/(x + 1j*y)**2
for x in arange(-1,1,0.05)] for y in arange(-1,1,0.05)])
imshow(complex_array_to_rgb(Z, rmax=5), extent=(-1,1,-1,1))
imshow(complex_array_to_rgb(Z, rmax=5, theme='light'), extent=(-1,1,-1,1))
imshow will take an NxMx3 (rbg) or NxMx4 (grba) array so you can do your color mapping 'by hand'.
You might be able to get a bit of traction by sub-classing Normalize to map your vector to a scaler and laying out a custom color map very cleverly (but I think this will end up having to bin one of your dimensions).
I have done something like this (pdf link, see figure on page 24), but the code is in MATLAB (and buried someplace in my archives).
I agree a bi-variate color map would be useful (primarily for representing very dense vector fields where your kinda up the creek no matter what you do).
I think the obvious extension is to let color maps take complex arguments. It would require specialized sub-classes of Normalize and Colormap and I am going back and forth on if I think it would be a lot of work to implement. I suspect if you get it working by hand it will just be a matter of api wrangling.
I created an easy to use 2D colormap class, that takes 2 NumPy arrays and maps them to an RGB image, based on a reference image.
I used #GjjvdBurg's answer as a starting point. With a bit of work, this could still be improved, and possibly turned into a proper Python module - if you want, feel free to do so, I grant you all credits.
TL;DR:
# read reference image
cmap_2d = ColorMap2D('const_chroma.jpeg', reverse_x=True) # , xclip=(0,0.9))
# map the data x and y to the RGB space, defined by the image
rgb = cmap_2d(data_x, data_y)
# generate a colorbar image
cbar_rgb = cmap_2d.generate_cbar()
The ColorMap2D class:
class ColorMap2D:
def __init__(self, filename: str, transpose=False, reverse_x=False, reverse_y=False, xclip=None, yclip=None):
"""
Maps two 2D array to an RGB color space based on a given reference image.
Args:
filename (str): reference image to read the x-y colors from
rotate (bool): if True, transpose the reference image (swap x and y axes)
reverse_x (bool): if True, reverse the x scale on the reference
reverse_y (bool): if True, reverse the y scale on the reference
xclip (tuple): clip the image to this portion on the x scale; (0,1) is the whole image
yclip (tuple): clip the image to this portion on the y scale; (0,1) is the whole image
"""
self._colormap_file = filename or COLORMAP_FILE
self._img = plt.imread(self._colormap_file)
if transpose:
self._img = self._img.transpose()
if reverse_x:
self._img = self._img[::-1,:,:]
if reverse_y:
self._img = self._img[:,::-1,:]
if xclip is not None:
imin, imax = map(lambda x: int(self._img.shape[0] * x), xclip)
self._img = self._img[imin:imax,:,:]
if yclip is not None:
imin, imax = map(lambda x: int(self._img.shape[1] * x), yclip)
self._img = self._img[:,imin:imax,:]
if issubclass(self._img.dtype.type, np.integer):
self._img = self._img / 255.0
self._width = len(self._img)
self._height = len(self._img[0])
self._range_x = (0, 1)
self._range_y = (0, 1)
#staticmethod
def _scale_to_range(u: np.ndarray, u_min: float, u_max: float) -> np.ndarray:
return (u - u_min) / (u_max - u_min)
def _map_to_x(self, val: np.ndarray) -> np.ndarray:
xmin, xmax = self._range_x
val = self._scale_to_range(val, xmin, xmax)
rescaled = (val * (self._width - 1))
return rescaled.astype(int)
def _map_to_y(self, val: np.ndarray) -> np.ndarray:
ymin, ymax = self._range_y
val = self._scale_to_range(val, ymin, ymax)
rescaled = (val * (self._height - 1))
return rescaled.astype(int)
def __call__(self, val_x, val_y):
"""
Take val_x and val_y, and associate the RGB values
from the reference picture to each item. val_x and val_y
must have the same shape.
"""
if val_x.shape != val_y.shape:
raise ValueError(f'x and y array must have the same shape, but have {val_x.shape} and {val_y.shape}.')
self._range_x = (np.amin(val_x), np.amax(val_x))
self._range_y = (np.amin(val_y), np.amax(val_y))
x_indices = self._map_to_x(val_x)
y_indices = self._map_to_y(val_y)
i_xy = np.stack((x_indices, y_indices), axis=-1)
rgb = np.zeros((*val_x.shape, 3))
for indices in np.ndindex(val_x.shape):
img_indices = tuple(i_xy[indices])
rgb[indices] = self._img[img_indices]
return rgb
def generate_cbar(self, nx=100, ny=100):
"generate an image that can be used as a 2D colorbar"
x = np.linspace(0, 1, nx)
y = np.linspace(0, 1, ny)
return self.__call__(*np.meshgrid(x, y))
Usage:
Full example, using the constant chroma reference taken from here as a screenshot:
# generate data
x = y = np.linspace(-2, 2, 300)
xx, yy = np.meshgrid(x, y)
ampl = np.exp(-(xx ** 2 + yy ** 2))
phase = (xx ** 2 - yy ** 2) * 6 * np.pi
data = ampl * np.exp(1j * phase)
data_x, data_y = np.abs(data), np.angle(data)
# Here is the 2D colormap part
cmap_2d = ColorMap2D('const_chroma.jpeg', reverse_x=True) # , xclip=(0,0.9))
rgb = cmap_2d(data_x, data_y)
cbar_rgb = cmap_2d.generate_cbar()
# plot the data
fig, plot_ax = plt.subplots(figsize=(8, 6))
plot_extent = (x.min(), x.max(), y.min(), y.max())
plot_ax.imshow(rgb, aspect='auto', extent=plot_extent, origin='lower')
plot_ax.set_xlabel('x')
plot_ax.set_ylabel('y')
plot_ax.set_title('data')
# create a 2D colorbar and make it fancy
plt.subplots_adjust(left=0.1, right=0.65)
bar_ax = fig.add_axes([0.68, 0.15, 0.15, 0.3])
cmap_extent = (data_x.min(), data_x.max(), data_y.min(), data_y.max())
bar_ax.imshow(cbar_rgb, extent=cmap_extent, aspect='auto', origin='lower',)
bar_ax.set_xlabel('amplitude')
bar_ax.set_ylabel('phase')
bar_ax.yaxis.tick_right()
bar_ax.yaxis.set_label_position('right')
for item in ([bar_ax.title, bar_ax.xaxis.label, bar_ax.yaxis.label] +
bar_ax.get_xticklabels() + bar_ax.get_yticklabels()):
item.set_fontsize(7)
plt.show()
I know this is an old post, but want to help out others that may arrive late. Below is a python function to implement complex_to_rgb from sage. Note: This implementation isn't optimal, but it is readable. See links: (examples)(source code)
Code:
import numpy as np
def complex_to_rgb(z_values):
width = z_values.shape[0]
height = z_values.shape[1]
rgb = np.zeros(shape=(width, height, 3))
for i in range(width):
row = z_values[i]
for j in range(height):
# define value, real(value), imag(value)
zz = row[j]
x = np.real(zz)
y = np.imag(zz)
# define magnitued and argument
magnitude = np.hypot(x, y)
arg = np.arctan2(y, x)
# define lighness
lightness = np.arctan(np.log(np.sqrt(magnitude) + 1)) * (4 / np.pi) - 1
if lightness < 0:
bot = 0
top = 1 + lightness
else:
bot = lightness
top = 1
# define hue
hue = 3 * arg / np.pi
if hue < 0:
hue += 6
# set ihue and use it to define rgb values based on cases
ihue = int(hue)
# case 1
if ihue == 0:
r = top
g = bot + hue * (top - bot)
b = bot
# case 2
elif ihue == 1:
r = bot + (2 - hue) * (top - bot)
g = top
b = bot
# case 3
elif ihue == 2:
r = bot
g = top
b = bot + (hue - 2) * (top - bot)
# case 4
elif ihue == 3:
r = bot
g = bot + (4 - hue) * (top - bot)
b = top
# case 5
elif ihue == 4:
r = bot + (hue - 4) * (top - bot)
g = bot
b = top
# case 6
else:
r = top
g = bot
b = bot + (6 - hue) * (top - bot)
# set rgb array values
rgb[i, j, 0] = r
rgb[i, j, 1] = g
rgb[i, j, 2] = b
return rgb