You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@spark.apache.org by Hao REN <ju...@gmail.com> on 2013/09/12 10:18:39 UTC
RDD.subtract doesn't work
Hi,
I am writing a logistic regression prog with Spark based on SparkLR example.
Say, a data set containing 10000 DataPoints, where DataPoint is a case
class like: case class DataPoint(x: Vector, y: Double) as defined in the
SparkLR example.
In order to divide the data set into 2 parts: training set and test set, I
tried some code below:
val trainingSet = points.sample(false, 0.6, 7)
val testSet = points.subtract(trainingSet)
,where points is a RDD[DataPoint] contains 10000 points
sample works well, trainingSet.count gives a number around 6000, but
testSet.count gives 10000 which is not the expected 4000.
It seems that subtract cant work with some custom class, as DataPoint here.
2 questions:
1) Which is the best way to divide data with a ratio, say 6/4, especially
when Data is not a primitive type, like some custom classes ?
2) Why subtract doesn't work ? Maybe ordering and compare should be
implemented for DataPoint class ?
I have also checked the SubtractedRDD class. Without background about the
Spark source code, I can not understand what the problem is.
https://github.com/mesos/spark/blob/master/core/src/main/scala/org/apache/spark/rdd/SubtractedRDD.scala
Any help is highly appreciated !
Thank you in advance. =)
Hao
--
REN Hao
Etudiant d'échange à l'Ecole Polytechnique Fédérale de Lausanne (EPFL)
Computer Science
Etudiant à l'Université de Technologie de Compiègne (UTC)
Génie Informatique - Fouille de Données
Tel: +33 06 14 54 57 24 / +41 07 86 47 52 69
Re: RDD.subtract doesn't work
Posted by Jason Lenderman <js...@gmail.com>.
I believe your understanding is correct. And, yes, the processing should
happen in parallel for each partition.
On Fri, Sep 13, 2013 at 10:56 AM, Fabrizio Milo aka misto <
mistobaan@gmail.com> wrote:
> @Jason
>
> I find interesting but I am not sure I understand it completely.
> I am assuming that the objective is to partition a dataset in two
> sub-sets where the element of each set is randomly selected from the
> first dataset.
>
> If I read the code correctly what it does is:
>
> For each partition creates a Random Seed.
> for each element inside a partition generate a random number.
> for each ( value, randomNumber)
> create two filters, one for the elements that are less than the split
> values and the other for elements that are greater than the
> randomNumber.
>
> And because the random generator is uniform then if we provide a p:Double
> = 0.3
> roughly 30% of the random numbers will fall under 0.3.
>
> Is this done on one node or on each node it will handle only the
> mapping on the partition it received ? I guess/hope the second but I
> want to make sure.
>
> Thank you
> --------------------------
> Luck favors the prepared mind. (Pasteur)
>
Re: RDD.subtract doesn't work
Posted by Fabrizio Milo aka misto <mi...@gmail.com>.
@Jason
I find interesting but I am not sure I understand it completely.
I am assuming that the objective is to partition a dataset in two
sub-sets where the element of each set is randomly selected from the
first dataset.
If I read the code correctly what it does is:
For each partition creates a Random Seed.
for each element inside a partition generate a random number.
for each ( value, randomNumber)
create two filters, one for the elements that are less than the split
values and the other for elements that are greater than the
randomNumber.
And because the random generator is uniform then if we provide a p:Double = 0.3
roughly 30% of the random numbers will fall under 0.3.
Is this done on one node or on each node it will handle only the
mapping on the partition it received ? I guess/hope the second but I
want to make sure.
Thank you
--------------------------
Luck favors the prepared mind. (Pasteur)
Re: RDD.subtract doesn't work
Posted by Jason Lenderman <js...@gmail.com>.
You're welcome. Be sure to use the second version I posted as the first
version is problematic and could result in a bad (non-random) split under
some circumstances.
On Fri, Sep 13, 2013 at 8:31 AM, Hao REN <ju...@gmail.com> wrote:
> @Jason : Thank you for your code. It works fine.
>
> @Mark : It's good to know about random number generation. Thanks for the
> advice.
>
> Still a question :
>
> As subtract can be replaced by the Jason's code, what is the use case of
> subtract, knowing that it is not a good way to partition data ?
>
> Thank you.
>
> Hao
>
>
> On Fri, Sep 13, 2013 at 3:33 AM, Jason Lenderman <js...@gmail.com>wrote:
>
>>
>> Yeah, I realized shortly after I sent that message that my use of map in
>> that code was problematic. This is probably a bit better:
>>
>>
>> def split[T : ClassManifest](data: RDD[T], p: Double, seed: Long =
>> System.currentTimeMillis): (RDD[T], RDD[T]) = {
>> val rand = new java.util.Random(seed)
>> val partitionSeeds = data.partitions.map(partition => rand.nextLong)
>> val temp = data.mapPartitionsWithIndex((index, iter) => {
>> val partitionRand = new java.util.Random(partitionSeeds(index))
>> iter.map(x => (x, partitionRand.nextDouble))
>>
>> })
>> (temp.filter(_._2 <= p).map(_._1), temp.filter(_._2 > p).map(_._1))
>> }
>>
>>
>>
>>
>
>
> --
> REN Hao
>
> Etudiant d'échange à l'Ecole Polytechnique Fédérale de Lausanne (EPFL)
>
> Computer Science
>
> Etudiant à l'Université de Technologie de Compiègne (UTC)
>
> Génie Informatique - Fouille de Données
>
> Tel: +33 06 14 54 57 24 / +41 07 86 47 52 69
>
Re: RDD.subtract doesn't work
Posted by Hao REN <ju...@gmail.com>.
@Jason : Thank you for your code. It works fine.
@Mark : It's good to know about random number generation. Thanks for the
advice.
Still a question :
As subtract can be replaced by the Jason's code, what is the use case of
subtract, knowing that it is not a good way to partition data ?
Thank you.
Hao
On Fri, Sep 13, 2013 at 3:33 AM, Jason Lenderman <js...@gmail.com>wrote:
>
> Yeah, I realized shortly after I sent that message that my use of map in
> that code was problematic. This is probably a bit better:
>
>
> def split[T : ClassManifest](data: RDD[T], p: Double, seed: Long =
> System.currentTimeMillis): (RDD[T], RDD[T]) = {
> val rand = new java.util.Random(seed)
> val partitionSeeds = data.partitions.map(partition => rand.nextLong)
> val temp = data.mapPartitionsWithIndex((index, iter) => {
> val partitionRand = new java.util.Random(partitionSeeds(index))
> iter.map(x => (x, partitionRand.nextDouble))
>
> })
> (temp.filter(_._2 <= p).map(_._1), temp.filter(_._2 > p).map(_._1))
> }
>
>
>
>
--
REN Hao
Etudiant d'échange à l'Ecole Polytechnique Fédérale de Lausanne (EPFL)
Computer Science
Etudiant à l'Université de Technologie de Compiègne (UTC)
Génie Informatique - Fouille de Données
Tel: +33 06 14 54 57 24 / +41 07 86 47 52 69
Re: RDD.subtract doesn't work
Posted by Jason Lenderman <js...@gmail.com>.
Yeah, I realized shortly after I sent that message that my use of map in
that code was problematic. This is probably a bit better:
def split[T : ClassManifest](data: RDD[T], p: Double, seed: Long =
System.currentTimeMillis): (RDD[T], RDD[T]) = {
val rand = new java.util.Random(seed)
val partitionSeeds = data.partitions.map(partition => rand.nextLong)
val temp = data.mapPartitionsWithIndex((index, iter) => {
val partitionRand = new java.util.Random(partitionSeeds(index))
iter.map(x => (x, partitionRand.nextDouble))
})
(temp.filter(_._2 <= p).map(_._1), temp.filter(_._2 > p).map(_._1))
}
Re: RDD.subtract doesn't work
Posted by Mark Hamstra <ma...@clearstorydata.com>.
That's not really the best way to handle random number generation. There
have been multiple discussions on
https://groups.google.com/forum/?fromgroups=#!forum/spark-users and
elsewhere about how to use mapPartitions or mapWith to create
higher-performance Spark code that uses PRNGs.
On Thu, Sep 12, 2013 at 9:55 AM, Jason Lenderman <js...@gmail.com>wrote:
> Even if it worked, using subtract doesn't seem like a good way to achieve
> this. You could try something like:
>
> def split[T : ClassManifest](data: RDD[T], p: Double, seed: Long =
> System.currentTimeMillis): (RDD[T], RDD[T]) = {
> val rand = new java.util.Random(seed)
> val temp = data.map(x => (x, rand.nextDouble))
> (temp.filter(_._2 <= p).map(_._1), temp.filter(_._2 > p).map(_._1))
> }
>
> Note: this code compiles, but I haven't tested it yet...
>
>
> On Thu, Sep 12, 2013 at 1:18 AM, Hao REN <ju...@gmail.com> wrote:
>
>> Hi,
>>
>> I am writing a logistic regression prog with Spark based on SparkLR
>> example.
>>
>> Say, a data set containing 10000 DataPoints, where DataPoint is a case
>> class like: case class DataPoint(x: Vector, y: Double) as defined in
>> the SparkLR example.
>>
>> In order to divide the data set into 2 parts: training set and test set,
>> I tried some code below:
>>
>> val trainingSet = points.sample(false, 0.6, 7)
>> val testSet = points.subtract(trainingSet)
>>
>> ,where points is a RDD[DataPoint] contains 10000 points
>>
>> sample works well, trainingSet.count gives a number around 6000, but
>> testSet.count gives 10000 which is not the expected 4000.
>>
>> It seems that subtract cant work with some custom class, as DataPoint
>> here.
>>
>> 2 questions:
>>
>> 1) Which is the best way to divide data with a ratio, say 6/4, especially
>> when Data is not a primitive type, like some custom classes ?
>>
>> 2) Why subtract doesn't work ? Maybe ordering and compare should be
>> implemented for DataPoint class ?
>>
>>
>> I have also checked the SubtractedRDD class. Without background about the
>> Spark source code, I can not understand what the problem is.
>>
>> https://github.com/mesos/spark/blob/master/core/src/main/scala/org/apache/spark/rdd/SubtractedRDD.scala
>>
>>
>> Any help is highly appreciated !
>>
>> Thank you in advance. =)
>>
>> Hao
>>
>>
>> --
>> REN Hao
>>
>> Etudiant d'échange à l'Ecole Polytechnique Fédérale de Lausanne (EPFL)
>>
>> Computer Science
>>
>> Etudiant à l'Université de Technologie de Compiègne (UTC)
>>
>> Génie Informatique - Fouille de Données
>>
>> Tel: +33 06 14 54 57 24 / +41 07 86 47 52 69
>>
>
>
Re: RDD.subtract doesn't work
Posted by Jason Lenderman <js...@gmail.com>.
Even if it worked, using subtract doesn't seem like a good way to achieve
this. You could try something like:
def split[T : ClassManifest](data: RDD[T], p: Double, seed: Long =
System.currentTimeMillis): (RDD[T], RDD[T]) = {
val rand = new java.util.Random(seed)
val temp = data.map(x => (x, rand.nextDouble))
(temp.filter(_._2 <= p).map(_._1), temp.filter(_._2 > p).map(_._1))
}
Note: this code compiles, but I haven't tested it yet...
On Thu, Sep 12, 2013 at 1:18 AM, Hao REN <ju...@gmail.com> wrote:
> Hi,
>
> I am writing a logistic regression prog with Spark based on SparkLR
> example.
>
> Say, a data set containing 10000 DataPoints, where DataPoint is a case
> class like: case class DataPoint(x: Vector, y: Double) as defined in the
> SparkLR example.
>
> In order to divide the data set into 2 parts: training set and test set, I
> tried some code below:
>
> val trainingSet = points.sample(false, 0.6, 7)
> val testSet = points.subtract(trainingSet)
>
> ,where points is a RDD[DataPoint] contains 10000 points
>
> sample works well, trainingSet.count gives a number around 6000, but
> testSet.count gives 10000 which is not the expected 4000.
>
> It seems that subtract cant work with some custom class, as DataPoint
> here.
>
> 2 questions:
>
> 1) Which is the best way to divide data with a ratio, say 6/4, especially
> when Data is not a primitive type, like some custom classes ?
>
> 2) Why subtract doesn't work ? Maybe ordering and compare should be
> implemented for DataPoint class ?
>
>
> I have also checked the SubtractedRDD class. Without background about the
> Spark source code, I can not understand what the problem is.
>
> https://github.com/mesos/spark/blob/master/core/src/main/scala/org/apache/spark/rdd/SubtractedRDD.scala
>
>
> Any help is highly appreciated !
>
> Thank you in advance. =)
>
> Hao
>
>
> --
> REN Hao
>
> Etudiant d'échange à l'Ecole Polytechnique Fédérale de Lausanne (EPFL)
>
> Computer Science
>
> Etudiant à l'Université de Technologie de Compiègne (UTC)
>
> Génie Informatique - Fouille de Données
>
> Tel: +33 06 14 54 57 24 / +41 07 86 47 52 69
>