what should one specify when creating a graphlab recommender model such that the item that a user already own is not recommended to him again? Can this be done directly by specifying certain parameters or do I need to write a recommender from scratch.? data would look something like this
| user_id | item_id | othercolumns |
|:-----------|------------:|:------------:|
| 1 | 21 | This |
| 2 | 22 | column |
| 1 | 23 | will |
| 3 | 24 | hold |
| 2 | 25 | other |
| 1 | 26 | values |
Since item 21,23 and 26 are already owned by user 1 this item should not be recommended to him.
This behaviour is controlled by the exclude_known parameter of the recommender.recommend method (doc).
exclude_known : bool, optional
By default, all user-item interactions previously seen in the training
data, or in any new data provided using new_observation_data.., are
excluded from the recommendations. Passing in exclude_known = False
overrides this behavior.
Example
>>> import graphlab as gl
>>> sf = gl.SFrame({'user_id':[1,2,1,3,2,1], 'item_id':[21,22,23,24,25,26]})
>>> print sf
+---------+---------+
| item_id | user_id |
+---------+---------+
| 21 | 1 |
| 22 | 2 |
| 23 | 1 |
| 24 | 3 |
| 25 | 2 |
| 26 | 1 |
+---------+---------+
[6 rows x 2 columns]
>>> rec_model = gl.recommender.create(sf)
>>> # we recommend items not owned by user
>>> rec_wo_own_item = rec_model.recommend(sf['user_id'].unique())
>>> rec_wo_own_item.sort('user_id').print_rows(100)
+---------+---------+----------------+------+
| user_id | item_id | score | rank |
+---------+---------+----------------+------+
| 1 | 22 | 0.0 | 1 |
| 1 | 24 | 0.0 | 2 |
| 1 | 25 | 0.0 | 3 |
| 2 | 21 | 0.0 | 1 |
| 2 | 23 | 0.0 | 2 |
| 2 | 24 | 0.0 | 3 |
| 2 | 26 | 0.0 | 4 |
| 3 | 21 | 0.333333333333 | 1 |
| 3 | 23 | 0.333333333333 | 2 |
| 3 | 26 | 0.333333333333 | 3 |
| 3 | 22 | 0.166666666667 | 4 |
| 3 | 25 | 0.166666666667 | 5 |
+---------+---------+----------------+------+
[12 rows x 4 columns]
>>> # we recommend items owned by user
>>> rec_w_own_item = rec_model.recommend(sf['user_id'].unique(), exclude_known=False)
>>> rec_w_own_item.sort('user_id').print_rows(100)
+---------+---------+----------------+------+
| user_id | item_id | score | rank |
+---------+---------+----------------+------+
| 1 | 21 | 0.666666666667 | 1 |
| 1 | 23 | 0.666666666667 | 2 |
| 1 | 26 | 0.666666666667 | 3 |
| 1 | 22 | 0.0 | 4 |
| 1 | 24 | 0.0 | 5 |
| 1 | 25 | 0.0 | 6 |
| 2 | 26 | 0.0 | 6 |
| 2 | 24 | 0.0 | 5 |
| 2 | 23 | 0.0 | 4 |
| 2 | 21 | 0.0 | 3 |
| 2 | 25 | 0.5 | 2 |
| 2 | 22 | 0.5 | 1 |
| 3 | 24 | 0.0 | 6 |
| 3 | 25 | 0.166666666667 | 5 |
| 3 | 22 | 0.166666666667 | 4 |
| 3 | 26 | 0.333333333333 | 3 |
| 3 | 23 | 0.333333333333 | 2 |
| 3 | 21 | 0.333333333333 | 1 |
+---------+---------+----------------+------+
[18 rows x 4 columns]
>>> # we add recommended items not owned by user to the original SFrame
>>> rec = rec_wo_own_item.groupby('user_id', {'reco':gl.aggregate.CONCAT('item_id')})
>>> sf = sf.join(rec, 'user_id', 'left')
>>> print sf
+---------+---------+----------------------+
| item_id | user_id | reco |
+---------+---------+----------------------+
| 21 | 1 | [24, 25, 22] |
| 22 | 2 | [24, 26, 23, 21] |
| 23 | 1 | [24, 25, 22] |
| 24 | 3 | [21, 23, 26, 25, 22] |
| 25 | 2 | [24, 26, 23, 21] |
| 26 | 1 | [24, 25, 22] |
+---------+---------+----------------------+
[6 rows x 3 columns]
Related
I have two Tables in Google Sheet where i want to bring the values to one row with the corresponding quantity in second column from the Second Table.
First Table
+----------------------------+----------------------------+----------------------------+-----------------------------+----------------------------+---+---+----------------------------+
| A | B | C | D | E | F | G | H |
+----------------------------+----------------------------+----------------------------+-----------------------------+----------------------------+---+---+----------------------------+
| . | . | Paralite 1215 Ladies-BKB/4 | Paralite 1215 Ladies-BKB/5 | Paralite 1215 Ladies-BKB/6 | . | . | . |
| Paralite 1216 Ladies-BKR/1 | Paralite 1216 Ladies-BKR/2 | . | . | . | . | . | . |
| . | . | . | . | . | . | . | Paralite 1217 Ladies-BKR/9 |
+----------------------------+----------------------------+----------------------------+-----------------------------+----------------------------+---+---+----------------------------+
Second Table
+---+---+---+---+---+---+---+---+
| A | B | C | D | E | F | G | H |
+---+---+---+---+---+---+---+---+
| 0 | 0 | 8 | 6 | 8 | 0 | 0 | 0 |
| 4 | 8 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 6 |
+---+---+---+---+---+---+---+---+
Output I want
+----------------------------+---+
| A | B |
+----------------------------+---+
| Paralite 1215 Ladies-BKB/4 | 8 |
| Paralite 1215 Ladies-BKB/5 | 6 |
| Paralite 1215 Ladies-BKB/6 | 8 |
| Paralite 1216 Ladies-BKR/1 | 4 |
| Paralite 1216 Ladies-BKR/2 | 8 |
| Paralite 1217 Ladies-BKR/9 | 6 |
+----------------------------+---+
Google Sheet Link :
https://docs.google.com/spreadsheets/d/1c_vaT7-_cTLvPlcquK8Ax4aM6yYUQxBx15WS6yIAwa0/edit?usp=sharing
Try this:
=query(
{
flatten(A2:R7),
flatten(T2:AK7)
},
"where Col1 is not null",
0
)
If that does not cut it for you, please follow MattKing's advice and show your expected results in the sample spreadsheet. Clearly identify in the spreadsheet where the two source tables are and where your expected results are shown.
Use the following query for 2 tables like A1:H3 and A8:H10
=QUERY({FLATTEN(A1:H3),FLATTEN(A8:H10)},
" where Col1 <>'' and Col2 <>0 ",0)A1
How to join left next row in Cloud Spanner.
I want to calculate how many kilometers driven each driver.
My table looks like:
vehicle_id | driver_id | odometer
1 | 1 | 10
1 | 1 | 20
1 | 2 | 20
1 | 2 | 40
1 | 1 | 40
1 | 1 | 50
2 | 1 | 10
2 | 1 | 20
2 | 2 | 20
2 | 2 | 30
2 | 1 | 30
2 | 1 | 80
2 | 2 | 80
2 | 2 | 120
Results should be:
driver_id | total_mileage
1 | 80
2 | 70
My solution is:
SUM (mileage)
FROM (SELECT (odometer2-odometer) AS mileage
FROM (SELECT vehicle_id , odometer ,driver_id ,
NEXT.driver_id AS driver_id 2, NEXT.odometer AS odometer2 FROM Table
**JOIN NEXT ROW** AS NEXT
)
WHERE driver_id=driver_id2
)
GROUP BY driver_id
vehicle_id | driver_id | odometer | driver_id2 | odometer2 |mileage
1 | 1 | 10 | 1 | 20 | 10
1 | 1 | 20 | 2 | 20 | -
1 | 2 | 20 | 2 | 40 | 20
1 | 2 | 40 | 1 | 40 | -
1 | 1 | 40 | 1 | 50 | 10
1 | 1 | 50 | - | - | -
2 | 1 | 10 | 1 | 20 | 10
2 | 1 | 20 | 2 | 20 | -
2 | 2 | 20 | 2 | 30 | 10
2 | 2 | 30 | 1 | 30 | -
2 | 1 | 30 | 1 | 80 | 50
2 | 1 | 80 | 2 | 80 | -
2 | 2 | 80 | 2 | 120 | 40
2 | 2 | 120 | - | - | -
In Cloud Spanner function Row_number, OVER, LAG do not exist.
My question is how to join left next row in Cloud Spanner?
You're looking to PIVOT the data. This is not something that is doable directly in Cloud Spanner SQL - you'll need to post-process the data to affect the pivot.
I have a tree structure like node(1)->node(2)->node(3). I have name as an property used to retrieve a node.
Given a node say node(3), i wanna retrieve node(1).
Query tried :
MATCH (p:Node)-[:HAS*]->(c:Node) WHERE c.name = "node 3" RETURN p LIMIT 5
But, not able to get node 1.
Your query will not only return "node 1", but it should at least include one path containing it. It's possible to filter the paths to only get the one traversing all the way to the root, however:
MATCH (c:Node {name: "node 3"})<-[:HAS*0..]-(p:Node)
// The root does not have any incoming relationship
WHERE NOT (p)<-[:HAS]-()
RETURN p
Note the use of the 0 length, which matches all cases, including the one where the start node is the root.
Fun fact: even if you have an index on Node:name, it won't be used (unless you're using Neo4j 3.1, where it seems to be fixed since 3.1 Beta2 at least) and you have to explicitly specify it.
MATCH (c:Node {name: "node 3"})<-[:HAS*0..]-(p:Node)
USING INDEX c:Node(name)
WHERE NOT (p)<-[:HAS]-()
RETURN p
Using PROFILE on the first query (with a numerical id property instead of name):
+-----------------------+----------------+------+---------+-------------------------+----------------------+
| Operator | Estimated Rows | Rows | DB Hits | Variables | Other |
+-----------------------+----------------+------+---------+-------------------------+----------------------+
| +ProduceResults | 0 | 1 | 0 | p | p |
| | +----------------+------+---------+-------------------------+----------------------+
| +AntiSemiApply | 0 | 1 | 0 | anon[23], c -- p | |
| |\ +----------------+------+---------+-------------------------+----------------------+
| | +Expand(All) | 1 | 0 | 3 | anon[58], anon[67] -- p | (p)<-[:HAS]-() |
| | | +----------------+------+---------+-------------------------+----------------------+
| | +Argument | 1 | 3 | 0 | p | |
| | +----------------+------+---------+-------------------------+----------------------+
| +Filter | 1 | 3 | 3 | anon[23], c, p | p:Node |
| | +----------------+------+---------+-------------------------+----------------------+
| +VarLengthExpand(All) | 1 | 3 | 5 | anon[23], p -- c | (c)<-[:HAS*]-(p) |
| | +----------------+------+---------+-------------------------+----------------------+
| +Filter | 1 | 1 | 3 | c | c.id == { AUTOINT0} |
| | +----------------+------+---------+-------------------------+----------------------+
| +NodeByLabelScan | 3 | 3 | 4 | c | :Node |
+-----------------------+----------------+------+---------+-------------------------+----------------------+
Total database accesses: 18
and on the second one:
+-----------------------+----------------+------+---------+-------------------------+------------------+
| Operator | Estimated Rows | Rows | DB Hits | Variables | Other |
+-----------------------+----------------+------+---------+-------------------------+------------------+
| +ProduceResults | 0 | 1 | 0 | p | p |
| | +----------------+------+---------+-------------------------+------------------+
| +AntiSemiApply | 0 | 1 | 0 | anon[23], c -- p | |
| |\ +----------------+------+---------+-------------------------+------------------+
| | +Expand(All) | 1 | 0 | 3 | anon[81], anon[90] -- p | (p)<-[:HAS]-() |
| | | +----------------+------+---------+-------------------------+------------------+
| | +Argument | 1 | 3 | 0 | p | |
| | +----------------+------+---------+-------------------------+------------------+
| +Filter | 1 | 3 | 3 | anon[23], c, p | p:Node |
| | +----------------+------+---------+-------------------------+------------------+
| +VarLengthExpand(All) | 1 | 3 | 5 | anon[23], p -- c | (c)<-[:HAS*]-(p) |
| | +----------------+------+---------+-------------------------+------------------+
| +NodeUniqueIndexSeek | 1 | 1 | 2 | c | :Node(id) |
+-----------------------+----------------+------+---------+-------------------------+------------------+
Total database accesses: 13
Is it possible to do union of two queries (from the same entity) in core data? In SQL speak, if entity is called t, then consider that T has following data:
+------+------+------+
| x | y | z |
+------+------+------+
| 1 | 11 | 2 |
| 1 | 12 | 3 |
| 2 | 11 | 1 |
| 3 | 12 | 3 |
Then I am trying to run the following query (using core data - not SQLite)
select x, y, sum(z)
from t
group by 1, 2
union
select x, 1 as y, sum(z)
from t
group by 1, 2
order by x, y, 1
;
+------+------+--------+
| x | y | sum(z) |
+------+------+--------+
| 1 | 1 | 5 |
| 1 | 11 | 2 |
| 1 | 12 | 3 |
| 2 | 1 | 1 |
| 2 | 11 | 1 |
| 3 | 1 | 3 |
| 3 | 12 | 3 |
+------+------+--------+
7 rows in set (0.00 sec)
Is it possible?
Thanks!
I have 2 mysql databases. (1) authorID, authorFirst, authorLast. (2) courseID, courseName, courseAuthor1, courseAuthor2, courseAuthor3, courseAuthor4. How to join 2 columns from authors database into one OR ALL 4 of the courseAuthor columns in the courses database?
authors
authorID | authorFirst | authorLast
1 | firstA | lastA
2 | firstB | lastB
3 | firstC | lastC
4 | firstD | lastC
courses
courseID | courseName | courseAuthor1 | courseAuthor2 | courseAuthor3 | courseAuthor4
1 | course1 | 2 | 3 | NULL | NULL
2 | course 2 | 1 | 4 | 3 | NULL
3 | course 33 | 4 | 1 | 2 | NULL
4 | course 4 | 3 | NULL | NULL | NULL
I Want Results to look like this:
courseID | courseName | courseAuthor1 | courseAuthor2 | courseAuthor3 | courseAuthor4
1 | course1 | firstB LastB | firstC LastC | NULL | NULL
2 | course 2 | firstA LastA | firstD LastD | firstC LastC | NULL
3 | course 33 | firstD LastD | firstA LastA | firstB LastB | NULL
4 | course 4 | firstC LastC | NULL | NULL | NULL
I don't even know where to start. Here is my code:
SELECT * FROM courses JOIN authors ON authors.authorID IN
(courses.courseAuthor1, courses.courseAuthor2, courses.courseAuthor3,
courses.courseAuthor4)
Which shows:
results | courseID | courseName | courseAuthor1 | courseAuthor2 | courseAuthor3 | courseAuthor4 | authorID | authorFirst | authorLast
1 | 1 | course1 | 2 | 3 | NULL | NULL | 2 | firstB | lastB
2 | 1 | course1 | 2 | 3 | NULL | NULL | 3 | firstC | lastC
3 | 2 | course 2 | 1 | 4 | 3 | NULL | 1 | firstA | lastA
4 | 2 | course 2 | 1 | 4 | 3 | NULL | 4 | firstD | lastD
5 | 2 | course 2 | 1 | 4 | 3 | NULL | 3 | firstC | lastC
6 | 3 | course 33 | 4 | 1 | 2 | NULL | 4 | firstD | lastD
7 | 3 | course 33 | 4 | 1 | 2 | NULL | 1 | firstA | lastA
8 | 3 | course 33 | 4 | 1 | 2 | NULL | 2 | firstB | lastB
9 | 4 | course 4 | 3 | NULL | NULL | NULL | 3 | firstC | lastC
Thanks So Much!
It took me a while, but i've answered my question. Here it is:
SELECT c1.courseID, c1.courseName,
CONCAT(a1.authorFirst,' ',a1.authorLast) AS aOne,
CONCAT(a2.authorFirst,' ',a2.authorLast) AS aTwo,
CONCAT(a3.authorFirst,' ',a3.authorLast) AS aThree,
CONCAT(a4.authorFirst,' ',a4.authorLast) AS aFour
FROM courses c1
LEFT JOIN authors a1 ON c1.courseAuthor1 = a1.authorID
LEFT JOIN authors a2 ON c1.courseAuthor2 = a2.authorID
LEFT JOIN authors a3 ON c1.courseAuthor3 = a3.authorID
LEFT JOIN authors a4 ON c1.courseAuthor4 = a4.authorID