'operands could not be broadcast together with remapped shapes [original->remapped]: (1000,) and requested shape (1000,1)

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

observations = 1000
xs = np.random.uniform(low=-10, high=10, size=(observations,1))
zs = np.random.uniform(-10, 10, (observations,1))

inputs = np.column_stack((xs,zs))

noise = np.random.uniform(-1, 1, (observations,1))
targets = 2*xs - 3*zs + 5 + noise

targets = targets.reshape(observations,)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')
ax.plot(xs, zs, targets)

ax.set_xlabel('xs')
ax.set_ylabel('zs')
ax.set_zlabel('Targets')

ax.view_init(azim=100)

plt.show()

targets = targets.reshape(observations,1)

operands could not be broadcast together with remapped shapes [original->remapped]: (1000,) and requested shape (1000,1)



Solution 1:[1]

I think your idea was:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

observations = 1000
xs = np.random.uniform(low=-10, high=10, size=(observations,1))
zs = np.random.uniform(-10, 10, (observations,1))

inputs = np.column_stack((xs,zs))

xxs , zzs = np.meshgrid(xs, zs)
noise = np.random.uniform(-1, 1, (observations,observations))

targets = 2*xxs - 3*zzs + 5 + noise

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(xxs, zzs, targets, alpha=.1)

ax.set_xlabel('xs')
ax.set_ylabel('zs')
ax.set_zlabel('Targets')

ax.view_init(azim=100)

plt.show()

output:

enter image description here

otherwise you can simply replace ax.plot(xs, zs, targets) with ax.scatter(xs.ravel(), zs.ravel(), targets) in you original code. In this case the output is:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Salvatore Daniele Bianco