forked from alteryx/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into countDistinctPartial
- Loading branch information
Showing
42 changed files
with
818 additions
and
79 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -77,3 +77,5 @@ | |
output = cass_rdd.collect() | ||
for (k, v) in output: | ||
print (k, v) | ||
|
||
sc.stop() |
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 |
---|---|---|
|
@@ -71,3 +71,5 @@ | |
output = hbase_rdd.collect() | ||
for (k, v) in output: | ||
print (k, v) | ||
|
||
sc.stop() |
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 |
---|---|---|
|
@@ -77,3 +77,5 @@ def closestPoint(p, centers): | |
kPoints[x] = y | ||
|
||
print "Final centers: " + str(kPoints) | ||
|
||
sc.stop() |
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 |
---|---|---|
|
@@ -80,3 +80,5 @@ def add(x, y): | |
w -= points.map(lambda m: gradient(m, w)).reduce(add) | ||
|
||
print "Final w: " + str(w) | ||
|
||
sc.stop() |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
""" | ||
Correlations using MLlib. | ||
""" | ||
|
||
import sys | ||
|
||
from pyspark import SparkContext | ||
from pyspark.mllib.regression import LabeledPoint | ||
from pyspark.mllib.stat import Statistics | ||
from pyspark.mllib.util import MLUtils | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) not in [1,2]: | ||
print >> sys.stderr, "Usage: correlations (<file>)" | ||
exit(-1) | ||
sc = SparkContext(appName="PythonCorrelations") | ||
if len(sys.argv) == 2: | ||
filepath = sys.argv[1] | ||
else: | ||
filepath = 'data/mllib/sample_linear_regression_data.txt' | ||
corrType = 'pearson' | ||
|
||
points = MLUtils.loadLibSVMFile(sc, filepath)\ | ||
.map(lambda lp: LabeledPoint(lp.label, lp.features.toArray())) | ||
|
||
print 'Summary of data file: ' + filepath | ||
print '%d data points' % points.count() | ||
|
||
# Statistics (correlations) | ||
print 'Correlation (%s) between label and each feature' % corrType | ||
print 'Feature\tCorrelation' | ||
numFeatures = points.take(1)[0].features.size | ||
labelRDD = points.map(lambda lp: lp.label) | ||
for i in range(numFeatures): | ||
featureRDD = points.map(lambda lp: lp.features[i]) | ||
corr = Statistics.corr(labelRDD, featureRDD, corrType) | ||
print '%d\t%g' % (i, corr) | ||
|
||
sc.stop() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# | ||
# 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 generated RDDs. | ||
""" | ||
|
||
import sys | ||
|
||
from pyspark import SparkContext | ||
from pyspark.mllib.random import RandomRDDs | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) not in [1, 2]: | ||
print >> sys.stderr, "Usage: random_rdd_generation" | ||
exit(-1) | ||
|
||
sc = SparkContext(appName="PythonRandomRDDGeneration") | ||
|
||
numExamples = 10000 # number of examples to generate | ||
fraction = 0.1 # fraction of data to sample | ||
|
||
# Example: RandomRDDs.normalRDD | ||
normalRDD = RandomRDDs.normalRDD(sc, numExamples) | ||
print 'Generated RDD of %d examples sampled from the standard normal distribution'\ | ||
% normalRDD.count() | ||
print ' First 5 samples:' | ||
for sample in normalRDD.take(5): | ||
print ' ' + str(sample) | ||
|
||
# Example: RandomRDDs.normalVectorRDD | ||
normalVectorRDD = RandomRDDs.normalVectorRDD(sc, numRows = numExamples, numCols = 2) | ||
print 'Generated RDD of %d examples of length-2 vectors.' % normalVectorRDD.count() | ||
print ' First 5 samples:' | ||
for sample in normalVectorRDD.take(5): | ||
print ' ' + str(sample) | ||
|
||
sc.stop() |
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 |
---|---|---|
@@ -0,0 +1,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) | ||
|
||
|
||
# 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() |
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 |
---|---|---|
|
@@ -34,3 +34,5 @@ | |
output = sortedCount.collect() | ||
for (num, unitcount) in output: | ||
print num | ||
|
||
sc.stop() |
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 |
---|---|---|
|
@@ -64,3 +64,5 @@ def generateGraph(): | |
break | ||
|
||
print "TC has %i edges" % tc.count() | ||
|
||
sc.stop() |
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 |
---|---|---|
|
@@ -33,3 +33,5 @@ | |
output = counts.collect() | ||
for (word, count) in output: | ||
print "%s: %i" % (word, count) | ||
|
||
sc.stop() |
Oops, something went wrong.