- Haskell Data Analysis Cookbook
- Nishant Shukla
- 311字
- 2021-12-08 12:43:37
Comparing sparse data using cosine similarity
When a data set has multiple empty fields, comparing the distance using the Manhattan or Euclidean metrics might result in skewed results. Cosine similarity measures how closely two vectors are oriented with each other. For example, the vectors (82, 86) and (86, 82) essentially point in the same direction. In fact, their cosine similarity is equivalent to the cosine similarity between (41, 43) and (43, 41). A cosine similarity of 1 corresponds to vectors that point in the exact same direction, and 0 corresponds to vectors that are completely orthogonal to each other.

As long as the angles between the two vectors are equal, their cosine similarity is equivalent. Applying a distance metric such as the Manhattan distance or Euclidean distance in this case produces a significant difference between the two sets of data.
The cosine similarity between the two vectors is the dot product of the two vectors divided by the product of their magnitudes.

How to do it...
Create a new file, which we will call Main.hs
, and perform the following steps:
- Implement
main
to compute the cosine similarity between two lists of numbers.main :: IO () main = do let d1 = [3.5, 2, 0, 4.5, 5, 1.5, 2.5, 2] let d2 = [ 3, 0, 0, 5, 4, 2.5, 3, 0]
- Compute the cosine similarity.
let similarity = dot d1 d2 / (eLen d1 * eLen d2) print similarity
- Define the dot product and Euclidean length helper functions.
dot a b = sum $ zipWith (*) a b eLen a = sqrt $ dot a a
- Run the code to print the cosine similarity.
$ runhaskell Main.hs 0.924679432210068
See also
If the data set is not sparse, consider using the Manhattan or Euclidean distance metrics instead, as detailed in the recipes Computing the Manhattan distance and Computing the Euclidean distance.
- Web程序設(shè)計(jì)及應(yīng)用
- Google Apps Script for Beginners
- Xcode 7 Essentials(Second Edition)
- 精通搜索分析
- Swift 3 New Features
- 游戲程序設(shè)計(jì)教程
- 機(jī)器學(xué)習(xí)與R語言實(shí)戰(zhàn)
- SQL Server與JSP動態(tài)網(wǎng)站開發(fā)
- Visual Basic程序設(shè)計(jì)上機(jī)實(shí)驗(yàn)教程
- JQuery風(fēng)暴:完美用戶體驗(yàn)
- Apache Solr PHP Integration
- Mastering Concurrency in Python
- UML基礎(chǔ)與Rose建模實(shí)用教程(第三版)
- Learning Alfresco Web Scripts
- Eclipse開發(fā)(學(xué)習(xí)筆記)