Util - Yuipas/Nature.js GitHub Wiki

argMax()

  • Description: returns the index of the maximum value in an array.

  • Example(s):

    const marks = [6.14, 9.66, 4.97, 6.58, 6.22, 3.4, 3.06];
    const index = nt.util.argMax(marks); // 1
    const value = marks[index]; // 9.66
    
  • Syntax: nature.util.argMax(list)

  • Parameters:

    • list: number array.
  • Returns: integer


max()

  • Description: returns the maximum value in an array.

  • Example(s):

    const marks = [6.14, 9.66, 4.97, 6.58, 6.22, 3.4, 3.06];
    const value = nt.util.max(marks); // 9.66 
    const index = marks.indexOf(value); // 1  
    
  • Syntax: nature.util.max(list)

  • Parameters:

    • list: number array.
  • Returns: number


random()

  • Description: if arg1 is a an array, the function returns a random object inside that array, if arg1 is a number, it returns a random value between arg1 and arg2.

  • Example(s):

    let list = [];
    for(let i = 0; i < 10; i++) {
      list.push(Math.floor(nt.util.random(2, 10)));
    }
    
    nt.util.print(
       list
    );
    
    /*
     * Array(10)
     * โ”œโ”€ 3
     * โ”œโ”€ 5
     * โ”œโ”€ 6
     * โ”œโ”€ 9
     * โ”œโ”€ 7
     * โ”œโ”€ 6
     * โ”œโ”€ 4
     * โ”œโ”€ 8
     * โ”œโ”€ 3
     * โ””โ”€ 7
     */
    
    const trainingData = [
           [0, 0],
           [0, 1],
           [1, 0],
           [1, 1],
    ];
              
    console.log(nt.util.random(trainingData)); // [0, 1]
    
  • Syntax: nature.util.random(arg1, arg2)

  • Parameters:

    • arg1: number or array
    • arg2: number
  • Returns: object (if arg1 is an array) or number.


oneHot()

  • Description: returns a oneHot array.

  • Example(s):

    let label = 3;
    let targets = nt.util.oneHot(label, 10);
    nt.util.print(targets);
    /* Array(10)
     * โ”œโ”€ 0
     * โ”œโ”€ 0
     * โ”œโ”€ 0
     * โ”œโ”€ 1
     * โ”œโ”€ 0
     * โ”œโ”€ 0
     * โ”œโ”€ 0
     * โ”œโ”€ 0
     * โ”œโ”€ 0
     * โ””โ”€ 0
     */
    
  • Syntax: nature.util.oneHot(index, length)

  • Parameters:

    • index: index to be one.
    • length: length of the array.
  • Returns: array


clone()

  • Description: returns a deep copy of an object.

  • Example(s):

    let profile = {name: 'Yuipas', http: 'https://github.com/Yuipas'};
    let shallow = profile;
    let deep = nature.util.clone(profile);
    
    shallow.name = 'Elon Musk';
    console.log(profile.name); // Elon Musk
    console.log(deep.name);    // Yuipas
    
  • Syntax: nature.util.clone(obj)

  • Parameters:

    • obj: object to be copied.
  • Returns: object.


isEqual()

  • Description: checks if both arrays are equal.

  • Example(s):

    let a1 = [4, [1, 1]];
    let a2 = [4, 1, 1];
    let a3 = [4, [1, 1]];
    
    nt.util.isEqual(a1, a2) // false
    nt.util.isEqual(a1, a3) // true
    nt.util.isEqual(a2, a3) // false
    
  • Syntax: nature.util.isEqual(a, b)

  • Parameters:

    • a: array.
    • b: array.
  • Returns: boolean.


print()

  • Description: prints the generated tree of an array.

  • Example(s):

    let a1 = [4, [1, 1], 0];
    let a2 = [4, 1, 1];
    
    nt.util.print(
          a1,
          a2,
    );
    /*
     * Array(3)
     *  โ”œโ”€ 4
     *  โ”‚
     *  โ”œโ”€โ”ฌ Array(2):
     *  โ”‚ยทโ”œโ”€ 1
     *  โ”‚ยทโ””โ”€ 1
     *  โ””โ”€ 0
    
     * Array(3)
     *  โ”œโ”€ 4
     *  โ”œโ”€ 1
     *  โ””โ”€ 1
     */
    
  • Syntax: nature.util.print(array, ..., ...)

  • Parameters: only arrays.

  • Returns: undefined.


matMul()

  • Description: calculates the multiplication of two matrices. See Matrix Multiplication

  • Example(s):

    let errors = [0.2, -0.3](/Yuipas/Nature.js/wiki/0.2,--0.3);
    let weights = [4.2, 2.1], [-1.9, 0.4](/Yuipas/Nature.js/wiki/4.2,-2.1],-[-1.9,-0.4); 
    let propagatedErrors = nt.util.matMul(errors, weights);
    
    nature.util.print(propagatedErrors);
    /*
     * Array(1)
     *  โ”‚
     *  โ””โ”€โ”ฌ Array(2):
     *  ยทยทโ”œโ”€ 1.41
     *  ยทยทโ””โ”€ 0.3
     */
    
  • Syntax: nature.util.matMul(e, p)

  • Parameters:

    • e: array with n columns.
    • p: array with n rows.
  • Returns: array.