1

I need to plot this surface. It turns out it was plotted in Matlab. I don’t have access to Matlab so I did the whole thing in python and got the x, y and z for the surface.

This is how it looks:

enter image description here

Is there any chance that this could be made with tikZ?

These are the coordinates:

learning rate: 
[0.0,
 0.0,
 0.0,
 0.0,
 0.005,
 0.005,
 0.005,
 0.005,
 0.01,
 0.01,
 0.01,
 0.01,
 0.015,
 0.015,
 0.015,
 0.015]

rmse:

[2.7689,
 2.7687,
 2.7688,
 2.7688,
 0.9809,
 0.9417,
 0.9268,
 0.9231,
 0.9486,
 0.9243,
 0.9306,
 0.9316,
 0.9406,
 0.934,
 0.9381,
 0.9419]

number of epochs:


[10, 20, 30, 40, 10, 20, 30, 40, 10, 20, 30, 40, 10, 20, 30, 40]

Algorithm:

enter image description here

Implementation of the algorithm:

def SGD(data):
    '''Learn the vectors p_u and q_i with SGD.
       data is a dataset containing all ratings + some useful info (e.g. number
       of items/users).
    '''

    n_factors = 10  # number of factors
    alpha = .01  # learning rate
    n_epochs = 10  # number of iteration of the SGD procedure

    # Randomly initialize the user and item factors.
    p = np.random.normal(0, .1, (data.n_users, n_factors))
    q = np.random.normal(0, .1, (data.n_items, n_factors))

    # Optimization procedure
    for _ in range(n_epochs):
        for u, i, r_ui in data.all_ratings():
            err = r_ui - np.dot(p[u], q[i])
            # Update vectors p_u and q_i
            p[u] += alpha * err * q[i]
            q[i] += alpha * err * p[u]


def estimate(u, i):
    '''Estimate rating of user u for item i.'''
    return np.dot(p[u], q[i])

mwe

\ documentclass{article}

\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}

\pgfplotsset{compat=1.3}

\begin{document}
\thispagestyle{empty}

\begin{tikzpicture}
    \begin{axis}
    \addplot3[patch,patch refines=3,shader=faceted interp,patch type=biquadratic] 
        table[z expr=x^2-y^2]
    {
        x y
        -2 -2
        2 -2
        2 2
        -2 2
        0 -2
        2 0
        0 2
        -2 0
        0 0
    };
    \end{axis}
\end{tikzpicture}
\end{document}
  • 2
    This should be possible. Look into the sagetex package which gives you a computer algebra system, called SAGE, as well as Python. I've done various examples with 2d such as here or here. A 3D using matplotlib is here. What is the function to generate the coordinates? – DJP Jul 25 '19 at 12:43
  • Thank you so much for helping me to solve this issue. I will edit the OP with the function with the whole code. Check it! – NaveganTeX Jul 25 '19 at 16:39
3

I'm having trouble with your function. It seems, as I understand it, to rely on a dataset and user ratings that I don't have. I'm looking for a multivariable function such as f(x,y)= x^2*y^2. Using this f(x,y), I have a proof of concept for your problem. xcoords = [10, 20, 30, 40] is one of your vectors, say p, and ycoords = [0,1,2,3] is the other vector q. Set the axis of tikzpicture to use your values (10,40,0,3) to get the following code.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{sagetex}
\pgfplotsset{compat=1.15} 
\begin{document}
\begin{sagesilent}
xcoords = [10, 20, 30, 40]
ycoords = [0,1,2,3]

output = ""
output += r"\begin{tikzpicture}[scale=1.0]"
output += r"\begin{axis}[xmin=%d, xmax=%d, ymin=%d, ymax=%d]"%(10,40,0,3)
output += r"\addplot3[surf,mesh/rows=%d] coordinates {"%(4)
# 4 is the number of y values
for y in ycoords:
    for x in xcoords:
        output += r"(%f, %f, %f) "%(x,y,x^2*y^2)

output += r"};"
output += r"\end{axis}"
output += r"\end{tikzpicture}"
\end{sagesilent}
\sagestr{output}
\end{document}

Here is the output running in Cocalc: enter image description here

You can adapt this example to your specific data and your f(x,y). Remember that SAGE is not part of LaTeX; the best way to use the sagetex package is through a free Cocalc account.

  • Outstanding! Thank you so much! You are a piece of work, that J can tell you! – NaveganTeX Jul 25 '19 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.