Home  >  Article  >  Backend Development  >  How to implement cosine similarity algorithm in PHP

How to implement cosine similarity algorithm in PHP

PHPz
PHPzOriginal
2023-03-31 10:08:08938browse

Understand the concepts and principles of the cosine similarity algorithm, which can be widely used in data analysis, information retrieval, machine learning and other fields. In practical applications, as a commonly used server-side programming language, how does PHP implement the cosine similarity algorithm? This article will introduce how to use PHP to implement the cosine similarity algorithm.

1. Concept and principle of cosine similarity algorithm

Cosine similarity algorithm is a common similarity calculation method, used to calculate the degree of similarity between two vectors. The core idea is to use the cosine of the angle between two vectors as their similarity.

The mathematical formula of the cosine similarity algorithm is as follows:

cosθ= A·B / |A|·|B|

where A and B are two vectors, cosθ is the cosine value between them, |A| and |B| are their module lengths respectively.

In the cosine similarity algorithm, the value range of similarity is between -1 and 1. When two vectors have the same direction, the cosine value is 1, indicating that the vectors are completely similar; when the two vectors are orthogonal, the cosine value is 0, indicating that the vectors are completely dissimilar; when the two vectors have completely opposite directions, the cosine value is - 1, means the vectors are completely opposite.

2. PHP implements cosine similarity algorithm

In PHP, we can store vectors through arrays and use built-in functions to perform calculations. Below is a sample code through which you can implement the cosine similarity algorithm in PHP.

/**

  • Implementation of cosine similarity algorithm
  • @param array $a vector A value
  • @param array $b vector B value
  • @return float cosine similarity value
    */
    function cosine_similarity($a, $b) {
    $dot_product = 0;
    $a_norm = 0;
    $b_norm = 0;

    foreach ($a as $key => $value) {

      $dot_product += $value * $b[$key];
      $a_norm += pow($value, 2);
      $b_norm += pow($b[$key], 2);

    }

    $a_norm = sqrt($a_norm) ;
    $b_norm = sqrt($b_norm);

    return $dot_product / ($a_norm * $b_norm);
    }

    // Example
    $a = [1, 2, 3, 4, 5];
    $b = [6, 7, 8, 9, 10];
    $c = [2, 3, 6, 7 , 9];

    echo cosine_similarity($a, $b) . "\n";
    echo cosine_similarity($a, $c) . "\n";
    ?>

    In the above code, the cosine_similarity function receives two vectors A and B, then traverses their key-value pairs, calculates their dot product and normalized values. Ultimately, the function returns a cosine similarity value, which can be used as a metric to evaluate the similarity between vectors.

    3. Summary

    The cosine similarity algorithm is one of the common methods to calculate the similarity of two vectors and can be widely used in data analysis, information retrieval, machine learning and other fields. In actual application, we can use PHP's built-in function to implement the cosine similarity algorithm and quickly calculate the similarity between vectors. I hope the methods and sample code introduced in this article can be helpful to readers.

    The above is the detailed content of How to implement cosine similarity algorithm in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn