-
Notifications
You must be signed in to change notification settings - Fork 28.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-7156][SQL] support RandomSplit in DataFrames #5761
Closed
+130
−22
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e98ebac
[SPARK-7156][SQL] support RandomSplit in DataFrames
brkyvz 2384266
addressed comments v0.1
brkyvz f400ade
fix build errors
brkyvz 3c11d1b
fixed broken test
brkyvz 6000328
added python api and fixed test
brkyvz 1ddb3da
copy base
brkyvz 69669c3
fix broken test
brkyvz a1fb0aa
remove unrelated file
brkyvz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -425,14 +425,30 @@ def distinct(self): | |
def sample(self, withReplacement, fraction, seed=None): | ||
"""Returns a sampled subset of this :class:`DataFrame`. | ||
|
||
>>> df.sample(False, 0.5, 97).count() | ||
>>> df.sample(False, 0.5, 42).count() | ||
1 | ||
""" | ||
assert fraction >= 0.0, "Negative fraction value: %s" % fraction | ||
seed = seed if seed is not None else random.randint(0, sys.maxsize) | ||
rdd = self._jdf.sample(withReplacement, fraction, long(seed)) | ||
return DataFrame(rdd, self.sql_ctx) | ||
|
||
def randomSplit(self, weights, seed=None): | ||
"""Randomly splits this :class:`DataFrame` with the provided weights. | ||
|
||
>>> splits = df4.randomSplit([1.0, 2.0], 24) | ||
>>> splits[0].count() | ||
1 | ||
|
||
>>> splits[1].count() | ||
3 | ||
""" | ||
for w in weights: | ||
assert w >= 0.0, "Negative weight value: %s" % w | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
seed = seed if seed is not None else random.randint(0, sys.maxsize) | ||
rdd_array = self._jdf.randomSplit(_to_seq(self.sql_ctx._sc, weights), long(seed)) | ||
return [DataFrame(rdd, self.sql_ctx) for rdd in rdd_array] | ||
|
||
@property | ||
def dtypes(self): | ||
"""Returns all column names and their data types as a list. | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -711,7 +711,7 @@ class DataFrame private[sql]( | |
* @group dfops | ||
*/ | ||
def sample(withReplacement: Boolean, fraction: Double, seed: Long): DataFrame = { | ||
Sample(fraction, withReplacement, seed, logicalPlan) | ||
Sample(0.0, fraction, withReplacement, seed, logicalPlan) | ||
} | ||
|
||
/** | ||
|
@@ -725,6 +725,42 @@ class DataFrame private[sql]( | |
sample(withReplacement, fraction, Utils.random.nextLong) | ||
} | ||
|
||
/** | ||
* Randomly splits this [[DataFrame]] with the provided weights. | ||
* | ||
* @param weights weights for splits, will be normalized if they don't sum to 1. | ||
* @param seed Seed for sampling. | ||
* @group dfops | ||
*/ | ||
def randomSplit(weights: Array[Double], seed: Long): Array[DataFrame] = { | ||
val sum = weights.sum | ||
val normalizedCumWeights = weights.map(_ / sum).scanLeft(0.0d)(_ + _) | ||
normalizedCumWeights.sliding(2).map { x => | ||
new DataFrame(sqlContext, Sample(x(0), x(1), false, seed, logicalPlan)) | ||
}.toArray | ||
} | ||
|
||
/** | ||
* Randomly splits this [[DataFrame]] with the provided weights. | ||
* | ||
* @param weights weights for splits, will be normalized if they don't sum to 1. | ||
* @group dfops | ||
*/ | ||
def randomSplit(weights: Array[Double]): Array[DataFrame] = { | ||
randomSplit(weights, Utils.random.nextLong) | ||
} | ||
|
||
/** | ||
* Randomly splits this [[DataFrame]] with the provided weights. Provided for the Python Api. | ||
* | ||
* @param weights weights for splits, will be normalized if they don't sum to 1. | ||
* @param seed Seed for sampling. | ||
* @group dfops | ||
*/ | ||
def randomSplit(weights: List[Double], seed: Long): Array[DataFrame] = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. private[spark] ? since it is only used for python. |
||
randomSplit(weights.toArray, seed) | ||
} | ||
|
||
/** | ||
* (Scala-specific) Returns a new [[DataFrame]] where each row has been expanded to zero or more | ||
* rows by the provided function. This is similar to a `LATERAL VIEW` in HiveQL. The columns of | ||
|
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
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be great to add params doc