Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions NumPy/Array Basics/Broadcasting/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@


class TestCase(unittest.TestCase):
def test_shape(self):
self.assertEqual(x.shape, (10, 1), msg="Wrong shape of x.")
self.assertEqual(w.shape, (3, 4), msg="Wrong shape of w.")
def test_shape_x(self):
self.assertEqual((10, 1), x.shape, msg="Wrong shape of x.")

def test_shape_w(self):
self.assertEqual((3, 4), w.shape, msg="Wrong shape of w.")


Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@


class TestCase(unittest.TestCase):
def test_add(self):
self.assertEqual(create_array(1, 2).shape, (1, 2), msg="Something is wrong.")
self.assertEqual(create_array(3, 5).shape, (3, 5), msg="Something is wrong.")
def test_one(self):
self.assertEqual((1, 2), create_array(1, 2).shape, msg="Something is wrong.")

def test_two(self):
self.assertEqual((3, 5), create_array(3, 5).shape, msg="Something is wrong.")
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def test_no_step(self):
except TypeError:
class TestCase2(unittest.TestCase):
def test_bad(self):
self.assertTrue(False, msg='Your function definition possibly has an invalid number of arguments.')
self.fail('Your function definition possibly has an invalid number of arguments.')
19 changes: 14 additions & 5 deletions NumPy/Array Basics/Create an Empty Array/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@


class TestCase(unittest.TestCase):
def test_create_arrays(self):
def test_dtype_ones(self):
a, b = create_arrays(2, 3)
self.assertEqual(a.dtype, 'int64', msg="Wrong dtype in the .ones array.")
self.assertEqual(a.shape, (3, 2), msg="Wrong shape of the .ones array.")
self.assertEqual(b.dtype, 'bool', msg="Wrong dtype in the .full array.")
self.assertEqual(b.shape, (3, 2), msg="Wrong shape of the .full array.")
self.assertEqual('int64', a.dtype, msg="Wrong dtype in the .ones array.")

def test_shape_ones(self):
a, b = create_arrays(2, 3)
self.assertEqual((3, 2), a.shape, msg="Wrong shape of the .ones array.")

def test_dtype_full(self):
a, b = create_arrays(2, 3)
self.assertEqual('bool', b.dtype, msg="Wrong dtype in the .full array.")

def test_shape_full(self):
a, b = create_arrays(2, 3)
self.assertEqual((3, 2), b.shape, msg="Wrong shape of the .full array.")

17 changes: 13 additions & 4 deletions NumPy/Array Basics/Linspace/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@


class TestCase(unittest.TestCase):
def test_linspace(self):
a, b = sine_array(0, 2 * pi, 100)
self.assertEqual(6.28318531, round(a[-1], 8), msg="Ooops!")

def test_sine(self):
a, b = sine_array(0, 2 * pi, 100)
self.assertEqual(round(a[-1], 8), 6.28318531, msg="Ooops!")
self.assertAlmostEqual(b[-1], 0, msg="Ooops!")
self.assertEqual(a.shape, (100,), msg="Wrong num in array a.")
self.assertEqual(b.shape, (100,), msg="Wrong num in array b.")
self.assertAlmostEqual(0, b[-1], msg="Ooops!")

def test_linspace_shape(self):
a, b = sine_array(0, 2 * pi, 100)
self.assertEqual((100,), a.shape, msg="Wrong shape of array a.")

def test_sine_shape(self):
a, b = sine_array(0, 2 * pi, 100)
self.assertEqual((100,), b.shape, msg="Wrong shape of array b.")
12 changes: 8 additions & 4 deletions NumPy/Array Basics/Random Sampling/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@


class TestCase(unittest.TestCase):
def test_normal(self):
self.assertEqual(s.shape, (1000,), msg="Draw 1000 samples.")
self.assertEqual(round(abs(mu - np.mean(s)), 1), 0.0, msg="Mean should be close to 0.0.")
self.assertEqual(round(abs(sigma - np.std(s, ddof=1)), 1), 0.0, msg="Variance should be close to 0.0.")
def test_draw(self):
self.assertEqual((1000,), s.shape, msg="Draw 1000 samples.")

def test_mean(self):
self.assertEqual(0.0, round(abs(mu - np.mean(s)), 1), msg="Mean should be close to 0.0.")

def test_variance(self):
self.assertEqual(0.0, round(abs(sigma - np.std(s, ddof=1)), 1), msg="Variance should be close to 0.0.")

6 changes: 3 additions & 3 deletions NumPy/Array Basics/Random Shuffle/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

class TestCase(unittest.TestCase):
def test_shape(self):
self.assertEqual(arr.shape, (5, 20), msg="Wrong shape of the array 'arr'.")
self.assertEqual(permuted_2d.shape, (5, 20), msg="Wrong shape of the array 'permuted_2d'.")
self.assertEqual(fully_random.shape, (5, 20), msg="Wrong shape of the array 'fully_random'.")
self.assertEqual((5, 20), arr.shape, msg="Wrong shape of the array 'arr'.")
self.assertEqual((5, 20), permuted_2d.shape, msg="Wrong shape of the array 'permuted_2d'.")
self.assertEqual((5, 20), fully_random.shape, msg="Wrong shape of the array 'fully_random'.")

def test_arr(self):
for i in arr:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
# todo: replace this with an actual test
class TestCase(unittest.TestCase):
def test_add(self):
self.assertEqual(arr.shape, (100, 4), msg="Wrong array shape.")
self.assertEqual((100, 4), arr.shape, msg="Wrong array shape.")
6 changes: 4 additions & 2 deletions NumPy/Array Basics/Reshape/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

from task import a, b


class TestCase(unittest.TestCase):
def test_arrays(self):
def test_a(self):
np.testing.assert_array_equal(a, np.arange(12, 30, 3), err_msg='Array a is not what we expected.')
np.testing.assert_array_equal(b, a.reshape(2, 3), err_msg='Array b is not what we expected.')

def test_b(self):
np.testing.assert_array_equal(b, a.reshape(2, 3), err_msg='Array b is not what we expected.')
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@


class TestCase(unittest.TestCase):
def test_add(self):
def test_a(self):
test_a = np.arange(20).reshape(2, 2, 5)
test_b = np.array([[True, False], [False, True]])
np.testing.assert_array_equal(a, test_a, err_msg='Array `a` seems off.')

def test_c(self):
test_a = np.arange(20).reshape(2, 2, 5)
test_b = np.array([[True, False], [False, True]])
np.testing.assert_array_equal(c, test_a[test_b], err_msg='Array `c` seems off.')

def test_mask(self):
test_b = np.array([[True, False], [False, True]])
np.testing.assert_array_equal(mask, test_b, err_msg='Something wrong with the mask array.')


Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

class TestCase(unittest.TestCase):
def test_arrays_shape(self):
self.assertEqual(b.shape, (5, 2), msg="Wrong shape of array b.")
self.assertEqual((5, 2), b.shape, msg="Wrong shape of array b.")

def test_array_content(self):
self.assertEqual(a, 19, msg="a has to be equal to 19.")
self.assertEqual(19, a, msg="a has to be equal to 19.")

def test_array_b(self):
np.testing.assert_array_equal(b, x[::2, ::2], err_msg='Something wrong in array b.')

Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,34 @@

from task import x, y, a, b, c, d, e


class TestCase(unittest.TestCase):
def test_shape(self):
self.assertEqual(a.shape, (4,), msg='Wrong shape of array a.')
self.assertEqual(b.shape, (3, 3), msg='Wrong shape of array b.')
self.assertEqual(c.shape, (3, 7), msg='Wrong shape of array c.')
self.assertEqual(d.shape, (3,), msg='Wrong shape of array d.')
self.assertEqual(e.shape, (3,), msg='Wrong shape of array e.')

def test_arrays(self):
def test_shape_a(self):
self.assertEqual((4,), a.shape, msg='Wrong shape of array a.')

def test_shape_b(self):
self.assertEqual((3, 3), b.shape, msg='Wrong shape of array b.')

def test_shape_c(self):
self.assertEqual((3, 7), c.shape, msg='Wrong shape of array c.')

def test_shape_d(self):
self.assertEqual((3,), d.shape, msg='Wrong shape of array d.')

def test_shape_e(self):
self.assertEqual((3,), e.shape, msg='Wrong shape of array e.')

def test_array_a(self):
np.testing.assert_array_equal(a, x[np.array([7, 13, 28, 33])], err_msg='Array a is not what we expected.')

def test_array_b(self):
np.testing.assert_array_equal(b, x[np.array([[0, 1, 2], [10, 11, 12], [28, 29, 30]])], err_msg='Array b is not what we expected.')

def test_array_c(self):
np.testing.assert_array_equal(c, y[np.array([0, 2, 4])], err_msg='Array c is not what we expected.')

def test_array_d(self):
np.testing.assert_array_equal(d, y[np.array([0, 2, 4]), np.array([0, 1, 2])], err_msg='Array d is not what we expected.')

def test_array_e(self):
np.testing.assert_array_equal(e, y[np.array([1, 2, 4]), 6], err_msg='Array e is not what we expected.')
11 changes: 7 additions & 4 deletions NumPy/Array Math/Basic Math Functions/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@


class TestCase(unittest.TestCase):
def test_entropy(self):
self.assertEqual(calculate_entropy(task_data), -np.sum(task_data * np.log2(task_data)), msg='Wrong answer for task '
'dataset.')
self.assertEqual(calculate_entropy(b), -np.sum(b * np.log2(b)), msg='Wrong answer for test dataset.')
def test_entropy_task_data(self):
self.assertEqual(-np.sum(task_data * np.log2(task_data)), calculate_entropy(task_data),
msg='Wrong answer for task dataset.')

def test_entropy_test(self):
self.assertEqual(-np.sum(b * np.log2(b)), calculate_entropy(b),
msg='Wrong answer for test dataset.')

8 changes: 5 additions & 3 deletions NumPy/Array Math/Linear Algebra/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@


class TestCase(unittest.TestCase):
def test_input(self):
np.testing.assert_array_equal(test_values, values, err_msg='Input values were not extracted properly from the '
def test_values(self):
np.testing.assert_array_equal(values, test_values, err_msg='Input values were not extracted properly from the '
'csv.')
np.testing.assert_array_equal(test_labels, labels, err_msg='Labels were not extracted properly from the '

def test_labels(self):
np.testing.assert_array_equal(labels, test_labels, err_msg='Labels were not extracted properly from the '
'csv.')

def test_predict(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ def test_upper(self):
uppercase_text = read_data('text.txt')
np.testing.assert_array_equal(uppercase_text, test_uppercase_text, err_msg='The first function does not work '
'as expected.')
self.assertEqual(uppercase_text.shape, test_uppercase_text.shape, msg="Expected array shape is (8,).")

def test_upper_shape(self):
uppercase_text = read_data('text.txt')
self.assertEqual(test_uppercase_text.shape, uppercase_text.shape, msg="Expected array shape is (8,).")

def test_lengths(self):
uppercase_text = read_data('text.txt')
line_lengths = get_line_lengths(uppercase_text)
np.testing.assert_array_equal(line_lengths, test_line_lengths, err_msg='The second function does not work '
'as expected.')
self.assertEqual(line_lengths.shape, test_line_lengths.shape, msg="Expected array shape is (8,).")

def test_lengths_shape(self):
uppercase_text = read_data('text.txt')
line_lengths = get_line_lengths(uppercase_text)
self.assertEqual(test_line_lengths.shape, line_lengths.shape, msg="Expected array shape is (8,).")
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ def test_array(self):
err_msg="Your function does something else. Please check out the expected result in the"
"task description.")

def test_chars(self):
def test_punctuation(self):
txt = remove_extra_stuff(test_text)
check_digits = [char not in element for char in string.digits for element in txt]
check_whitespace = [char not in element for char in string.whitespace for element in txt]
check_punctuation = [char not in element for char in string.punctuation for element in txt]
self.assertTrue(all(check_punctuation), msg='Your result still contains punctuation.')

def test_digits(self):
txt = remove_extra_stuff(test_text)
check_digits = [char not in element for char in string.digits for element in txt]
self.assertTrue(all(check_digits), msg='Your result still contains digits.')

def test_whitespaces(self):
txt = remove_extra_stuff(test_text)
check_whitespace = [char not in element for char in string.whitespace for element in txt]
self.assertTrue(all(check_whitespace), msg='Your result still contains whitespaces.')
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

# todo: replace this with an actual test
class TestCase(unittest.TestCase):
def test_filter(self):
def test_filter_shape(self):
filter_ = arr % 4 == 0
np.testing.assert_array_equal(filtered_arr.shape, arr[filter_].shape, err_msg='filtered_array shape does not match the expected shape.')

def test_filter(self):
filter_ = arr % 4 == 0
np.testing.assert_array_equal(filtered_arr, arr[filter_], err_msg='filtered_array does not match the expected result.')
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class TestCase(unittest.TestCase):
def test_array_b(self):
test_b = np.arange(0, 25, 6)
self.assertEqual(b.shape, test_b.shape, msg='Shape of array b has to be (5,).')
self.assertEqual(test_b.shape, b.shape, msg='Shape of array b has to be (5,).')

def test_array_c(self):
test_b = np.arange(0, 25, 6)
Expand Down
20 changes: 15 additions & 5 deletions NumPy/Compare Search/Find Unique Values/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,31 @@


class TestCase(unittest.TestCase):
def test_data(self):
def test_import(self):
np.testing.assert_array_equal(csv, test_csv, err_msg='Dataset is imported improperly.')

def test_measurements(self):
np.testing.assert_array_equal(data, test_data, err_msg='Array of measurements is off.')

def test_labels(self):
np.testing.assert_array_equal(labels, test_labels, err_msg='Labels array is off.')

def test_unique(self):
def test_set_of_classes(self):
np.testing.assert_array_equal(classes, test_classes,
err_msg='The set of classes is wrong.')

def test_unique_measurements(self):
np.testing.assert_array_equal(unique_measurements, test_unique_measurements,
err_msg='The set of unique measurements is wrong.')

def test_occurrences(self):
np.testing.assert_array_equal(unique_data_counts, test_unique_data_counts,
err_msg='The set containing the number of occurrences of the unique values is wrong.')

def test_most_frequent(self):
self.assertEqual(most_frequent_index, test_most_frequent_index,
def test_most_frequent_index(self):
self.assertEqual(test_most_frequent_index, most_frequent_index,
msg="The index of the most frequent value is incorrect.")
self.assertEqual(most_frequent_measurement, test_most_frequent_measurement,

def test_most_frequent_value(self):
self.assertEqual(test_most_frequent_measurement, most_frequent_measurement,
msg="The most frequent value is identified incorrectly.")
2 changes: 1 addition & 1 deletion NumPy/Compare Search/Find maximum/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ def test_array(self):
np.testing.assert_array_equal(result, test_result, err_msg='Your result does not match the expected.')

def test_array_shape(self):
self.assertEqual(result.shape, test_result.shape, msg='Shape of the array result should be (100, 1).')
self.assertEqual(test_result.shape, result.shape, msg='Shape of the array result should be (100, 1).')
4 changes: 3 additions & 1 deletion NumPy/Compare Search/Search/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def test_week(self):
np.testing.assert_array_equal(result, np.where(temperatures > 15, high_test, low_test),
err_msg='Your `result` array '
'does not contain the values we expected.')
self.assertEqual(result.shape, days.shape, msg='Shape of the array `result` should match the shape of `days`.')

def test_shape(self):
self.assertEqual(days.shape, result.shape, msg='Shape of the array `result` should match the shape of `days`.')

def test_names(self):
np.testing.assert_array_equal(warm_days, days[temperatures > 15],
Expand Down
12 changes: 8 additions & 4 deletions NumPy/Compare Search/bincount/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@


class TestCase(unittest.TestCase):
def test_most_frequent_class(self):
self.assertEqual(find_most_frequent_class('data.csv'), 1, msg="Incorrect class found for task data.")
self.assertEqual(find_most_frequent_class('test_data1.csv'), 4, msg="Incorrect class found for test data.")
self.assertEqual(find_most_frequent_class('test_data2.csv'), 37, msg="Incorrect class found for test data.")
def test_most_frequent_class1(self):
self.assertEqual(1, find_most_frequent_class('data.csv'), msg="Incorrect class found for task data.")

def test_most_frequent_class2(self):
self.assertEqual(4, find_most_frequent_class('test_data1.csv'), msg="Incorrect class found for test data.")

def test_most_frequent_class3(self):
self.assertEqual(37, find_most_frequent_class('test_data2.csv'), msg="Incorrect class found for test data.")
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@


class TestCase(unittest.TestCase):
def test_arr(self):
def test_b(self):
np.testing.assert_array_equal(b, np.arange(4).reshape(1, 4),
err_msg='Array b should be of shape (1, 4) and have values from 0 to 3.')

def test_c(self):
np.testing.assert_array_equal(c, np.concatenate((a, np.arange(4).reshape(1, 4)), axis=0),
err_msg='Array c is not what we expected.')

def test_stacked(self):
np.testing.assert_array_equal(stacked, np.vstack((np.arange(10), np.arange(20, 30), np.arange(40, 50))),
err_msg='Array stacked is not what we expected.')
Loading