0

(working in Jupyter Notebooks)

So, when I use the code below, I'm able to plot the surface with no errors:

# batch gradient descent setup
xInitialValue = 1.8
yInitialValue = 1.0
xyValuesArray = gradientDescent(xInitialValue, yInitialValue, xPartialDerivative, yPartialDerivative)


# plot gradient descent algorithm


# fig is a figure object (container) that holds data to represent the chart
fig = pyplot.figure(figsize = [15, 10])


# axis is a variable that holds the axis for 3D representation
# gca = get current axis
# axis -> x, y and z
axis = fig.gca(projection = '3d')
axis.plot_surface(xAxisValues,
                  yAxisValues,
                  costFunction(xAxisValues, yAxisValues),
                  cmap = colorMap.coolwarm,
                  alpha = 0.4)


# set the axis labels
axis.set_xlabel('x', fontsize = 21)
axis.set_ylabel('y', fontsize = 21)
axis.set_zlabel('z = costFunction(x, y)', fontsize = 21)


# show
pyplot.show()


However, when I try to plot some points in the same chart, I get an error:

# batch gradient descent setup
xInitialValue = 1.8
yInitialValue = 1.0
xyValuesArray = gradientDescent(xInitialValue, yInitialValue, xPartialDerivative, yPartialDerivative)


# plot gradient descent algorithm


# fig is a figure object (container) that holds data to represent the chart
fig = pyplot.figure(figsize = [15, 10])


# axis is a variable that holds the axis for 3D representation
# gca = get current axis
# axis -> x, y and z
axis = fig.gca(projection = '3d')
axis.plot_surface(xAxisValues,
                  yAxisValues,
                  costFunction(xAxisValues, yAxisValues),
                  cmap = colorMap.coolwarm,
                  alpha = 0.4)
axis.scatter(xyValuesArray[:, 0],
             xyValuesArray[:, 1],
             costFunction(xyValuesArray[:, 0], xyValuesArray[:, 1]),
             s=50,
             color='red')


# set the axis labels
axis.set_xlabel('x', fontsize = 21)
axis.set_ylabel('y', fontsize = 21)
axis.set_zlabel('z = costFunction(x, y)', fontsize = 21)


# show
pyplot.show()


The error being the following:

ValueError: Invalid RGBA argument: masked_array(data=[1.0, 0.0, 0.0, 1.0],
             mask=False,
       fill_value='?',
            dtype=object)

<Figure size 1080x720 with 1 Axes>


What am I doing wrong?

Thank you in advance!

If more context is needed I'd be happy to provide it :)

doublethink13
  • 633
  • 8
  • 19

1 Answers1

0

After doing some more digging in Stack Overflow I came across an answer that solved my problem, in this link.

Apparently, there is a problem in my environment (Ubuntu 18.04), making the data types, floats, inside my xyValuesArray different from the values returned from my costFunction (they end up being different types of floats).

xyValuesArray is a numpy.array, and the values inside of it are calculated through sympy, using diff() and then evalf(). The costFunction returns "normal" python data. I've added those modules to the tag, as the problem may reside there; if someone has a better explanation of the problem, I'd love to read it :)

If I add the following lines of code, making the float data type the same in the three different lists, then all is okay in the world:

# format data, so that the data type inside all three different is the same
x = [float(i) for i in xyValuesArray[:, 0]]
y = [float(i) for i in xyValuesArray[:, 1]]
z = [float(i) for i in costFunction(xyValuesArray[:, 0], xyValuesArray[:, 1])]


# scatter function
axis.scatter(x, y, z, s=50, color='red')


My amazing chart:

chart.png

doublethink13
  • 633
  • 8
  • 19