Created
April 18, 2016 10:09
-
-
Save huatangzhi/44e5188e2533e35874b64460934c1c49 to your computer and use it in GitHub Desktop.
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
test |
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
<component name="ProjectDictionaryState"> | |
<dictionary name="hp" /> | |
</component> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="Encoding"> | |
<file url="PROJECT" charset="UTF-8" /> | |
</component> | |
</project> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectInspectionProfilesVisibleTreeState"> | |
<entry key="Project Default"> | |
<profile-state> | |
<expanded-state> | |
<State> | |
<id /> | |
</State> | |
</expanded-state> | |
<selected-state> | |
<State> | |
<id>Buildout</id> | |
</State> | |
</selected-state> | |
</profile-state> | |
</entry> | |
</component> | |
<component name="ProjectLevelVcsManager" settingsEditedManually="false"> | |
<OptionsSetting value="true" id="Add" /> | |
<OptionsSetting value="true" id="Remove" /> | |
<OptionsSetting value="true" id="Checkout" /> | |
<OptionsSetting value="true" id="Update" /> | |
<OptionsSetting value="true" id="Status" /> | |
<OptionsSetting value="true" id="Edit" /> | |
<ConfirmationsSetting value="0" id="Add" /> | |
<ConfirmationsSetting value="0" id="Remove" /> | |
</component> | |
<component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7.10 (/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7)" project-jdk-type="Python SDK" /> | |
</project> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectModuleManager"> | |
<modules> | |
<module fileurl="file://$PROJECT_DIR$/.idea/test.iml" filepath="$PROJECT_DIR$/.idea/test.iml" /> | |
</modules> | |
</component> | |
</project> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<module type="PYTHON_MODULE" version="4"> | |
<component name="NewModuleRootManager"> | |
<content url="file://$MODULE_DIR$" /> | |
<orderEntry type="inheritedJdk" /> | |
<orderEntry type="sourceFolder" forTests="false" /> | |
</component> | |
<component name="TestRunnerService"> | |
<option name="PROJECT_TEST_RUNNER" value="Unittests" /> | |
</component> | |
</module> |
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
import random, math | |
def SpiltData(data, M, k, seed): | |
test = [] | |
train = [] | |
random.seed(seed) | |
for user, item in data: | |
if random.randint(0, M) == k: | |
test.append([user, item]) | |
else: | |
train.append([user, item]) | |
return train, test | |
def Recall(train, test, N): | |
hit = 0 | |
all = 0 | |
for user in train.keys(): | |
tu = test[user] | |
rank = GetRecommendation(user, N) | |
for item, pui in rank: | |
if item in tu: | |
hit += 1 | |
all += len(tu) | |
return hit / (all * 1.0) | |
def Precision(train, test, N): | |
hit = 0 | |
all = 0 | |
for user in train.keys(): | |
tu = test[user] | |
rank = GetRecommendation(user, N) | |
for item, pui in rank: | |
if item in tu: | |
hit += 1 | |
all += N | |
return hit / (all * 1.0) | |
def Coverage(train, test, N): | |
recommend_items = set() | |
all_items = set() | |
for user in train.keys(): | |
for item in train[user].keys(): | |
all_items.add(item) | |
rank = GetRecommendation(user, N) | |
for item, pui in rank: | |
recommend_items.add(item) | |
return len(recommend_items) / (len(all_items) * 1.0) | |
def Popularity(train, test, N): | |
item_popularity = dict() | |
for user, items in train.items(): | |
for item in items.keys(): | |
if item not in item_popularity: | |
item_popularity[item] = 0 | |
item_popularity[item] += 1 | |
ret = 0 | |
n = 0 | |
for user in train.keys(): | |
rank = GetRecommendation(user, N) | |
for item, pui in rank: | |
ret += math.log(1 + item_popularity(item)) | |
n += 1 | |
ret /= n * 1.0 | |
return ret | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment