本文整理汇总了Python中test_helper.Test类的典型用法代码示例。如果您正苦于以下问题:Python Test类的具体用法?Python Test怎么用?Python Test使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Test类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: setUp
def setUp(self):
Test.setUp(self)
self.recorder = FakeRecorder()
self.uploader = FakeUploader()
self.confirmator = FakeConfirmator()
self.real_subprocess_call = subprocess.call
subprocess.call = lambda *args: None
开发者ID:tomekwojcik,项目名称:asciinema,代码行数:7,代码来源:record_test.py
示例2: hash
assert shakespeareCount == 122395
# ### ** Part 2: Check class testing library **
# #### ** (2a) Compare with hash **
# In[ ]:
# TEST Compare with hash (2a)
# Check our testing library/package
# This should print '1 test passed.' on two lines
from test_helper import Test
twelve = 12
Test.assertEquals(twelve, 12, "twelve should equal 12")
Test.assertEqualsHashed(
twelve, "7b52009b64fd0a2a49e6d8a939753077792b0554", "twelve, once hashed, should equal the hashed value of 12"
)
# #### ** (2b) Compare lists **
# In[ ]:
# TEST Compare lists (2b)
# This should print '1 test passed.'
unsortedList = [(5, "b"), (5, "a"), (4, "c"), (3, "a")]
Test.assertEquals(sorted(unsortedList), [(3, "a"), (4, "c"), (5, "a"), (5, "b")], "unsortedList does not sort properly")
开发者ID:pombredanne,项目名称:BigDataSpark,代码行数:29,代码来源:lab0_student.py
示例3: DenseVector
# COMMAND ----------
# MAGIC %md
# MAGIC Create a `DenseVector` with the values 1.5, 2.5, 3.0 (in that order).
# COMMAND ----------
# ANSWER
denseVec = Vectors.dense([1.5, 2.5, 3.0])
# COMMAND ----------
# TEST
from test_helper import Test
Test.assertEquals(denseVec, DenseVector([1.5, 2.5, 3.0]), 'incorrect value for denseVec')
# COMMAND ----------
# MAGIC %md
# MAGIC Create a `LabeledPoint` with a label equal to 10.0 and features equal to `denseVec`
# COMMAND ----------
# ANSWER
labeledP = LabeledPoint(10.0, denseVec)
# COMMAND ----------
# TEST
Test.assertEquals(str(labeledP), '(10.0,[1.5,2.5,3.0])', 'incorrect value for labeledP')
开发者ID:smoltis,项目名称:spark,代码行数:30,代码来源:1-mllib-datatypes_answers.py
示例4: vectors
# In[1]:
# TODO: Replace <FILL IN> with appropriate code
# Manually calculate your answer and represent the vector as a list of integers values.
# For example, [2, 4, 8].
x = [3, -6, 0]
y = [4, 8, 16]
# In[2]:
# TEST Scalar multiplication: vectors (1a)
# Import test library
from test_helper import Test
Test.assertEqualsHashed(x, 'e460f5b87531a2b60e0f55c31b2e49914f779981',
'incorrect value for vector x')
Test.assertEqualsHashed(y, 'e2d37ff11427dbac7f833a5a7039c0de5a740b1e',
'incorrect value for vector y')
# #### ** (1b) Element-wise multiplication: vectors **
# #### In this exercise, you will calculate the element-wise multiplication of two vectors by hand and enter the result in the code cell below. You'll later see that element-wise multiplication is the default method when two NumPy arrays are multiplied together. Note we won't be performing element-wise multiplication in future labs, but we are introducing it here to distinguish it from other vector operators, and to because it is a common operations in NumPy, as we will discuss in Part (2b).
# #### The element-wise calculation is as follows: $$ \mathbf{x} \odot \mathbf{y} = \begin{bmatrix} x_1 y_1 \\\ x_2 y_2 \\\ \vdots \\\ x_n y_n \end{bmatrix} $$
# #### Calculate the value of $ \mathbf{z} $: $$ \mathbf{z} = \begin{bmatrix} 1 \\\ 2 \\\ 3 \end{bmatrix} \odot \begin{bmatrix} 4 \\\ 5 \\\ 6 \end{bmatrix} $$
# In[3]:
# TODO: Replace <FILL IN> with appropriate code
# Manually calculate your answer and represent the vector as a list of integers values.
z = [4, 10, 18]
开发者ID:navink,项目名称:Apache-Spark_CS190.1x,代码行数:30,代码来源:ML_lab1_review_student.py
示例5: run_tests
def run_tests():
Test.assertEquals(test_year(1945, df), [u'Mary', u'Linda', u'Barbara', u'Patricia', u'Carol'], 'incorrect top 5 names for 1945')
Test.assertEquals(test_year(1970, df), [u'Jennifer', u'Lisa', u'Kimberly', u'Michelle', u'Amy'], 'incorrect top 5 names for 1970')
Test.assertEquals(test_year(1987, df), [u'Jessica', u'Ashley', u'Amanda', u'Jennifer', u'Sarah'], 'incorrect top 5 names for 1987')
Test.assertTrue(len(test_year(1945, df)) <= 5, 'list not limited to 5 names')
Test.assertTrue(u'James' not in test_year(1945, df), 'male names not filtered')
Test.assertTrue(test_year(1945, df) != [u'Linda', u'Linda', u'Linda', u'Linda', u'Mary'], 'year not filtered')
Test.assertEqualsHashed(test_year(1880, df), "2038e2c0bb0b741797a47837c0f94dbf24123447", "incorrect top 5 names for 1880")
开发者ID:smoltis,项目名称:spark,代码行数:8,代码来源:Lab.py
示例6:
sampleDataRDD = sc.parallelize([sampleOne, sampleTwo, sampleThree])
sampleOHEDictManual = {}
sampleOHEDictManual[(0,'bear')] = 0
sampleOHEDictManual[(0,'cat')] = 1
sampleOHEDictManual[(0,'mouse')] = 2
sampleOHEDictManual[(1,'black')] = 3
sampleOHEDictManual[(1,'tabby')] = 4
sampleOHEDictManual[(2,'mouse')] = 5
sampleOHEDictManual[(2,'salmon')]= 6
# TEST One-hot-encoding
from test_helper import Test
Test.assertEqualsHashed(sampleOHEDictManual[(0,'bear')],
'b6589fc6ab0dc82cf12099d1c2d40ab994e8410c',
"incorrect value for sampleOHEDictManual[(0,'bear')]")
Test.assertEqualsHashed(sampleOHEDictManual[(0,'cat')],
'356a192b7913b04c54574d18c28d46e6395428ab',
"incorrect value for sampleOHEDictManual[(0,'cat')]")
Test.assertEqualsHashed(sampleOHEDictManual[(0,'mouse')],
'da4b9237bacccdf19c0760cab7aec4a8359010b0',
"incorrect value for sampleOHEDictManual[(0,'mouse')]")
Test.assertEqualsHashed(sampleOHEDictManual[(1,'black')],
'77de68daecd823babbb58edb1c8e14d7106e83bb',
"incorrect value for sampleOHEDictManual[(1,'black')]")
Test.assertEqualsHashed(sampleOHEDictManual[(1,'tabby')],
'1b6453892473a467d07372d45eb05abc2031647a',
"incorrect value for sampleOHEDictManual[(1,'tabby')]")
Test.assertEqualsHashed(sampleOHEDictManual[(2,'mouse')],
'ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4',
开发者ID:samkujovich,项目名称:SparkExperience,代码行数:31,代码来源:ClickThroughPrediction.py
示例7: makePlural
# One way of completing the function
def makePlural(word):
return word + 's'
print makePlural('cat')
# In[4]:
# Load in the testing code and check to see if your answer is correct
# If incorrect it will report back '1 test failed' for each failed test
# Make sure to rerun any cell you change before trying the test again
from test_helper import Test
# TEST Pluralize and test (1b)
Test.assertEquals(makePlural('rat'), 'rats', 'incorrect result: makePlural does not add an s')
# #### ** (1c) Apply `makePlural` to the base RDD **
# #### Now pass each item in the base RDD into a [map()](http://spark.apache.org/docs/latest/api/python/pyspark.html#pyspark.RDD.map) transformation that applies the `makePlural()` function to each element. And then call the [collect()](http://spark.apache.org/docs/latest/api/python/pyspark.html#pyspark.RDD.collect) action to see the transformed RDD.
# In[7]:
# TODO: Replace <FILL IN> with appropriate code
pluralRDD = wordsRDD.map(makePlural)
print pluralRDD.collect()
# In[ ]:
# TEST Apply makePlural to the base RDD(1c)
开发者ID:harishashok,项目名称:Big-Data-with-Apache-Spark-,代码行数:30,代码来源:lab2_wordcount.py
示例8: estimateCovariance
plt.scatter(dataCorrelated[:,0], dataCorrelated[:,1], s=14**2, c='#d6ebf2',
edgecolors='#8cbfd0', alpha=0.75)
pass
correlatedData = sc.parallelize(dataCorrelated)
meanCorrelated = correlatedData.mean()
correlatedDataZeroMean = correlatedData.map(lambda x:np.subtract(x,meanCorrelated))
print meanCorrelated
print correlatedData.take(1)
print correlatedDataZeroMean.take(1)
from test_helper import Test
Test.assertTrue(np.allclose(meanCorrelated, [49.95739037, 49.97180477]),
'incorrect value for meanCorrelated')
Test.assertTrue(np.allclose(correlatedDataZeroMean.take(1)[0], [-0.28561917, 0.10351492]),
'incorrect value for correlatedDataZeroMean')
correlatedCov = correlatedDataZeroMean.map(lambda x: np.outer(x,x)).reduce(lambda x,y:x+y)/correlatedDataZeroMean.count()
print correlatedCov
covResult = [[ 0.99558386, 0.90148989], [0.90148989, 1.08607497]]
Test.assertTrue(np.allclose(covResult, correlatedCov), 'incorrect value for correlatedCov')
def estimateCovariance(data):
meanData = data.mean()
zeroMeanData = data.map(lambda x:np.subtract(x,meanData))
correlatedMatrix = zeroMeanData.map(lambda x: np.outer(x,x)).reduce(lambda x,y:x+y)/zeroMeanData.count()
return correlatedMatrix
开发者ID:JsNoNo,项目名称:Spark-Test-Program,代码行数:32,代码来源:PCAtest.py
示例9: calcUserMeanRating
# In[7]:
def calcUserMeanRating(userRatingGroup):
""" Calculate the average rating of a user
"""
userID = userRatingGroup[0]
ratingSum = 0.0
ratingCnt = len(userRatingGroup[1])
if ratingCnt == 0:
return (userID, 0.0)
for item in userRatingGroup[1]:
ratingSum += item[1]
return (userID, 1.0 * ratingSum / ratingCnt)
Test.assertEquals(calcUserMeanRating((123, [(1, 1), (2, 2), (3, 3)])),
(123, 2.0), 'incorrect calcUserMeanRating()')
# In[8]:
def broadcastUserRatingAvg(sContext, uRRDDTrain):
""" Broadcast the user average rating RDD
"""
userRatingAvgList = uRRDDTrain.map(lambda x: calcUserMeanRating(x)).collect()
userRatingAvgDict = {}
for (user, avgscore) in userRatingAvgList:
userRatingAvgDict[user] = avgscore
uRatingAvgBC = sContext.broadcast(userRatingAvgDict)# broadcast
return uRatingAvgBC
def predictUsingAvg(tup, avgDict):
开发者ID:NathanLvzs,项目名称:MovieRecommendationSpark,代码行数:31,代码来源:Movie_Recommendation_on_Apache_Spark.py
示例10: display
# MAGIC
# MAGIC The resulting `DataFrame` should have two columns: one named `features` and another named `label`.
# COMMAND ----------
# ANSWER
from pyspark.sql.functions import col
irisDFZeroIndex = irisDF.select('features', (col('label') - 1).alias('label'))
display(irisDFZeroIndex)
# COMMAND ----------
# TEST
from test_helper import Test
Test.assertEquals(irisDFZeroIndex.select('label').map(lambda r: r[0]).take(3), [0, 0, 0],
'incorrect value for irisDFZeroIndex')
# COMMAND ----------
# MAGIC %md
# MAGIC You'll also notice that we have four values for features and that those values are stored as a `SparseVector`. We'll reduce those down to two values (for visualization purposes) and convert them to a `DenseVector`. To do that we'll need to create a `udf` and apply it to our dataset. Here's a `udf` reference for [Python](http://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.functions.udf) and for [Scala](https://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql.UserDefinedFunction).
# MAGIC
# MAGIC Note that you can call the `toArray` method on a `SparseVector` to obtain an array, and you can convert an array into a `DenseVector` using the `Vectors.dense` method.
# COMMAND ----------
# ANSWER
from pyspark.sql.functions import udf
# Note that VectorUDT and MatrixUDT are found in linalg while other types are in sql.types
# VectorUDT should be the return type of the udf
from pyspark.mllib.linalg import Vectors, VectorUDT
开发者ID:Inscrutive,项目名称:spark,代码行数:32,代码来源:B.py
示例11: display
# TODO: Replace <FILL IN> with appropriate code
from pyspark.ml.feature import StringIndexer
stringIndexer = (<FILL IN>
.<FILL IN>
.<FILL IN>)
indexerModel = stringIndexer.<FILL IN>
irisTrainIndexed = indexerModel.<FILL IN>
display(irisTrainIndexed)
# COMMAND ----------
# TEST
from test_helper import Test
Test.assertEquals(irisTrainIndexed.select('indexed').take(50)[-1][0], 2.0, 'incorrect values in indexed column')
Test.assertTrue(irisTrainIndexed.schema.fields[2].metadata != {}, 'indexed should have metadata')
# COMMAND ----------
# MAGIC %md
# MAGIC We've updated the metadata for the field. Now we know that the field takes on three values and is nominal.
# COMMAND ----------
print irisTrainIndexed.schema.fields[1].metadata
print irisTrainIndexed.schema.fields[2].metadata
# COMMAND ----------
# MAGIC %md
开发者ID:smoltis,项目名称:spark,代码行数:31,代码来源:4-trees_student.py
示例12: float
# Remember to cast the value you extract from the Vector using float()
getElement = udf(lambda v, i: float(v[i]), DoubleType())
irisSeparateFeatures = (irisTwoFeatures
.withColumn('sepalLength', getElement('features', lit(0)))
.withColumn('sepalWidth', getElement('features', lit(1))))
display(irisSeparateFeatures)
# COMMAND ----------
# TEST
from test_helper import Test
firstRow = irisSeparateFeatures.select('sepalWidth', 'features').map(lambda r: (r[0], r[1])).first()
Test.assertEquals(firstRow[0], firstRow[1][1], 'incorrect definition for getElement')
# COMMAND ----------
# MAGIC %md
# MAGIC What about using `Column`'s `getItem` method?
# COMMAND ----------
from pyspark.sql.functions import col
display(irisTwoFeatures.withColumn('sepalLength', col('features').getItem(0)))
# COMMAND ----------
# MAGIC %md
开发者ID:Inscrutive,项目名称:spark,代码行数:30,代码来源:II.py
示例13: makePlural
# One way of completing the function
def makePlural(word):
return word + 's'
print makePlural('cat')
# In[8]:
# Load in the testing code and check to see if your answer is correct
# If incorrect it will report back '1 test failed' for each failed test
# Make sure to rerun any cell you change before trying the test again
from test_helper import Test
# TEST Pluralize and test (1b)
Test.assertEquals(makePlural('rat'), 'rats', 'incorrect result: makePlural does not add an s')
# #### ** (1c) Apply `makePlural` to the base RDD **
# #### Now pass each item in the base RDD into a [map()](http://spark.apache.org/docs/latest/api/python/pyspark.html#pyspark.RDD.map) transformation that applies the `makePlural()` function to each element. And then call the [collect()](http://spark.apache.org/docs/latest/api/python/pyspark.html#pyspark.RDD.collect) action to see the transformed RDD.
# In[9]:
# TODO: Replace <FILL IN> with appropriate code
wordsList = ['cat', 'elephant', 'rat', 'rat', 'cat']
wordsRDD = sc.parallelize(wordsList, 4)
def makePlural(word):
return word + 's'
pluralRDD = wordsRDD.map(makePlural)
print pluralRDD.collect()
开发者ID:Mvrm,项目名称:Spark,代码行数:29,代码来源:2.py
示例14: display
# COMMAND ----------
# TODO: Replace <FILL IN> with appropriate code
# Create a new DataFrame with the features from irisDF and with labels that are zero-indexed (just subtract one).
# Also make sure your label column is still called label.
from pyspark.sql.functions import col
irisDFZeroIndex = irisDF.<FILL IN>
display(irisDFZeroIndex)
# COMMAND ----------
# TEST
from test_helper import Test
Test.assertEquals(irisDFZeroIndex.select('label').map(lambda r: r[0]).take(3), [0, 0, 0],
'incorrect value for irisDFZeroIndex')
# COMMAND ----------
# MAGIC %md
# MAGIC You'll also notice that we have four values for features and that those values are stored as a `SparseVector`. We'll reduce those down to two values (for visualization purposes) and convert them to a `DenseVector`. To do that we'll need to create a `udf` and apply it to our dataset. Here's a `udf` reference for [Python](http://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.functions.udf) and for [Scala](https://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql.UserDefinedFunction).
# MAGIC
# MAGIC Note that you can call the `toArray` method on a `SparseVector` to obtain an array, and you can convert an array into a `DenseVector` using the `Vectors.dense` method.
# COMMAND ----------
# TODO: Replace <FILL IN> with appropriate code
from pyspark.sql.functions import udf
# Note that VectorUDT and MatrixUDT are found in linalg while other types are in sql.types
# VectorUDT should be the return type of the udf
开发者ID:smoltis,项目名称:spark,代码行数:32,代码来源:2-etl-kmeans_part1_student.py
示例15: setUp
def setUp(self):
Test.setUp(self)
self.recorder = FakeRecorder()
self.uploader = FakeUploader()
self.confirmator = FakeConfirmator()
开发者ID:caseyscarborough,项目名称:asciinema,代码行数:5,代码来源:record_test.py
示例16: setUp
def setUp(self):
Test.setUp(self)
self.real_stdin = sys.stdin
sys.stdin = self.stdin = FakeStdin()
开发者ID:caseyscarborough,项目名称:asciinema,代码行数:4,代码来源:confirmator_test.py
示例17: tearDown
def tearDown(self):
Test.tearDown(self)
sys.stdin = self.real_stdin
开发者ID:caseyscarborough,项目名称:asciinema,代码行数:3,代码来源:confirmator_test.py
示例18: len
# MAGIC %md
# MAGIC First, create a `DataFrame` named sized that has a `size` column with the size of each array of words. Here you can use `func.size`.
# COMMAND ----------
# ANSWER
sized = noStopWords.withColumn('size', func.size('words'))
sizedFirst = sized.select('size', 'words').first()
print sizedFirst[0]
# COMMAND ----------
# TEST
from test_helper import Test
Test.assertEquals(sizedFirst[0], len(sizedFirst[1]), 'incorrect implementation for sized')
# COMMAND ----------
# MAGIC %md
# MAGIC Next, you'll need to aggregate the counts. You can do this using `func.sum` in either a `.select` or `.agg` method call on the `DataFrame`. Make sure to give your `Column` the alias `numberOfWords`. There are some examples in [Python](http://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.GroupedData.agg) and [Scala](https://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql.DataFrame) in the APIs.
# COMMAND ----------
# ANSWER
numberOfWords = sized.agg(func.sum('size').alias('numberOfWords'))
wordCount = numberOfWords.first()[0]
print wordCount
# COMMAND ----------
开发者ID:smoltis,项目名称:spark,代码行数:31,代码来源:wiki-eda_answers.py
示例19: makePlural
def makePlural(word):
return word + "s"
print makePlural("cat")
# In[5]:
# Load in the testing code and check to see if your answer is correct
# If incorrect it will report back '1 test failed' for each failed test
# Make sure to rerun any cell you change before trying the test again
from test_helper import Test
# TEST Pluralize and test (1b)
Test.assertEquals(makePlural("rat"), "rats", "incorrect result: makePlural does not add an s")
# #### ** (1c) Apply `makePlural` to the base RDD **
# #### Now pass each item in the base RDD into a [map()](http://spark.apache.org/docs/latest/api/python/pyspark.html#pyspark.RDD.map) transformation that applies the `makePlural()` function to each element. And then call the [collect()](http://spark.apache.org/docs/latest/api/python/pyspark.html#pyspark.RDD.collect) action to see the transformed RDD.
# In[6]:
# TODO: Replace <FILL IN> with appropriate code
pluralRDD = wordsRDD.map(makePlural)
print pluralRDD.collect()
# In[7]:
# TEST Apply makePlural to the base RDD(1c)
开发者ID:luckylouis,项目名称:MachineLearningSpark,代码行数:31,代码来源:lab1_word_count_student.py
示例20: float
# Remember to cast the value you extract from the Vector using float()
getElement = udf(lambda v, i: float(v[i]), DoubleType())
irisSeparateFeatures = (irisTwoFeatures
.withColumn('sepalLength', getElement('features', lit(0)))
.withColumn('sepalWidth', getElement('features', lit(1))))
display(irisSeparateFeatures)
# COMMAND ----------
# TEST
from test_helper import Test
firstRow = irisSeparateFeatures.select('sepalWidth', 'features').map(lambda r: (r[0], r[1])).first()
Test.assertEquals(firstRow[0], firstRow[1][1], 'incorrect definition for getElement')
# COMMAND ----------
# MAGIC %md
# MAGIC What about using `Column`'s `getItem` method?
# COMMAND ----------
from pyspark.sql.functions import col
from pyspark.sql.utils import AnalysisException
try:
display(irisTwoFeatures.withColumn('sepalLength', col('features').getItem(0)))
except AnalysisException as e:
print e
开发者ID:smoltis,项目名称:spark,代码行数:30,代码来源:3-pipeline-logistic_part1_answers.py
注:本文中的test_helper.Test类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论