aboutsummaryrefslogtreecommitdiff
path: root/examples/src/main/python/mllib/sampled_rdds.py
blob: ec64a5978c6729e216543b5f6b3df8de28e6585e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""
Randomly sampled RDDs.
"""

import sys

from pyspark import SparkContext
from pyspark.mllib.util import MLUtils


if __name__ == "__main__":
    if len(sys.argv) not in [1, 2]:
        print >> sys.stderr, "Usage: sampled_rdds <libsvm data file>"
        exit(-1)
    if len(sys.argv) == 2:
        datapath = sys.argv[1]
    else:
        datapath = 'data/mllib/sample_binary_classification_data.txt'

    sc = SparkContext(appName="PythonSampledRDDs")

    fraction = 0.1 # fraction of data to sample

    examples = MLUtils.loadLibSVMFile(sc, datapath)
    numExamples = examples.count()
    if numExamples == 0:
        print >> sys.stderr, "Error: Data file had no samples to load."
        exit(1)
    print 'Loaded data with %d examples from file: %s' % (numExamples, datapath)

    # Example: RDD.sample() and RDD.takeSample()
    expectedSampleSize = int(numExamples * fraction)
    print 'Sampling RDD using fraction %g.  Expected sample size = %d.' \
        % (fraction, expectedSampleSize)
    sampledRDD = examples.sample(withReplacement = True, fraction = fraction)
    print '  RDD.sample(): sample has %d examples' % sampledRDD.count()
    sampledArray = examples.takeSample(withReplacement = True, num = expectedSampleSize)
    print '  RDD.takeSample(): sample has %d examples' % len(sampledArray)

    print

    # Example: RDD.sampleByKey()
    keyedRDD = examples.map(lambda lp: (int(lp.label), lp.features))
    print '  Keyed data using label (Int) as key ==> Orig'
    #  Count examples per label in original data.
    keyCountsA = keyedRDD.countByKey()

    #  Subsample, and count examples per label in sampled data.
    fractions = {}
    for k in keyCountsA.keys():
        fractions[k] = fraction
    sampledByKeyRDD = keyedRDD.sampleByKey(withReplacement = True, fractions = fractions)
    keyCountsB = sampledByKeyRDD.countByKey()
    sizeB = sum(keyCountsB.values())
    print '  Sampled %d examples using approximate stratified sampling (by label). ==> Sample' \
        % sizeB

    #  Compare samples
    print '   \tFractions of examples with key'
    print 'Key\tOrig\tSample'
    for k in sorted(keyCountsA.keys()):
        fracA = keyCountsA[k] / float(numExamples)
        if sizeB != 0:
            fracB = keyCountsB.get(k, 0) / float(sizeB)
        else:
            fracB = 0
        print '%d\t%g\t%g' % (k, fracA, fracB)

    sc.stop()