{"action":"created","comment":{"url":"https://api.github.com/repos/databricks/spark-sklearn/pulls/comments/68611451","id":68611451,"diff_hunk":"@@ -0,0 +1,407 @@\n+\"\"\"\n+Classes for implementing keyed models.\n+\n+The use case that this addresses is where a client has a dataset with many keys - the distribution\n+ of distinct keys amongst the is such that the total number of rows for every key value can be\n+contained completely in memory on a single machine.\n+\n+This assumption is particularly enabling because clients may wish to apply more intricate single-\n+machine models (such as a scikit-learn estimator) to every user.\n+\n+The API provided here generalizes the scikit-learn estimator interface to the Spark ML one; in\n+particular, it allows clients to train their scikit-learn estimators in parallel over a grouped\n+and aggregated dataframe.\n+\n+>>> import numpy as np\n+>>> from sklearn.linear_model import LinearRegression\n+>>> from pyspark.ml.linalg import Vectors\n+>>> from pyspark.sql import SparkSession\n+>>> from pyspark.sql.functions import udf\n+>>> spark = SparkSession.builder.master(\"local\").getOrCreate()\n+>>> df = spark.createDataFrame([(user,\n+...                              Vectors.dense([i, i ** 2, i ** 3]),\n+...                              user + i + 2 * i ** 2 + 3 * i ** 3)\n+...                             for user in range(3) for i in range(5)])\n+>>> df = df.toDF(\"key\", \"features\", \"y\")\n+>>> km = KeyedEstimator(sklearnEstimator=LinearRegression(), yCol=\"y\").fit(df)\n+>>> printnp = lambda nparr: \"[\" + \", \".join(\"{:.2f}\".format(x) for x in nparr) + \"]\"\n+>>> coefs = udf(lambda lr: \"intercept: {:.2f} coefs: {}\".format(lr.intercept_, printnp(lr.coef_)))\n+>>> km.keyedModels.select(\"key\", coefs(\"estimator\").alias(\"lr\")).show(truncate=False)\n++---+-----------------------------------------+\n+|key|lr                                       |\n++---+-----------------------------------------+\n+|0  |intercept: 0.00 coefs: [1.00, 2.00, 3.00]|\n+|1  |intercept: 1.00 coefs: [1.00, 2.00, 3.00]|\n+|2  |intercept: 2.00 coefs: [1.00, 2.00, 3.00]|\n++---+-----------------------------------------+\n+<BLANKLINE>\n+>>> input = spark.createDataFrame([(0, Vectors.dense(3, 1, -1))]).toDF(\"key\", \"features\")","path":"python/spark_sklearn/keyed_models.py","position":38,"original_position":38,"commit_id":"914ee33ffe15332ab2243a9bdd9be61d31a12d23","original_commit_id":"914ee33ffe15332ab2243a9bdd9be61d31a12d23","user":{"login":"vlad17","id":5834964,"avatar_url":"https://avatars.githubusercontent.com/u/5834964?v=3","gravatar_id":"","url":"https://api.github.com/users/vlad17","html_url":"https://github.com/vlad17","followers_url":"https://api.github.com/users/vlad17/followers","following_url":"https://api.github.com/users/vlad17/following{/other_user}","gists_url":"https://api.github.com/users/vlad17/gists{/gist_id}","starred_url":"https://api.github.com/users/vlad17/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vlad17/subscriptions","organizations_url":"https://api.github.com/users/vlad17/orgs","repos_url":"https://api.github.com/users/vlad17/repos","events_url":"https://api.github.com/users/vlad17/events{/privacy}","received_events_url":"https://api.github.com/users/vlad17/received_events","type":"User","site_admin":false},"body":"The blank line has to be there to match `show()` output, but I will add a paragraph interjecting with your explanation. Done","created_at":"2016-06-27T16:41:03Z","updated_at":"2016-06-27T16:41:03Z","html_url":"https://github.com/databricks/spark-sklearn/pull/26#discussion_r68611451","pull_request_url":"https://api.github.com/repos/databricks/spark-sklearn/pulls/26","_links":{"self":{"href":"https://api.github.com/repos/databricks/spark-sklearn/pulls/comments/68611451"},"html":{"href":"https://github.com/databricks/spark-sklearn/pull/26#discussion_r68611451"},"pull_request":{"href":"https://api.github.com/repos/databricks/spark-sklearn/pulls/26"}}},"pull_request":{"url":"https://api.github.com/repos/databricks/spark-sklearn/pulls/26","id":75186308,"html_url":"https://github.com/databricks/spark-sklearn/pull/26","diff_url":"https://github.com/databricks/spark-sklearn/pull/26.diff","patch_url":"https://github.com/databricks/spark-sklearn/pull/26.patch","issue_url":"https://api.github.com/repos/databricks/spark-sklearn/issues/26","number":26,"state":"open","locked":false,"title":"High level api","user":{"login":"vlad17","id":5834964,"avatar_url":"https://avatars.githubusercontent.com/u/5834964?v=3","gravatar_id":"","url":"https://api.github.com/users/vlad17","html_url":"https://github.com/vlad17","followers_url":"https://api.github.com/users/vlad17/followers","following_url":"https://api.github.com/users/vlad17/following{/other_user}","gists_url":"https://api.github.com/users/vlad17/gists{/gist_id}","starred_url":"https://api.github.com/users/vlad17/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vlad17/subscriptions","organizations_url":"https://api.github.com/users/vlad17/orgs","repos_url":"https://api.github.com/users/vlad17/repos","events_url":"https://api.github.com/users/vlad17/events{/privacy}","received_events_url":"https://api.github.com/users/vlad17/received_events","type":"User","site_admin":false},"body":"This commit adds the high-level KeyedModels API, which enables users to train scikit-learn estimators on grouped data.\r\n\r\nTesting done via unit tests (note some edge case functionality is not offered / tests are skipped until associated spark JIRAs are resolved).\r\n\r\nSome additional refactoring in test_gapply was done to extract common pandas dataframe functionality.","created_at":"2016-06-24T23:23:44Z","updated_at":"2016-06-27T16:41:03Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3ad569f1d8410834bfff59d022ad668a94a281c7","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/databricks/spark-sklearn/pulls/26/commits","review_comments_url":"https://api.github.com/repos/databricks/spark-sklearn/pulls/26/comments","review_comment_url":"https://api.github.com/repos/databricks/spark-sklearn/pulls/comments{/number}","comments_url":"https://api.github.com/repos/databricks/spark-sklearn/issues/26/comments","statuses_url":"https://api.github.com/repos/databricks/spark-sklearn/statuses/914ee33ffe15332ab2243a9bdd9be61d31a12d23","head":{"label":"vlad17:high-level-api","ref":"high-level-api","sha":"914ee33ffe15332ab2243a9bdd9be61d31a12d23","user":{"login":"vlad17","id":5834964,"avatar_url":"https://avatars.githubusercontent.com/u/5834964?v=3","gravatar_id":"","url":"https://api.github.com/users/vlad17","html_url":"https://github.com/vlad17","followers_url":"https://api.github.com/users/vlad17/followers","following_url":"https://api.github.com/users/vlad17/following{/other_user}","gists_url":"https://api.github.com/users/vlad17/gists{/gist_id}","starred_url":"https://api.github.com/users/vlad17/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vlad17/subscriptions","organizations_url":"https://api.github.com/users/vlad17/orgs","repos_url":"https://api.github.com/users/vlad17/repos","events_url":"https://api.github.com/users/vlad17/events{/privacy}","received_events_url":"https://api.github.com/users/vlad17/received_events","type":"User","site_admin":false},"repo":{"id":61221264,"name":"spark-sklearn","full_name":"vlad17/spark-sklearn","owner":{"login":"vlad17","id":5834964,"avatar_url":"https://avatars.githubusercontent.com/u/5834964?v=3","gravatar_id":"","url":"https://api.github.com/users/vlad17","html_url":"https://github.com/vlad17","followers_url":"https://api.github.com/users/vlad17/followers","following_url":"https://api.github.com/users/vlad17/following{/other_user}","gists_url":"https://api.github.com/users/vlad17/gists{/gist_id}","starred_url":"https://api.github.com/users/vlad17/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vlad17/subscriptions","organizations_url":"https://api.github.com/users/vlad17/orgs","repos_url":"https://api.github.com/users/vlad17/repos","events_url":"https://api.github.com/users/vlad17/events{/privacy}","received_events_url":"https://api.github.com/users/vlad17/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/vlad17/spark-sklearn","description":"Scikit-learn integration package for Spark","fork":true,"url":"https://api.github.com/repos/vlad17/spark-sklearn","forks_url":"https://api.github.com/repos/vlad17/spark-sklearn/forks","keys_url":"https://api.github.com/repos/vlad17/spark-sklearn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/vlad17/spark-sklearn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/vlad17/spark-sklearn/teams","hooks_url":"https://api.github.com/repos/vlad17/spark-sklearn/hooks","issue_events_url":"https://api.github.com/repos/vlad17/spark-sklearn/issues/events{/number}","events_url":"https://api.github.com/repos/vlad17/spark-sklearn/events","assignees_url":"https://api.github.com/repos/vlad17/spark-sklearn/assignees{/user}","branches_url":"https://api.github.com/repos/vlad17/spark-sklearn/branches{/branch}","tags_url":"https://api.github.com/repos/vlad17/spark-sklearn/tags","blobs_url":"https://api.github.com/repos/vlad17/spark-sklearn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/vlad17/spark-sklearn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/vlad17/spark-sklearn/git/refs{/sha}","trees_url":"https://api.github.com/repos/vlad17/spark-sklearn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/vlad17/spark-sklearn/statuses/{sha}","languages_url":"https://api.github.com/repos/vlad17/spark-sklearn/languages","stargazers_url":"https://api.github.com/repos/vlad17/spark-sklearn/stargazers","contributors_url":"https://api.github.com/repos/vlad17/spark-sklearn/contributors","subscribers_url":"https://api.github.com/repos/vlad17/spark-sklearn/subscribers","subscription_url":"https://api.github.com/repos/vlad17/spark-sklearn/subscription","commits_url":"https://api.github.com/repos/vlad17/spark-sklearn/commits{/sha}","git_commits_url":"https://api.github.com/repos/vlad17/spark-sklearn/git/commits{/sha}","comments_url":"https://api.github.com/repos/vlad17/spark-sklearn/comments{/number}","issue_comment_url":"https://api.github.com/repos/vlad17/spark-sklearn/issues/comments{/number}","contents_url":"https://api.github.com/repos/vlad17/spark-sklearn/contents/{+path}","compare_url":"https://api.github.com/repos/vlad17/spark-sklearn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/vlad17/spark-sklearn/merges","archive_url":"https://api.github.com/repos/vlad17/spark-sklearn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/vlad17/spark-sklearn/downloads","issues_url":"https://api.github.com/repos/vlad17/spark-sklearn/issues{/number}","pulls_url":"https://api.github.com/repos/vlad17/spark-sklearn/pulls{/number}","milestones_url":"https://api.github.com/repos/vlad17/spark-sklearn/milestones{/number}","notifications_url":"https://api.github.com/repos/vlad17/spark-sklearn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/vlad17/spark-sklearn/labels{/name}","releases_url":"https://api.github.com/repos/vlad17/spark-sklearn/releases{/id}","deployments_url":"https://api.github.com/repos/vlad17/spark-sklearn/deployments","created_at":"2016-06-15T16:00:20Z","updated_at":"2016-06-27T16:06:28Z","pushed_at":"2016-06-27T16:06:27Z","git_url":"git://github.com/vlad17/spark-sklearn.git","ssh_url":"git@github.com:vlad17/spark-sklearn.git","clone_url":"https://github.com/vlad17/spark-sklearn.git","svn_url":"https://github.com/vlad17/spark-sklearn","homepage":"","size":431,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"databricks:master","ref":"master","sha":"922aee935743c4ab1f170d16e37cb968323ebf9d","user":{"login":"databricks","id":4998052,"avatar_url":"https://avatars.githubusercontent.com/u/4998052?v=3","gravatar_id":"","url":"https://api.github.com/users/databricks","html_url":"https://github.com/databricks","followers_url":"https://api.github.com/users/databricks/followers","following_url":"https://api.github.com/users/databricks/following{/other_user}","gists_url":"https://api.github.com/users/databricks/gists{/gist_id}","starred_url":"https://api.github.com/users/databricks/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/databricks/subscriptions","organizations_url":"https://api.github.com/users/databricks/orgs","repos_url":"https://api.github.com/users/databricks/repos","events_url":"https://api.github.com/users/databricks/events{/privacy}","received_events_url":"https://api.github.com/users/databricks/received_events","type":"Organization","site_admin":false},"repo":{"id":41820544,"name":"spark-sklearn","full_name":"databricks/spark-sklearn","owner":{"login":"databricks","id":4998052,"avatar_url":"https://avatars.githubusercontent.com/u/4998052?v=3","gravatar_id":"","url":"https://api.github.com/users/databricks","html_url":"https://github.com/databricks","followers_url":"https://api.github.com/users/databricks/followers","following_url":"https://api.github.com/users/databricks/following{/other_user}","gists_url":"https://api.github.com/users/databricks/gists{/gist_id}","starred_url":"https://api.github.com/users/databricks/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/databricks/subscriptions","organizations_url":"https://api.github.com/users/databricks/orgs","repos_url":"https://api.github.com/users/databricks/repos","events_url":"https://api.github.com/users/databricks/events{/privacy}","received_events_url":"https://api.github.com/users/databricks/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/databricks/spark-sklearn","description":"Scikit-learn integration package for Spark","fork":false,"url":"https://api.github.com/repos/databricks/spark-sklearn","forks_url":"https://api.github.com/repos/databricks/spark-sklearn/forks","keys_url":"https://api.github.com/repos/databricks/spark-sklearn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/databricks/spark-sklearn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/databricks/spark-sklearn/teams","hooks_url":"https://api.github.com/repos/databricks/spark-sklearn/hooks","issue_events_url":"https://api.github.com/repos/databricks/spark-sklearn/issues/events{/number}","events_url":"https://api.github.com/repos/databricks/spark-sklearn/events","assignees_url":"https://api.github.com/repos/databricks/spark-sklearn/assignees{/user}","branches_url":"https://api.github.com/repos/databricks/spark-sklearn/branches{/branch}","tags_url":"https://api.github.com/repos/databricks/spark-sklearn/tags","blobs_url":"https://api.github.com/repos/databricks/spark-sklearn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/databricks/spark-sklearn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/databricks/spark-sklearn/git/refs{/sha}","trees_url":"https://api.github.com/repos/databricks/spark-sklearn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/databricks/spark-sklearn/statuses/{sha}","languages_url":"https://api.github.com/repos/databricks/spark-sklearn/languages","stargazers_url":"https://api.github.com/repos/databricks/spark-sklearn/stargazers","contributors_url":"https://api.github.com/repos/databricks/spark-sklearn/contributors","subscribers_url":"https://api.github.com/repos/databricks/spark-sklearn/subscribers","subscription_url":"https://api.github.com/repos/databricks/spark-sklearn/subscription","commits_url":"https://api.github.com/repos/databricks/spark-sklearn/commits{/sha}","git_commits_url":"https://api.github.com/repos/databricks/spark-sklearn/git/commits{/sha}","comments_url":"https://api.github.com/repos/databricks/spark-sklearn/comments{/number}","issue_comment_url":"https://api.github.com/repos/databricks/spark-sklearn/issues/comments{/number}","contents_url":"https://api.github.com/repos/databricks/spark-sklearn/contents/{+path}","compare_url":"https://api.github.com/repos/databricks/spark-sklearn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/databricks/spark-sklearn/merges","archive_url":"https://api.github.com/repos/databricks/spark-sklearn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/databricks/spark-sklearn/downloads","issues_url":"https://api.github.com/repos/databricks/spark-sklearn/issues{/number}","pulls_url":"https://api.github.com/repos/databricks/spark-sklearn/pulls{/number}","milestones_url":"https://api.github.com/repos/databricks/spark-sklearn/milestones{/number}","notifications_url":"https://api.github.com/repos/databricks/spark-sklearn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/databricks/spark-sklearn/labels{/name}","releases_url":"https://api.github.com/repos/databricks/spark-sklearn/releases{/id}","deployments_url":"https://api.github.com/repos/databricks/spark-sklearn/deployments","created_at":"2015-09-02T18:44:51Z","updated_at":"2016-06-27T07:39:55Z","pushed_at":"2016-06-25T01:33:25Z","git_url":"git://github.com/databricks/spark-sklearn.git","ssh_url":"git@github.com:databricks/spark-sklearn.git","clone_url":"https://github.com/databricks/spark-sklearn.git","svn_url":"https://github.com/databricks/spark-sklearn","homepage":"","size":262,"stargazers_count":173,"watchers_count":173,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":35,"mirror_url":null,"open_issues_count":4,"forks":35,"open_issues":4,"watchers":173,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/databricks/spark-sklearn/pulls/26"},"html":{"href":"https://github.com/databricks/spark-sklearn/pull/26"},"issue":{"href":"https://api.github.com/repos/databricks/spark-sklearn/issues/26"},"comments":{"href":"https://api.github.com/repos/databricks/spark-sklearn/issues/26/comments"},"review_comments":{"href":"https://api.github.com/repos/databricks/spark-sklearn/pulls/26/comments"},"review_comment":{"href":"https://api.github.com/repos/databricks/spark-sklearn/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/databricks/spark-sklearn/pulls/26/commits"},"statuses":{"href":"https://api.github.com/repos/databricks/spark-sklearn/statuses/914ee33ffe15332ab2243a9bdd9be61d31a12d23"}}}}
{"actor":{"display_login":"vlad17"}}