Exemplo n.º 1
0
def lab1_test_ex_2(dummy_divisible_by_3):
    res = dummy_divisible_by_3.take(10) + dummy_divisible_by_3.top(10)
    f_10 = dummy_divisible_by_3.take(10)
    Test.assertEqualsHashed(
        res, 'f560a8e93e904ba6eafad0c41ff0ad52e8db37ee',
        'Incorrect RDD: [{}, ...]'.format(list_continious(f_10)),
        'Correct RDD: [{}, ...]'.format(list_continious(f_10)))
Exemplo n.º 2
0
def lab4_test_ex_8(average, RMSE):
    Test.assertEqualsHashed(
        average, '48da600e257d511cd9a7db7ce4fa34ae6b92748e',
        'incorrect average purchase (expected 3.8254751809)',
        'Test 8.1 is successful')
    Test.assertEqualsHashed(RMSE, '215eafc55a1caa2db0dd959fcba648e27d270cc9',
                            'incorrect RMSE (expected 4.99122318964)',
                            'Test 8.2 is successful')
Exemplo n.º 3
0
Arquivo: Lab.py Projeto: smoltis/spark
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")
Exemplo n.º 4
0
def lab4_test_ex_4_3(purchases):
    Test.assertEqualsHashed(
        [purchases.take(5), purchases.count()],
        '27475424fbfde8a2a48b480a5db01eb3f9884182',
        #'c1fdc61ea8e59eec85a99cc2c017831baa4f4051',
        'Incorrect purchases RDD: [{}, ...]'.format(
            list_continious(purchases.take(5))),
        'Correct purchases RDD: [{}, ...]'.format(
            list_continious(purchases.take(5))))
Exemplo n.º 5
0
def lab1_test_ex_4(letters, occurrence):
    res = letters.collect()
    Test.assertEqualsHashed(res, '19275f3ffa39938f61d208d6b310835fb18b759f',
                            'Incorrect RDD: {}'.format(res),
                            'Correct RDD: {}'.format(res))

    Test.assertEqualsHashed(occurrence,
                            '22d71673a52391bd1724694ce8a2ec0238356522',
                            'Incorrect dict: {}'.format(occurrence),
                            'Correct dict: {}'.format(occurrence))
Exemplo n.º 6
0
def lab1_test_ex_6(number_pairs, number_duplicates):
    res1 = number_pairs.collect()
    Test.assertEqualsHashed(res1, '19e2885cbed30b9b2cfc220cf979b1d137e11f52',
                            'Incorrect RDD: {}'.format(res1),
                            'Correct RDD: {}'.format(res1))

    res2 = number_duplicates.collect()
    Test.assertEqualsHashed(res2, '84df916b883f187ed27a3109cf14bb4bd80e78e9',
                            'Incorrect RDD: {}'.format(res2),
                            'Correct RDD: {}'.format(res2))
Exemplo n.º 7
0
def lab3_test_ex_7_5(test_error_lr, test_error_svm):
    Test.assertEqualsHashed(test_error_lr <= 0.23,
                            '88b33e4e12f75ac8bf792aebde41f1a090f3a612',
                            'Incorrect LogisticRegression err calculation',
                            'Correct LogisticRegression err calculation')

    Test.assertEqualsHashed(test_error_svm <= 0.23,
                            '88b33e4e12f75ac8bf792aebde41f1a090f3a612',
                            'Incorrect SVMWithSGD err calculation',
                            'Correct SVMWithSGD err calculation')
Exemplo n.º 8
0
def lab1_test_ex_7(inventory_diff, inventory_diff_sorted):
    res1 = inventory_diff.collect()
    Test.assertEqualsHashed(res1, '3af93b5e352a178963b6f6d72986edfc5a20aa6c',
                            'Incorrect RDD: {}'.format(res1),
                            'Correct RDD: {}'.format(res1))

    res2 = inventory_diff_sorted.collect()
    Test.assertEqualsHashed(res2, '89066489ead47cae2fa3737e8c4a82bc3e9afbd6',
                            'Incorrect RDD: {}'.format(res2),
                            'Correct RDD: {}'.format(res2))
Exemplo n.º 9
0
def lab3_test_ex_5_1(squared_error, example_RMSE):
    Test.assertEqualsHashed(squared_error(3, 1),
                            '1b6453892473a467d07372d45eb05abc2031647a',
                            'Incorrect squared_error function',
                            'Correct squared_error function')

    Test.assertEqualsHashed(example_RMSE,
                            '4b322e37f07576b9230316f90b84285b5d096941',
                            'Incorrect example_error: {}'.format(example_RMSE),
                            'Correct example_error: {}'.format(example_RMSE))
Exemplo n.º 10
0
def lab2_test_ex_1(logs):
    c = logs.count()
    t = logs.top(2)
    Test.assertEqualsHashed(c, 'd779e44c393784ee42c0baf60e5f787f2ab64d81',
                            'Incorrect logs count: {}'.format(c),
                            'Correct logs count: {}'.format(c))

    Test.assertEqualsHashed(
        t, '80b7732e143c2f3f867e9fdd0118c2ad6e47c141',
        'Incorrect logs: [{}, ...]'.format(list_continious(logs.take(2))),
        'Correct logs: [{}, ...]'.format(list_continious(logs.take(2))))
Exemplo n.º 11
0
def lab3_test_ex_7_1(parsed_groups, train_data, test_data):
    Test.assertEqualsHashed(
        parsed_groups.map(lambda lp: lp.label).sum(),
        '0ce7925bf636cbafa9005ce2d9673bc31b8122a6', 'Incorect splitting',
        'Correct splitting')

    Test.assertEqualsHashed(
        train_data.map(lambda lp: lp.label).sum() +
        test_data.map(lambda lp: lp.label).sum(),
        '0ce7925bf636cbafa9005ce2d9673bc31b8122a6', 'Incorect splitting',
        'Correct splitting')
Exemplo n.º 12
0
def lab3_test_ex_5_3(val_RMSE, best_RMSE):
    Test.assertEqualsHashed(
        abs(val_RMSE - 1.52408180718) < 1,
        '88b33e4e12f75ac8bf792aebde41f1a090f3a612',
        'Incorrect val_RMSE: {}'.format(val_RMSE),
        'Correct val_RMSE: {}'.format(val_RMSE))

    Test.assertEqualsHashed(
        abs(best_RMSE - 1.524081788) < 1,
        '88b33e4e12f75ac8bf792aebde41f1a090f3a612',
        'Incorrect best_RMSE: {}'.format(best_RMSE),
        'Correct best_RMSE: {}'.format(best_RMSE))
Exemplo n.º 13
0
def lab4_test_ex_7(RMSE, predictions):
    Test.assertTrue(
        abs(RMSE - 4.72051562103) < 0.5,
        'incorrect RMSE value (expected about 4.721)', 'Test 7.1 - success')
    Test.assertEqualsHashed(
        predictions.take(3)[1], '11ec7603d09fbc0af0517cacd4d162f4a9e18196',
        'incorrect first 3 elements of the predictions', 'Test 7.2 - success')
    Test.assertEqualsHashed(
        abs(predictions.take(5)[4][2] - 5.048388885082981) < 0.2,
        '88b33e4e12f75ac8bf792aebde41f1a090f3a612',
        'incorrect prediction for the element in predictions',
        'Test 7.3 - success')
Exemplo n.º 14
0
def lab4_test_ex_4_1(indexed_buyers, indexed_products):
    l1 = len(indexed_buyers)
    Test.assertEqualsHashed(
        l1, '0029723da11a640ffd2eec846a9a2cca6285831b',
        'Incorrect length of indexed_buyers: {}'.format(l1),
        'Correct length of indexed_buyers: {}'.format(l1))

    l2 = len(indexed_products)
    Test.assertEqualsHashed(
        l2, '2e0ab5b38ac3a139b7527efc5eec2118105b82b0',
        'Incorrect length of indexed_buyers: {}'.format(l2),
        'Correct length of indexed_buyers: {}'.format(l2))
Exemplo n.º 15
0
def lab4_test_ex_3(recommended_products):
    l = len(recommended_products)
    Test.assertEqualsHashed(l, '91032ad7bbcb6cf72875e8e8207dcfba80173f7c',
                            'Incorrect number of products: {}'.format(l),
                            'Correct number of products: {}'.format(l))

    Test.assertEqualsHashed(
        recommended_products,
        '389979fae5f36cafbd562ea5c80f5660d42eaefb',
        #'d5ee60603688f50e1d4bbf18e174866db11c1b99',
        'Incorrect products: [{}, ...]'.format(
            list_continious(recommended_products)),
        'Correct products: [{}, ...]'.format(
            list_continious(recommended_products[0:3])))
Exemplo n.º 16
0
def lab4_test_ex_1(get_counts_and_averages):
    data = [(1, (1, 2, 3, 4)), (100, (10.0, 20.0, 30.0)), (110, xrange(20))]

    expectations = [
        '7650c6f17f08af462b87700b3d78f4007675a7db',
        '878cc669276bfb82d2953b2e50896b7e1e1e1c4f',
        '8d7a82af2322875247f75cac0f18ff522101b9c6'
    ]

    for (args, expectation) in zip(data, expectations):
        result = get_counts_and_averages(args)
        Test.assertEqualsHashed(result, expectation,
                                'Test failed for: {}'.format(args),
                                '{} -> {}'.format(args, result))
Exemplo n.º 17
0
def lab3_test_ex_2_2(parsed_points, shifted_parsed_points):
    old_features = parsed_points.first().features
    new_features = shifted_parsed_points.first().features
    diff_features = old_features - new_features

    new_min_rating = shifted_parsed_points.map(lambda lp: lp.label).min()
    new_max_rating = shifted_parsed_points.map(lambda lp: lp.label).max()
    new_rating_range = new_max_rating - new_min_rating

    Test.assertEqualsHashed(diff_features,
                            '0bf31a85e53f1ce22604702e55bbf7f41ebde965',
                            'New features are not calculated correctly',
                            'New features calculated correctly')

    Test.assertEqualsHashed(new_rating_range,
                            '2ce6ddc84d93a3a1dcd84238cd67260bd7272e08',
                            'New year range is not calculated correctly',
                            'New year range calculated correctly')
Exemplo n.º 18
0
def lab3_test_ex_4_1(weights, intercept):
    expected_intercept = 1.00003687092
    expected_weights = [
        0.000250792702944, 9.87942310023e-06, 1.22220831068e-05,
        0.000221312646545, 1.58457070761e-06, 0.00128775449573,
        0.00488322878768, 3.66514390106e-05, 0.000117928277594,
        1.81713979186e-05, 0.000399983541013
    ]

    Test.assertEqualsHashed(
        abs(intercept - expected_intercept) < 1,
        '88b33e4e12f75ac8bf792aebde41f1a090f3a612',
        'Incorrect intercept: {}'.format(intercept),
        'Correct intercept: {}'.format(intercept))

    Test.assertEqualsHashed(
        abs(weights[0] - expected_weights[0]) < 1,
        '88b33e4e12f75ac8bf792aebde41f1a090f3a612',
        'Incorrect weights: {}'.format(weights),
        'Correct weights: {}'.format(weights))
Exemplo n.º 19
0
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")
Exemplo n.º 20
0
def lab2_test_ex_2(csa, csmin, csmax):
    Test.assertEqualsHashed(csa, '3b37b833000b5646f0ea8606a5e980b5728c77c4',
                            'Incorrect avg size: {}'.format(csa),
                            'Correct avg size: {}'.format(csa))

    Test.assertEqualsHashed(csmin, 'b6589fc6ab0dc82cf12099d1c2d40ab994e8410c',
                            'Incorrect min size: {}'.format(csmin),
                            'Correct min size: {}'.format(csmin))

    Test.assertEqualsHashed(csmax, '23ecf47a03f192561239cc7da9ea3e8755c8c0ee',
                            'Incorrect max size: {}'.format(csmax),
                            'Correct max size: {}'.format(csmax))
Exemplo n.º 21
0
def lab1_test_ex_3(dummy_less_than_50, dummy_top_5, dummy_product):
    res = dummy_less_than_50.collect()
    Test.assertEqualsHashed(res, '3e4e1750c95be2c366128d69d78aea2afde114a8',
                            'Incorrect RDD: {}'.format(res),
                            'Correct RDD: {}'.format(res))

    Test.assertEqualsHashed(dummy_top_5,
                            '8ff5b2b55bc143e048bf78182ec3194f1e49e0c7',
                            'Incorrect RDD: {}'.format(dummy_top_5),
                            'Correct RDD: {}'.format(dummy_top_5))

    Test.assertEqualsHashed(dummy_product,
                            'cb78c76bd7a7cfe8feb17ad7a4e1166dcfbbb7d3',
                            'Incorrect product: {}'.format(dummy_product),
                            'Correct product: {}'.format(dummy_product))
Exemplo n.º 22
0
def lab3_test_ex_2_1(min_rating, max_rating, rating_range):
    Test.assertEqualsHashed(min_rating,
                            'bdc1408a91f161dab5a9893d23db3c7095200e1d',
                            'Incorrect min rating: {}'.format(min_rating),
                            'Correct min rating: {}'.format(min_rating))

    Test.assertEqualsHashed(max_rating,
                            'b85ab32eaa572c8016edf68011078dceed8149e5',
                            'Incorrect max rating: {}'.format(max_rating),
                            'Correct max rating: {}'.format(max_rating))

    Test.assertEqualsHashed(rating_range,
                            '2ce6ddc84d93a3a1dcd84238cd67260bd7272e08',
                            'Incorrect rating range: {}'.format(rating_range),
                            'Correct rating range: {}'.format(rating_range))
Exemplo n.º 23
0
def lab3_test_ex_6_2(train_data, test_data, parsed_intervals):
    Test.assertEqualsHashed(
        train_data.map(lambda lp: lp.label).sum(),
        '95790ce05be9850d7bb5d2c4e8f8d2cb9c3476d6', 'Incorect splitting',
        'Correct splitting')

    Test.assertEqualsHashed(
        test_data.map(lambda lp: lp.label).sum(),
        'dd10ec4543eef62a4eff4ce4f324ca036df08b4c', 'Incorect splitting',
        'Correct splitting')

    Test.assertEqualsHashed(
        np.allclose(
            parsed_intervals.map(lambda lp: lp.label).sum(),
            train_data.map(lambda lp: lp.label).sum() +
            test_data.map(lambda lp: lp.label).sum()),
        '88b33e4e12f75ac8bf792aebde41f1a090f3a612', 'Incorect splitting',
        'Correct splitting')
Exemplo n.º 24
0
def lab1_test_ex_5(pairs, flat_pairs, flattened_pairs):
    res1 = pairs.take(5) + pairs.top(5)
    Test.assertEqualsHashed(
        res1, 'fb52be8957a9bdf4eaf329b26cdb707cdec41135',
        'Incorrect RDD: [{}, ...]'.format(list_continious(res1)),
        'Correct RDD: [{}, ...]'.format(list_continious(res1)))

    res2 = flat_pairs.take(10) + flat_pairs.top(10)
    Test.assertEqualsHashed(
        res2, 'a0e5a552e17d1c96511d392768c197e7318ec479',
        'Incorrect RDD: [{}, ...]'.format(list_continious(res2)),
        'Correct RDD: [{}, ...]'.format(list_continious(res2)))

    res3 = flattened_pairs.take(10) + flattened_pairs.top(10)
    Test.assertEqualsHashed(
        res3, 'a0e5a552e17d1c96511d392768c197e7318ec479',
        'Incorrect RDD: [{}, ...]'.format(list_continious(res3)),
        'Correct RDD: [{}, ...]'.format(list_continious(res3)))
Exemplo n.º 25
0
def lab3_test_ex_1(first_point_label, first_point_features):
    Test.assertEqualsHashed(
        len(first_point_features), '17ba0791499db908433b80f37c5fbc89b870084b',
        'Incorrect number of features: {}'.format(len(first_point_features)),
        'Correct number of features: {}'.format(len(first_point_features)))

    Test.assertEqualsHashed(first_point_label,
                            '2ce6ddc84d93a3a1dcd84238cd67260bd7272e08',
                            'Incorrect label: {}'.format(first_point_label),
                            'Correct label: {}'.format(first_point_label))

    expected_x0 = [
        7.0, 0.27, 0.36, 20.7, 0.045, 45.0, 170.0, 1.001, 3.0, 0.45, 8.8
    ]
    Test.assertEqualsHashed(
        (first_point_features - expected_x0),
        '0bf31a85e53f1ce22604702e55bbf7f41ebde965',
        'Incorrect features: {}'.format(first_point_features),
        'Correct features: {}'.format(first_point_features))
Exemplo n.º 26
0
def lab4_test_ex_2(grouped_purchases, average_purchases):
    gpc = grouped_purchases.count()
    Test.assertEqualsHashed(
        gpc, '2e0ab5b38ac3a139b7527efc5eec2118105b82b0',
        'Incorrect length of "grouped_purchases": {}'.format(gpc),
        'Length of `grouped_purchases` is {}'.format(gpc))

    apc = average_purchases.count()
    Test.assertEqualsHashed(
        apc, '2e0ab5b38ac3a139b7527efc5eec2118105b82b0',
        'Incorrect length of "average_purchases": {}'.format(apc),
        'Length of `average_purchases` is {}'.format(apc))

    ap = average_purchases.takeOrdered(3)
    Test.assertEqualsHashed(
        ap,
        'b66b7ed8a7d8127b31625f4d0ce631bb4cc8e232',
        #'b0eb8bf0055def9cb27b14faea14e9873afe3d52',
        'Incorrect `average_purchases`: {}'.format(ap),
        'Correct `average_purchases`: {}'.format(ap))
Exemplo n.º 27
0
def lab3_test_ex_3(n_partitions, train_data, val_data, test_data):
    sf2 = train_data.map(lambda lp: lp.features[2]).sum()
    sf3 = val_data.map(lambda lp: lp.features[3]).sum()
    sf4 = test_data.map(lambda lp: lp.features[4]).sum()
    sums = [sf2, sf3, sf4]
    n_total = train_data.count() + val_data.count() + test_data.count()

    Test.assertEqualsHashed(n_total,
                            '6f62570437644d4721b266a162d20cc35889917f',
                            'Unexpected Train, Val, Test dataset sizes',
                            'Train, Val, Test sizes are ok')

    Test.assertEqualsHashed(sums, '8b4a1f9f2301bd4396fe196fb3cb62dda4b70b0d',
                            'Train, Val, Test have unexpected values',
                            'Train, Val, Test are ok')

    Test.assertEqualsHashed(
        n_partitions, '9f28346a7d38b31687c9d21909b6318da391f569',
        'Wrong number of partitions: {}'.format(n_partitions),
        'Correct number of partitions: {}'.format(n_partitions))
# In[2]:

# 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[3]:

# 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[4]:

# 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]

Exemplo n.º 29
0
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',
Exemplo n.º 30
0

# ### ** 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')


# ### ** Part 3: Check plotting **
Exemplo n.º 31
0
from test_helper import Test

x = 1
y = 2

Test.assertEquals(x, 1)
Test.assertEquals(x + 1, 2)
Test.assertEquals(y, 1, "y is incorrect")

Test.assertEqualsHashed(x, '356a192b7913b04c54574d18c28d46e6395428ab',
                        'this is a test')

Test.printStats()

Test.setFailFast()
Test.setPrivateMode()
Test.assertEquals(y, 1, "y is incorrect")
# Transparent Tests
from test_helper import Test
def test_year(year, df):
    return [row.firstName for row in top_female_names_for_year(year, 5, df).collect()]

# COMMAND ----------

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")
  
run_tests()

# COMMAND ----------

# MAGIC %md

# COMMAND ----------

# MAGIC %md 
# MAGIC ## Solution
# MAGIC 
# MAGIC If you're stuck, and you're really not sure how to proceed, feel free to check out the solution. You'll find it in the same folder as the lab.

# COMMAND ----------
Exemplo n.º 33
0
# ### ** Part 2: Check class testing library **

# #### ** (2a) Compare with hash **

# In[4]:

# 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[5]:

# 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')

# ### ** Part 3: Check plotting **
Exemplo n.º 34
0
# 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]
Exemplo n.º 35
0
from test_helper import Test

x = 1
y = 2

Test.assertEquals(x, 1)
Test.assertEquals(x + 1, 2)
Test.assertEquals(y, 1, "y is incorrect")

Test.assertEqualsHashed(x,'356a192b7913b04c54574d18c28d46e6395428ab', 'this is a test')

Test.printStats()

Test.setFailFast()
Test.setPrivateMode()
Test.assertEquals(y, 1, "y is incorrect")