Comments (6)
WIP
from tensorrec.
@jfkirk finished the dense version. Going to take some tweaking to work with sparse iteration matrix as input . Have it on a branch for now
from tensorrec.
def _idcg(hits, k=10, ctype="binary"):
if ctype == "binary":
arg = min(hits, k+1)
idgc = np.sum(1/np.log2(np.arange(arg) + 2)) # arange index from 0
elif ctype == "scalar":
sorted = hits[np.argsort(-hits)][:k]
idgc = np.sum(sorted/np.log2(np.arange(len(sorted)) + 2))
else:
raise ValueError("Invalid IDCG calculation type")
return idgc
def normalized_discounted_cumulative_gain(model, test_interactions, k=10,
user_features=None,
item_features=None,
preserve_rows=False):
predicted_ranks = model.predict_rank(user_features=user_features,
item_features=item_features)
positive_test_interactions = test_interactions > 0
ranks_of_relevant = sp.csr_matrix(predicted_ranks *
positive_test_interactions.A)
k_mask = np.less(ranks_of_relevant.data, k + 1)
ror_at_k = np.maximum(np.multiply(ranks_of_relevant.data, k_mask), 1)
relevance = sp.csr_matrix(
test_interactions.A *
positive_test_interactions.A
)
relevance_at_k = (2**np.multiply(relevance.data, k_mask)) - 1
ranks_of_relevant.data = relevance_at_k/np.log2(ror_at_k + 1) # ranks at 1
dcg = ranks_of_relevant.sum(axis=1).flatten()
if np.max(test_interactions.A) == 1:
# If the data is binary, we can save the ideal ranking sort
idcg = np.apply_along_axis(_idcg,
0,
positive_test_interactions.sum(axis=1).flatten()[0],
ctype="binary"
)
else:
idcg = np.apply_along_axis(_idcg,
0,
relevance,
ctype="scalar"
)
print relevance
ndcg = dcg/idcg
if preserve_rows:
return ndcg.flatten()
else:
return np.nanmean(ndcg.flatten())
from tensorrec.
some trickery required for the "no relevant example case". I don't see a way around the sort for the scalar case. Note np.argsort
gives the sort slice for the original array NOT the resultant sorted position. I break out the binary and scalar cases to save the sort in the binary case
from tensorrec.
need to add tests for scalar case somehow
from tensorrec.
from tensorrec.
Related Issues (20)
- TFLite export - Input and output nodes HOT 3
- How to use tensorrec to do online serving?
- Error: Pack node (stack_24) axis attribute is out of bounds: 1 HOT 1
- Validation techniques HOT 1
- Trouble saving models HOT 2
- How to exclude user's liked items from the predictions? HOT 1
- Unexpected outcomes from custom prediction graph
- How can i get similar users?
- Tensorrec environment issues HOT 5
- Inferences' issue HOT 1
- How to make Tensorrec have stable results HOT 2
- Temporal dynamics
- Fix simple typo: predictiones -> predictions
- How to write kNN by TensorRec?
- Error on dimensionality HOT 1
- Error - tensorflow has no attribute`get_default_session()` HOT 3
- module 'tensorflow' has no attribute 'get_default_session' HOT 4
- About users Features matrix
- Performance issues in the program
- Question - install of tensorrec only once
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tensorrec.