Spaces:
Sleeping
Sleeping
update (examples): adjusted to the new query approach
Browse files- examples/README.md +40 -105
examples/README.md
CHANGED
|
@@ -20,135 +20,70 @@ SELECT * FROM disease_embedding LIMIT 3;
|
|
| 20 |
* Similarity: 0.9657052160858545
|
| 21 |
|
| 22 |
~~~sql
|
| 23 |
-
WITH
|
| 24 |
-
d1 AS (
|
| 25 |
-
SELECT embedding
|
| 26 |
-
FROM disease d, disease_embedding de
|
| 27 |
-
WHERE de.disease_id = d.disease_id
|
| 28 |
-
AND d.name = 'colon cancer'
|
| 29 |
-
),
|
| 30 |
-
d2 AS (
|
| 31 |
-
SELECT embedding
|
| 32 |
-
FROM disease d, disease_embedding de
|
| 33 |
-
WHERE de.disease_id = d.disease_id
|
| 34 |
-
AND d.name = 'liver cancer'
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
SELECT
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
/
|
| 45 |
-
(
|
| 46 |
-
sqrt(list_sum(list_transform(d1.embedding, x -> x * x)))
|
| 47 |
-
*
|
| 48 |
-
sqrt(list_sum(list_transform(d2.embedding, x -> x * x)))
|
| 49 |
-
)
|
| 50 |
-
AS cosine_similarity
|
| 51 |
-
FROM d1, d2;
|
| 52 |
~~~
|
| 53 |
|
| 54 |
### Similarity between `cirrhosis of the liver` and `liver cancer`:
|
| 55 |
* Similarity: 0.9498555648670207
|
| 56 |
|
| 57 |
~~~sql
|
| 58 |
-
WITH
|
| 59 |
-
d1 AS (
|
| 60 |
-
SELECT embedding
|
| 61 |
-
FROM disease d, disease_embedding de
|
| 62 |
-
WHERE de.disease_id = d.disease_id
|
| 63 |
-
AND d.name = 'cirrhosis of the liver'
|
| 64 |
-
),
|
| 65 |
-
d2 AS (
|
| 66 |
-
SELECT embedding
|
| 67 |
-
FROM disease d, disease_embedding de
|
| 68 |
-
WHERE de.disease_id = d.disease_id
|
| 69 |
-
AND d.name = 'liver cancer'
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
SELECT
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
/
|
| 80 |
-
(
|
| 81 |
-
sqrt(list_sum(list_transform(d1.embedding, x -> x * x)))
|
| 82 |
-
*
|
| 83 |
-
sqrt(list_sum(list_transform(d2.embedding, x -> x * x)))
|
| 84 |
-
)
|
| 85 |
-
AS cosine_similarity
|
| 86 |
-
FROM d1, d2;
|
| 87 |
~~~
|
| 88 |
|
| 89 |
### Similarity between `common cold` and `liver cancer`:
|
| 90 |
* Similarity: 0.7145078607670947
|
| 91 |
|
| 92 |
~~~sql
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
WHERE de.disease_id = d.disease_id
|
| 104 |
-
AND d.name = 'liver cancer'
|
| 105 |
-
)
|
| 106 |
|
|
|
|
| 107 |
SELECT
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
sqrt(list_sum(list_transform(d1.embedding, x -> x * x)))
|
| 117 |
-
*
|
| 118 |
-
sqrt(list_sum(list_transform(d2.embedding, x -> x * x)))
|
| 119 |
-
)
|
| 120 |
-
AS cosine_similarity
|
| 121 |
-
FROM d1, d2;
|
| 122 |
~~~
|
| 123 |
|
| 124 |
### Rank Diseases by Similarity to `common cold`:
|
| 125 |
|
| 126 |
~~~sql
|
| 127 |
-
WITH target AS (
|
| 128 |
-
SELECT embedding
|
| 129 |
-
FROM disease d, disease_embedding de
|
| 130 |
-
WHERE de.disease_id = d.disease_id
|
| 131 |
-
AND d.name = 'common cold'
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
SELECT
|
| 135 |
d.name,
|
| 136 |
-
(
|
| 137 |
-
list_sum(
|
| 138 |
-
list_transform(
|
| 139 |
-
list_zip(target.embedding, de.embedding),
|
| 140 |
-
x -> x[1] * x[2]
|
| 141 |
-
)
|
| 142 |
-
)
|
| 143 |
-
/
|
| 144 |
(
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
| 149 |
) AS similarity
|
| 150 |
-
FROM disease_embedding de
|
| 151 |
-
|
| 152 |
-
target
|
| 153 |
ORDER BY similarity DESC;
|
| 154 |
~~~
|
|
|
|
| 20 |
* Similarity: 0.9657052160858545
|
| 21 |
|
| 22 |
~~~sql
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
SELECT
|
| 24 |
+
cosine(e1.embedding, e2.embedding) AS similarity
|
| 25 |
+
FROM disease_embedding e1, disease d1, disease_embedding e2, disease d2
|
| 26 |
+
WHERE d1.disease_id = e1.disease_id
|
| 27 |
+
AND d2.disease_id = e2.disease_id
|
| 28 |
+
AND d1.name = 'colon cancer'
|
| 29 |
+
AND d2.name = 'liver cancer';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
~~~
|
| 31 |
|
| 32 |
### Similarity between `cirrhosis of the liver` and `liver cancer`:
|
| 33 |
* Similarity: 0.9498555648670207
|
| 34 |
|
| 35 |
~~~sql
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
SELECT
|
| 37 |
+
cosine(e1.embedding, e2.embedding) AS similarity
|
| 38 |
+
FROM disease_embedding e1, disease d1, disease_embedding e2, disease d2
|
| 39 |
+
WHERE d1.disease_id = e1.disease_id
|
| 40 |
+
AND d2.disease_id = e2.disease_id
|
| 41 |
+
AND d1.name = 'cirrhosis of the liver'
|
| 42 |
+
AND d2.name = 'liver cancer';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
~~~
|
| 44 |
|
| 45 |
### Similarity between `common cold` and `liver cancer`:
|
| 46 |
* Similarity: 0.7145078607670947
|
| 47 |
|
| 48 |
~~~sql
|
| 49 |
+
SELECT
|
| 50 |
+
cosine(e1.embedding, e2.embedding) AS similarity
|
| 51 |
+
FROM disease_embedding e1, disease d1, disease_embedding e2, disease d2
|
| 52 |
+
WHERE d1.disease_id = e1.disease_id
|
| 53 |
+
AND d2.disease_id = e2.disease_id
|
| 54 |
+
AND d1.name = 'common cold'
|
| 55 |
+
AND d2.name = 'liver cancer';
|
| 56 |
+
~~~
|
| 57 |
+
|
| 58 |
+
### Rank diseases by symptoms `fever` and `cough`:
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
~~~sql
|
| 61 |
SELECT
|
| 62 |
+
d.name,
|
| 63 |
+
cosine(
|
| 64 |
+
embed_symptoms(['fever','cough']),
|
| 65 |
+
de.embedding
|
| 66 |
+
) AS similarity
|
| 67 |
+
FROM disease d, disease_embedding de
|
| 68 |
+
WHERE d.disease_id = de.disease_id
|
| 69 |
+
ORDER BY similarity DESC;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
~~~
|
| 71 |
|
| 72 |
### Rank Diseases by Similarity to `common cold`:
|
| 73 |
|
| 74 |
~~~sql
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
SELECT
|
| 76 |
d.name,
|
| 77 |
+
cosine(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
(
|
| 79 |
+
SELECT de.embedding
|
| 80 |
+
FROM disease_embedding de
|
| 81 |
+
JOIN disease d2 ON d2.disease_id = de.disease_id
|
| 82 |
+
WHERE d2.name = 'common cold'
|
| 83 |
+
),
|
| 84 |
+
de.embedding
|
| 85 |
) AS similarity
|
| 86 |
+
FROM disease d, disease_embedding de
|
| 87 |
+
WHERE d.disease_id = de.disease_id
|
|
|
|
| 88 |
ORDER BY similarity DESC;
|
| 89 |
~~~
|