-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #608 from wjohnsto/master
Vector Search Tutorial
- Loading branch information
Showing
20 changed files
with
1,015 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
docs/howtos/solutions/vector/getting-started-vector/chart/common.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Sample data | ||
const products = [ | ||
{ | ||
name: 'Puma Men Race Black Watch', | ||
price: 150, | ||
quality: 5, | ||
popularity: 8, | ||
}, | ||
{ | ||
name: 'Puma Men Top Fluctuation Red Black Watch', | ||
price: 180, | ||
quality: 7, | ||
popularity: 6, | ||
}, | ||
{ | ||
name: 'Inkfruit Women Behind Cream Tshirt', | ||
price: 5, | ||
quality: 9, | ||
popularity: 7, | ||
}, | ||
]; | ||
|
||
const dataWithAttributes = products.map((product) => ({ | ||
x: product.price, | ||
y: product.quality, | ||
label: product.name, | ||
})); | ||
|
||
const product1Point = { x: products[0].price, y: products[0].quality }; | ||
const product2Point = { x: products[1].price, y: products[1].quality }; |
95 changes: 95 additions & 0 deletions
95
docs/howtos/solutions/vector/getting-started-vector/chart/cosine.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-return */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-call */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-argument */ | ||
|
||
const ctxCosine = document | ||
.getElementById('productChartCosine') | ||
.getContext('2d'); | ||
|
||
function cosineSimilarity(point1, point2) { | ||
let dotProduct = point1.x * point2.x + point1.y * point2.y; | ||
let magnitudePoint1 = Math.sqrt( | ||
Math.pow(point1.x, 2) + Math.pow(point1.y, 2), | ||
); | ||
let magnitudePoint2 = Math.sqrt( | ||
Math.pow(point2.x, 2) + Math.pow(point2.y, 2), | ||
); | ||
return dotProduct / (magnitudePoint1 * magnitudePoint2); | ||
} | ||
|
||
const cosineSim = cosineSimilarity(product1Point, product2Point); | ||
|
||
const scatterChartCosine = new Chart(ctxCosine, { | ||
type: 'scatter', | ||
data: { | ||
datasets: [ | ||
{ | ||
label: 'Products', | ||
data: dataWithAttributes, | ||
pointBackgroundColor: ['black', 'red', 'bisque'], | ||
pointRadius: 5, | ||
}, | ||
{ | ||
label: 'Vector for Product-1', | ||
data: [{ x: 0, y: 0 }, product1Point], | ||
showLine: true, | ||
fill: false, | ||
borderColor: 'black', | ||
pointRadius: [0, 5], | ||
lineTension: 0, | ||
}, | ||
{ | ||
label: 'Vector for Product-2', | ||
data: [{ x: 0, y: 0 }, product2Point], | ||
showLine: true, | ||
fill: false, | ||
borderColor: 'red', | ||
pointRadius: [0, 5], | ||
lineTension: 0, | ||
}, | ||
], | ||
}, | ||
options: { | ||
responsive: true, | ||
plugins: { | ||
legend: { | ||
position: 'top', | ||
}, | ||
title: { | ||
display: true, | ||
text: `Cosine Similarity between Product-1 and Product-2 is ${cosineSim}`, | ||
}, | ||
}, | ||
scales: { | ||
x: { | ||
type: 'linear', | ||
position: 'bottom', | ||
title: { | ||
display: true, | ||
text: 'Price ($)', | ||
}, | ||
ticks: { | ||
beginAtZero: true, | ||
}, | ||
min: 0, // Ensure it starts from 0 | ||
}, | ||
y: { | ||
title: { | ||
display: true, | ||
text: 'Quality (1-10)', | ||
}, | ||
ticks: { | ||
beginAtZero: true, | ||
}, | ||
min: 0, // Ensure it starts from 0 | ||
}, | ||
}, | ||
tooltips: { | ||
callbacks: { | ||
title: function (tooltipItem, data) { | ||
return data.datasets[0].data[tooltipItem[0].index].label; | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); |
Oops, something went wrong.