run example from github library

1

I'm trying to try a library link

this is the code which is on the page

// include the library
<script src="./svmjs/lib/svm.js"></script>
<script>
data = [[0,0], [0,1], [1,0], [1,1]];
labels = [-1, 1, 1, -1];
svm = new svmjs.SVM();
svm.train(data, labels, {C: 1.0}); // C is a parameter to SVM
testlabels = svm.predict(testdata);
</script>

I do not know how to make it work, I get this error

  

ReferenceError: svmjs is not defined

    
asked by hubman 24.11.2016 в 01:40
source

2 answers

2

One is to download it and use it in that relative path ... The other is to use the raw file.

  • Go to this link: link
  • Then touch where it says raw.
  • It will take you to this link.

    link

    Then all you have to do is change this:

    <script src="./svmjs/lib/svm.js"></script>
    

    ... for this:

    <script src="https://raw.githubusercontent.com/karpathy/svmjs/master/lib/svm.js"></script>
    

    That, only if it is not downloaded, and if you do not want to use relative routes ... It is likely that this way the CORS will jump ... In that case, the best way is to download it.

        
    answered by 24.11.2016 в 02:27
    2

    I downloaded the svm.js file to test locally, my file structure is as follows:

    SVM // directorio raíz
    ---- index.html
    ---- svm.js
    

    This is my index.html

    <!DOCTYPE html>
    <html>
    <head>
        <title>SVM</title>
        <script type="text/javascript" src="svm.js"></script> 
    </head>
    <body>
    
    <script type="text/javascript">
        data = [[0,0], [0,1], [1,0], [1,1]];
        labels = [-1, 1, 1, -1];
        svm = new svmjs.SVM();
        svm.train(data, labels, {C: 1.0}); // C is a parameter to SVM
        testlabels = svm.predict(testdata);
    </script>
    </body>
    </html>
    

    It worked for me correctly and I could not reproduce your error, however, he threw me:

      

    Uncaught ReferenceError: testdata is not defined (...)

    I corrected it by changing this line of code:

    testlabels = svm.predict(testdata);
    

    for this:

    testlabels = svm.predict(data);
    

    Try trying it as I am explaining to you in my answer to see how it is

        
    answered by 24.11.2016 в 02:44