From 84fefb26cef72bbad34e3997d457f7fd8aec3407 Mon Sep 17 00:00:00 2001 From: gvidon-7 Date: Fri, 13 Mar 2026 23:20:30 +0300 Subject: [PATCH 1/5] =?UTF-8?q?=D0=9B=D0=B0=D1=80=D0=B8=D0=BE=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=9112-512?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Задание к 4-му занятию --- solutions/sem02/lesson04/task1.py | 77 +++++++++++++++++++++++++++++-- solutions/sem02/lesson04/task2.py | 36 +++++++++++++-- 2 files changed, 106 insertions(+), 7 deletions(-) diff --git a/solutions/sem02/lesson04/task1.py b/solutions/sem02/lesson04/task1.py index 1b5526c1f..2e754fc80 100644 --- a/solutions/sem02/lesson04/task1.py +++ b/solutions/sem02/lesson04/task1.py @@ -2,16 +2,85 @@ def pad_image(image: np.ndarray, pad_size: int) -> np.ndarray: - # ваш код - return image + if pad_size < 1: + raise ValueError + + shape = image.shape + shape = list(shape) + + if len(shape) == 2: + for i in range(len(shape)): + shape[i] += 2 * pad_size + else: + shape[0] += 2 * pad_size + shape[1] += 2 * pad_size + + border_array = np.ones(shape, dtype=image.dtype) + + if len(shape) == 2: + border_array[:pad_size, :] = 0 + border_array[-pad_size:, :] = 0 + border_array[:, :pad_size] = 0 + border_array[:, -pad_size:] = 0 + else: + border_array[:pad_size, :, :] = 0 + border_array[-pad_size:, :, :] = 0 + border_array[:, :pad_size, :] = 0 + border_array[:, -pad_size:, :] = 0 + + idx = border_array != 0 + border_array[idx] = image.reshape(-1) + + return border_array def blur_image( image: np.ndarray, kernel_size: int, ) -> np.ndarray: - # ваш код - return image + if kernel_size < 1 or kernel_size % 2 == 0: + raise ValueError + + border = kernel_size // 2 + if border == 0: + return image.astype(np.uint8) + + if image.ndim == 3: + first, second, third = image.shape + out = np.zeros((first, second, third), dtype=np.uint8) + for i in range(third): + out[:, :, i] = blur_image(image[:, :, i], kernel_size) + return out + + first, second = image.shape + image_new = pad_image(image, border) + new_first, new_second = image_new.shape + + flat = image_new.flatten() + + centers = [] + for i in range(border, border + first): + for j in range(border, border + second): + centers.append(i * new_second + j) + centers = np.array(centers) + + remotes = [] + for i in range(-border, border + 1): + for j in range(-border, border + 1): + remotes.append(i * new_second + j) + remotes = np.array(remotes) + + n_idx = centers[:, None] + remotes[None, :] + n_vals = flat[n_idx] + means = n_vals.mean(axis=1) + + blur_flat = flat.astype(float) + blur_flat[centers] = means + + blur_new = blur_flat.reshape(new_first, new_second) + final_image = blur_new[border : border + first, border : border + second] + + return np.round(final_image).astype(np.uint8) if __name__ == "__main__": diff --git a/solutions/sem02/lesson04/task2.py b/solutions/sem02/lesson04/task2.py index be9a2288f..254169d48 100644 --- a/solutions/sem02/lesson04/task2.py +++ b/solutions/sem02/lesson04/task2.py @@ -2,9 +2,39 @@ def get_dominant_color_info( - image: np.ndarray[np.uint8], + image: np.ndarray, threshold: int = 5, ) -> tuple[np.uint8, float]: - # ваш код + if threshold < 1: + raise ValueError('threshold must be positive') - return 0, 0 + first, second = image.shape + total = first * second + if total == 0: + return (np.uint8(0), 0.0) + + num = [0] * 256 + for i in range(first): + for j in range(second): + num[image[i, j]] += 1 + + palette = [i for i in range(256) if num[i] > 0] + if not palette: + return (np.uint8(0), 0.0) + if len(palette) == 1: + return (np.uint8(palette[0]), 100.0) + + biggest_sum = -1 + most_frequent_col = 0 + for c in palette: + low = max(0, c - threshold + 1) + high = min(255, c + threshold - 1) + s = 0 + for k in range(low, high + 1): + s += num[k] + if s > biggest_sum: + biggest_sum = s + most_frequent_col = c + + percentage = (biggest_sum / total) * 100.0 + return (np.uint8(most_frequent_col), percentage) From c323cc7f1902889e0e7169665e5b76eb34294ead Mon Sep 17 00:00:00 2001 From: gvidon-7 Date: Fri, 20 Mar 2026 01:24:28 +0300 Subject: [PATCH 2/5] =?UTF-8?q?=D0=9B=D0=B0=D1=80=D0=B8=D0=BE=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=9112-512?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Домашнее задание к 5-му занятию --- solutions/sem02/lesson05/task1.py | 15 ++++++++++++++- solutions/sem02/lesson05/task2.py | 16 +++++++++++++++- solutions/sem02/lesson05/task3.py | 18 +++++++++++++++++- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/solutions/sem02/lesson05/task1.py b/solutions/sem02/lesson05/task1.py index e9c7c3c56..be0d2a113 100644 --- a/solutions/sem02/lesson05/task1.py +++ b/solutions/sem02/lesson05/task1.py @@ -9,4 +9,17 @@ def can_satisfy_demand( costs: np.ndarray, resource_amounts: np.ndarray, demand_expected: np.ndarray, -) -> bool: ... +) -> bool: + + first, second = costs.shape + if demand_expected.size != second or resource_amounts.size != first: + raise ShapeMismatchError + + costs_required = costs * demand_expected + resource_required = np.sum(costs_required, axis = 1) + result = resource_amounts - resource_required + + if np.all(result >= 0): + return True + + return False \ No newline at end of file diff --git a/solutions/sem02/lesson05/task2.py b/solutions/sem02/lesson05/task2.py index be1fb9d2b..058931b8a 100644 --- a/solutions/sem02/lesson05/task2.py +++ b/solutions/sem02/lesson05/task2.py @@ -8,4 +8,18 @@ class ShapeMismatchError(Exception): def get_projections_components( matrix: np.ndarray, vector: np.ndarray, -) -> tuple[np.ndarray | None, np.ndarray | None]: ... +) -> tuple[np.ndarray | None, np.ndarray | None]: + first, second = matrix.shape + + if first != second or second != vector.size: + raise ShapeMismatchError + + if np.linalg.det(matrix) == 0: + return (None, None) + + vectors_from_matrix_sq_lens = np.sum(matrix**2, axis = 1) + col_temp = np.sum(matrix*vector, axis = 1) + col_temp = col_temp / vectors_from_matrix_sq_lens + elem_1 = col_temp[:, np.newaxis] * matrix + + return (elem_1, vector - elem_1) diff --git a/solutions/sem02/lesson05/task3.py b/solutions/sem02/lesson05/task3.py index 0c66906cb..57267262b 100644 --- a/solutions/sem02/lesson05/task3.py +++ b/solutions/sem02/lesson05/task3.py @@ -9,4 +9,20 @@ def adaptive_filter( Vs: np.ndarray, Vj: np.ndarray, diag_A: np.ndarray, -) -> np.ndarray: ... +) -> np.ndarray: + + if Vj.shape[1] != diag_A.size or Vs.shape[0] != Vj.shape[0]: + raise ShapeMismatchError + A = np.zeros((diag_A.size, diag_A.size), dtype=diag_A.dtype) + A[np.arange(diag_A.size), np.arange(diag_A.size)] = diag_A + Vj_H = np.transpose(Vj.conj()) + result_interm = Vj_H@Vj@A + M0 = result_interm.shape[0] + x1 = np.linalg.inv((np.eye(M0)+result_interm)) + x2 = Vj_H@Vs + x3 = Vj@x1 + result = Vs - x3@x2 + # Так как 2 последних теста постоянно падали, пришлось выражение ниже + # на отдельные кусочки поменьше разбить + # result = Vs - Vj@(np.linalg.inv((np.eye(M0)+result_interm)))@Vj_H@Vs + return result \ No newline at end of file From ea70f1c9881a3ebbb093814c4dedc1ded671d287 Mon Sep 17 00:00:00 2001 From: gvidon-7 Date: Fri, 10 Apr 2026 23:04:08 +0300 Subject: [PATCH 3/5] =?UTF-8?q?=D0=9B=D0=B0=D1=80=D0=B8=D0=BE=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=9112-512?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Только 1-я задача к 7-му занятию --- solutions/sem02/lesson07/task1.py | 88 ++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/solutions/sem02/lesson07/task1.py b/solutions/sem02/lesson07/task1.py index 3a505d89b..b79ee2ef2 100644 --- a/solutions/sem02/lesson07/task1.py +++ b/solutions/sem02/lesson07/task1.py @@ -13,7 +13,93 @@ def visualize_diagrams( ordinates: np.ndarray, diagram_type: Any, ) -> None: - # ваш код + if abscissa.shape != ordinates.shape: + raise ShapeMismatchError + + valid_types = ("hist", "violin", "box") + if diagram_type not in valid_types: + raise ValueError + + picture = plt.figure(figsize=(12, 12)) + grid = plt.GridSpec(4, 4, wspace=space, hspace=space) + + main = picture.add_subplot(grid[:-1, 1:]) + left = picture.add_subplot(grid[:-1, 0], sharey=main) + low = picture.add_subplot(grid[-1, 1:], sharex=main) + + main.scatter(abscissa, ordinates, color="m", alpha=0.7) + + if diagram_type == "hist": + + low.hist( + abscissa, + bins=75, + color="m", + density=True + ) + + left.hist( + ordinates, + bins=75, + color="m", + density=True, + orientation="horizontal", + ) + + low.invert_yaxis() + left.invert_xaxis() + + elif diagram_type == "violin": + v_left = left.violinplot( + ordinates, + vert=False, + showmedians=True + ) + + for body in v_left["bodies"]: + body.set_facecolor("m") + body.set_edgecolor("m") + + for part in v_left: + if part != "bodies": + v_left[part].set_edgecolor("m") + + v_low = low.violinplot( + abscissa, + vert=True, + showmedians=True + ) + + for body in v_low["bodies"]: + body.set_facecolor("m") + body.set_edgecolor("m") + + for part in v_low: + if part != "bodies": + v_low[part].set_edgecolor("m") + + low.invert_yaxis() + left.invert_xaxis() + + else: + + left.boxplot(ordinates, vert=False, patch_artist=True, + boxprops=dict(facecolor="m", alpha=0.7), + medianprops=dict(color="darkmagenta"), + whiskerprops=dict(color="m"), + capprops=dict(color="m"), + flierprops=dict(marker="o", markerfacecolor="m", markeredgecolor="m")) + + low.boxplot(abscissa, vert=True, patch_artist=True, + boxprops=dict(facecolor="m", alpha=0.7), + medianprops=dict(color="darkmagenta"), + whiskerprops=dict(color="m"), + capprops=dict(color="m"), + flierprops=dict(marker="o", markerfacecolor="m", markeredgecolor="m")) + + low.invert_yaxis() + left.invert_xaxis() + pass From 5b94d2465a0aba953cf3e3bea57260ab013e751e Mon Sep 17 00:00:00 2001 From: gvidon-7 Date: Fri, 17 Apr 2026 23:08:11 +0300 Subject: [PATCH 4/5] =?UTF-8?q?=D0=9B=D0=B0=D1=80=D0=B8=D0=BE=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2,=20=D0=9112-512?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ДЗ полностью к 8-му уроку --- solutions/sem02/lesson08/task1.py | 74 ++++++++++++++++----- solutions/sem02/lesson08/task2.py | 107 +++++++++++++++++++++++------- 2 files changed, 139 insertions(+), 42 deletions(-) diff --git a/solutions/sem02/lesson08/task1.py b/solutions/sem02/lesson08/task1.py index 89f88572f..fecc41186 100644 --- a/solutions/sem02/lesson08/task1.py +++ b/solutions/sem02/lesson08/task1.py @@ -8,27 +8,67 @@ def create_modulation_animation( - modulation, - fc, - num_frames, - plot_duration, - time_step=0.001, - animation_step=0.01, - save_path="" + modulation, # Функция модуляции + fc, # Частота несущего сигнала + num_frames, # Количество кадров, которые будут сохранены в анимацию + plot_duration, # длительность интервала времени до зацикливания + time_step=0.001, # Промежуток времени между соседними вычисленными точками сигнала + animation_step=0.01, # время, которое прибавляется к оси за кадр + save_path="", ) -> FuncAnimation: - # ваш код - return FuncAnimation() + figure, axis = plt.subplots(figsize=(16, 9)) + axis.set_xlabel("Время (с)", fontsize=20) + axis.set_ylabel("Амплитуда", fontsize=20) + axis.text( + 0.98, + 0.98, + "Модулированный сигнал", + transform=axis.transAxes, + ha="right", + va="top", + fontsize=14, + bbox=dict(facecolor="white", alpha=0.5, edgecolor="black"), + ) + x_values = np.arange(0, plot_duration, time_step) + if modulation is None: + modulation_multiplier = 1.0 + else: + modulation_multiplier = modulation(x_values) + y_values_start = modulation_multiplier * np.sin(2 * np.pi * fc * x_values) + (line_start,) = axis.plot(x_values, y_values_start, lw=1, c="purple") + + axis.set_xlim(x_values.min(), x_values.max()) + axis.set_ylim(-1.5, 1.5) + + def update_frame(frame_id: int): + t = x_values + animation_step * frame_id + if modulation is None: + modulation_multiplier = 1.0 + else: + modulation_multiplier = modulation(t) + y_values_new = modulation_multiplier * np.sin(2 * np.pi * fc * t) + line_start.set_data(t, y_values_new) + axis.set_xlim(t.min(), t.max()) + return line_start, axis + + anim = FuncAnimation(figure, update_frame, frames=num_frames, interval=50, blit=False) + + if save_path: + anim.save(save_path, writer="pillow") + + return anim if __name__ == "__main__": + def modulation_function(t): - return np.cos(t * 6) + return np.cos(t * 6) - num_frames = 100 - plot_duration = np.pi / 2 - time_step = 0.001 - animation_step = np.pi / 200 - fc = 50 + num_frames = 100 + plot_duration = np.pi / 2 + time_step = 0.001 + animation_step = np.pi / 200 + fc = 50 save_path_with_modulation = "modulated_signal.gif" animation = create_modulation_animation( @@ -38,6 +78,6 @@ def modulation_function(t): plot_duration=plot_duration, time_step=time_step, animation_step=animation_step, - save_path=save_path_with_modulation + save_path=save_path_with_modulation, ) - HTML(animation.to_jshtml()) \ No newline at end of file + HTML(animation.to_jshtml()) diff --git a/solutions/sem02/lesson08/task2.py b/solutions/sem02/lesson08/task2.py index b677c0702..352ea982d 100644 --- a/solutions/sem02/lesson08/task2.py +++ b/solutions/sem02/lesson08/task2.py @@ -5,30 +5,91 @@ from IPython.display import HTML from matplotlib.animation import FuncAnimation +from matplotlib.colors import ListedColormap # !!! +# Пусть путь всегда существует +def animate_wave_algorithm( + maze: np.ndarray, start: tuple[int, int], end: tuple[int, int], save_path: str = "" +) -> FuncAnimation: + starting_maze = maze.copy() + def make_visual(p_ways): + visual = np.where(starting_maze == 0, 0, 1) + visual[p_ways >= 0] = 2 + return visual + + p_ways = np.full_like(maze, -1) + coordinates = [start] + steps = [(0, 1), (1, 0), (-1, 0), (0, -1)] + head = 0 + p_ways[start] = 0 + + frames = [make_visual(p_ways)] + current_dist = 0 + + while head < len(coordinates): + dist = p_ways[coordinates[head]] + if dist > current_dist: + frames.append(make_visual(p_ways)) + current_dist = dist + + maze[coordinates[head]] = 7 + for i in range(len(steps)): + shift_up_down = coordinates[head][0] + steps[i][0] + shift_left_right = coordinates[head][1] + steps[i][1] + if ((0 <= shift_left_right < maze.shape[1]) == False) or ( + (0 <= shift_up_down < maze.shape[0]) == False + ): + continue + if maze[shift_up_down, shift_left_right] == 1: + coordinates.append((shift_up_down, shift_left_right)) + p_ways[shift_up_down, shift_left_right] = p_ways[coordinates[head]] + 1 + head += 1 + + figure, axes = plt.subplots(figsize=(12, 12)) + cmap = ListedColormap(["green", "brown", "blue"]) + image = axes.imshow( + frames[0], + cmap=cmap, + ) + axes.set_xticks(np.arange(starting_maze.shape[1])) + axes.set_yticks(np.arange(starting_maze.shape[0])) + axes.set_xticklabels(np.arange(starting_maze.shape[1])) + axes.set_yticklabels(np.arange(starting_maze.shape[0])) + + axes.set_xticks(np.arange(-0.5, starting_maze.shape[1], 1), minor=True) + axes.set_yticks(np.arange(-0.5, starting_maze.shape[0], 1), minor=True) + + axes.grid(which="minor", color="black", linestyle="-", linewidth=2) + axes.grid(which="major", visible=False) + + axes.set_title("Волновой алгоритм", fontsize=16) + + def update(frame_id): + image.set_data(frames[frame_id]) + return [image] + + anim = FuncAnimation(figure, update, frames=len(frames), interval=100, blit=True, repeat=False) + + if save_path: + anim.save(save_path, writer="pillow") + + return anim -def animate_wave_algorithm( - maze: np.ndarray, - start: tuple[int, int], - end: tuple[int, int], - save_path: str = "" -) -> FuncAnimation: - # ваш код - return FuncAnimation() if __name__ == "__main__": - # Пример 1 - maze = np.array([ - [0, 0, 0, 0, 0, 0, 0], - [0, 1, 1, 1, 1, 1, 0], - [1, 1, 0, 1, 0, 1, 0], - [0, 0, 1, 1, 0, 1, 0], - [0, 0, 0, 0, 0, 1, 0], - [1, 1, 1, 1, 1, 1, 0], - [0, 0, 0, 0, 0, 0, 0], - ]) + maze = np.array( + [ + [0, 0, 0, 0, 0, 0, 0], + [0, 1, 1, 1, 1, 1, 0], + [1, 1, 0, 1, 0, 1, 0], + [0, 0, 1, 1, 0, 1, 0], + [0, 0, 0, 0, 0, 1, 0], + [1, 1, 1, 1, 1, 1, 0], + [0, 0, 0, 0, 0, 0, 0], + ] + ) start = (2, 0) end = (5, 0) @@ -36,10 +97,8 @@ def animate_wave_algorithm( animation = animate_wave_algorithm(maze, start, end, save_path) HTML(animation.to_jshtml()) - - # Пример 2 - - maze_path = "./data/maze.npy" + + """ maze_path = "./data/maze.npy" loaded_maze = np.load(maze_path) # можете поменять, если захотите запустить из других точек @@ -48,6 +107,4 @@ def animate_wave_algorithm( loaded_save_path = "loaded_labyrinth.gif" loaded_animation = animate_wave_algorithm(loaded_maze, start, end, loaded_save_path) - HTML(loaded_animation.to_jshtml()) - - \ No newline at end of file + HTML(loaded_animation.to_jshtml()) """ From 6176a4f2d133f19353cf96a47258b736ec12f1b1 Mon Sep 17 00:00:00 2001 From: gvidon-7 Date: Tue, 28 Apr 2026 00:06:23 +0300 Subject: [PATCH 5/5] =?UTF-8?q?=D0=9B=D0=B0=D1=80=D0=B8=D0=BE=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2,=20=D0=9112-512?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Дз про Титаник --- solutions/sem02/lesson10/task1.ipynb | 482 +++++++++++++++++++++++++-- 1 file changed, 449 insertions(+), 33 deletions(-) diff --git a/solutions/sem02/lesson10/task1.ipynb b/solutions/sem02/lesson10/task1.ipynb index 4b4e9e335..3068b9d71 100644 --- a/solutions/sem02/lesson10/task1.ipynb +++ b/solutions/sem02/lesson10/task1.ipynb @@ -34,16 +34,271 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "d7b00711", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
survivedpclasssexagesibspparchfareembarkedclasswhoadult_maledeckembark_townalivealone
8513female33.03015.8500SThirdwomanFalseNaNSouthamptonyesFalse
62213male20.01115.7417CThirdmanTrueNaNCherbourgyesFalse
74303male24.01016.1000SThirdmanTrueNaNSouthamptonnoFalse
70913maleNaN1115.2458CThirdmanTrueNaNCherbourgyesFalse
25811female35.000512.3292CFirstwomanFalseNaNCherbourgyesTrue
16112female40.00015.7500SSecondwomanFalseNaNSouthamptonyesTrue
7413male32.00056.4958SThirdmanTrueNaNSouthamptonyesTrue
88403male25.0007.0500SThirdmanTrueNaNSouthamptonnoTrue
41503femaleNaN008.0500SThirdwomanFalseNaNSouthamptonnoTrue
2903maleNaN007.8958SThirdmanTrueNaNSouthamptonnoTrue
\n", + "
" + ], + "text/plain": [ + " survived pclass sex age sibsp parch fare embarked class \\\n", + "85 1 3 female 33.0 3 0 15.8500 S Third \n", + "622 1 3 male 20.0 1 1 15.7417 C Third \n", + "743 0 3 male 24.0 1 0 16.1000 S Third \n", + "709 1 3 male NaN 1 1 15.2458 C Third \n", + "258 1 1 female 35.0 0 0 512.3292 C First \n", + "161 1 2 female 40.0 0 0 15.7500 S Second \n", + "74 1 3 male 32.0 0 0 56.4958 S Third \n", + "884 0 3 male 25.0 0 0 7.0500 S Third \n", + "415 0 3 female NaN 0 0 8.0500 S Third \n", + "29 0 3 male NaN 0 0 7.8958 S Third \n", + "\n", + " who adult_male deck embark_town alive alone \n", + "85 woman False NaN Southampton yes False \n", + "622 man True NaN Cherbourg yes False \n", + "743 man True NaN Southampton no False \n", + "709 man True NaN Cherbourg yes False \n", + "258 woman False NaN Cherbourg yes True \n", + "161 woman False NaN Southampton yes True \n", + "74 man True NaN Southampton yes True \n", + "884 man True NaN Southampton no True \n", + "415 woman False NaN Southampton no True \n", + "29 man True NaN Southampton no True " + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import seaborn as sns\n", - "\n", + "import numpy as np\n", + "import pandas as pd\n", "\n", "titanic_data = sns.load_dataset(\"titanic\")\n", - "titanic_data.sample(5)" + "titanic_data.sample(10)" ] }, { @@ -66,12 +321,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "cd2f1773", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "survived 0\n", + "pclass 0\n", + "sex 0\n", + "age 177\n", + "sibsp 0\n", + "parch 0\n", + "fare 0\n", + "embarked 2\n", + "class 0\n", + "who 0\n", + "adult_male 0\n", + "deck 688\n", + "embark_town 2\n", + "alive 0\n", + "alone 0\n", + "dtype: int64" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# ваш код" + "empty = titanic_data.isnull().values\n", + "# empty = empty.astype(int)\n", + "empty = empty.sum(axis=0)\n", + "number_of_empty = pd.Series(\n", + " index= titanic_data.columns,\n", + " data= empty\n", + ")\n", + "number_of_empty" ] }, { @@ -88,12 +376,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "7e458447", "metadata": {}, "outputs": [], "source": [ - "# ваш код" + "titanic_data = titanic_data.dropna(thresh= (titanic_data.values.shape[0] + 1) // 2, axis= \"columns\")\n", + "titanic_data = titanic_data.dropna(thresh= (titanic_data.values.shape[1]+1) // 2)" ] }, { @@ -111,12 +400,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "f7db5e99", "metadata": {}, "outputs": [], "source": [ - "# ваш код" + "rows_to_fill_men = ((titanic_data[\"age\"].isna()) & (titanic_data[\"who\"] == \"man\"))\n", + "rows_to_fill_women = ((titanic_data[\"age\"].isna()) & (titanic_data[\"who\"] == \"woman\"))\n", + "rows_to_fill_children = ((titanic_data[\"age\"].isna()) & (titanic_data[\"who\"] == \"child\"))\n", + "\n", + "men_middle_age = round(titanic_data[titanic_data[\"who\"] == \"man\"][\"age\"].dropna().median())\n", + "women_middle_age = round(titanic_data[titanic_data[\"who\"] == \"woman\"][\"age\"].dropna().median())\n", + "children_middle_age = round(titanic_data[titanic_data[\"who\"] == \"child\"][\"age\"].dropna().median())\n", + "titanic_data.loc[rows_to_fill_children, \"age\"] = children_middle_age\n", + "titanic_data.loc[rows_to_fill_men, \"age\"] = men_middle_age\n", + "titanic_data.loc[rows_to_fill_women, \"age\"] = women_middle_age" ] }, { @@ -131,12 +429,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "67a0c6bc", "metadata": {}, "outputs": [], "source": [ - "# ваш код" + "titanic_data = titanic_data.dropna(thresh=(titanic_data.values.shape[1] - 1))" ] }, { @@ -151,12 +449,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "6a2d0ae9", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Southampton\n" + ] + } + ], "source": [ - "# ваш код" + "cities = titanic_data[\"embarked\"].dropna().values\n", + "unique, counts = np.unique(cities, return_counts=True)\n", + "if unique[counts.argmax()] == 'S':\n", + " print(\"Southampton\")\n", + "elif unique[counts.argmax()] == 'Q':\n", + " print(\"Queenstown\")\n", + "else:\n", + " print(\"Cherbough\")\n" ] }, { @@ -171,12 +484,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "11acb4b4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "38.25" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# ваш код" + "survived_ones = titanic_data[titanic_data[\"survived\"] == 1]\n", + "round((len(survived_ones.index) / len(titanic_data.index)) * 100, 2)" ] }, { @@ -191,12 +516,47 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "59ec7295", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/var/folders/27/dvqvlln90m15hk5wbzsb7lj80000gn/T/ipykernel_10356/87840810.py:1: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n", + " Southampton_city = survived_ones[titanic_data[\"embarked\"] == \"S\"]\n", + "/var/folders/27/dvqvlln90m15hk5wbzsb7lj80000gn/T/ipykernel_10356/87840810.py:2: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n", + " Queenstown_city = survived_ones[titanic_data[\"embarked\"] == \"Q\"]\n", + "/var/folders/27/dvqvlln90m15hk5wbzsb7lj80000gn/T/ipykernel_10356/87840810.py:3: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n", + " Cherbough_city = survived_ones[titanic_data[\"embarked\"] == \"C\"]\n" + ] + }, + { + "data": { + "text/plain": [ + "Southampton 217\n", + "Queenstown 30\n", + "Cherbough 93\n", + "dtype: int64" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# ваш код" + "Southampton_city = survived_ones[titanic_data[\"embarked\"] == \"S\"]\n", + "Queenstown_city = survived_ones[titanic_data[\"embarked\"] == \"Q\"]\n", + "Cherbough_city = survived_ones[titanic_data[\"embarked\"] == \"C\"]\n", + "\n", + "cities_and_survivors = pd.Series(\n", + " index= [\"Southampton\", \"Queenstown\", \"Cherbough\"],\n", + " data= [len(Southampton_city), len(Queenstown_city), len(Cherbough_city)]\n", + ")\n", + "\n", + "cities_and_survivors" ] }, { @@ -211,12 +571,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "34daca28", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "First 62.62\n", + "Second 47.28\n", + "Third 24.24\n", + "dtype: float64" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# ваш код" + "first_class_survived = survived_ones[survived_ones[\"class\"] == \"First\"]\n", + "second_class_survived = survived_ones[survived_ones[\"class\"] == \"Second\"]\n", + "third_class_survived = survived_ones[survived_ones[\"class\"] == \"Third\"]\n", + "\n", + "percent_first = round(len(first_class_survived) / len(titanic_data[titanic_data[\"class\"] == \"First\"]) * 100, 2)\n", + "percent_second = round(len(second_class_survived) / len(titanic_data[titanic_data[\"class\"] == \"Second\"]) * 100, 2)\n", + "percent_third = round(len(third_class_survived) / len(titanic_data[titanic_data[\"class\"] == \"Third\"]) * 100, 2)\n", + "\n", + "\n", + "\n", + "statistics = pd.Series(\n", + " index=[\"First\", \"Second\", \"Third\"],\n", + " data= [percent_first, percent_second, percent_third]\n", + ")\n", + "statistics" ] }, { @@ -231,12 +619,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "1fc0d667", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "73.58" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# ваш код" + "rich_ones = titanic_data[titanic_data[\"fare\"] >= 100]\n", + "rich_ones_number = len(rich_ones.index)\n", + "rich_ones_survived = survived_ones[survived_ones[\"fare\"] >= 100]\n", + "percentage_of_rich_survivors = round(len(rich_ones_survived.index) / rich_ones_number * 100, 2)\n", + "percentage_of_rich_survivors" ] }, { @@ -251,12 +654,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "480352b6", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# ваш код" + "children = titanic_data[titanic_data[\"who\"] == \"child\"]\n", + "children = children[children[\"alone\"]]\n", + "len(children.index)" ] }, { @@ -270,7 +686,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "numpy_env", "language": "python", "name": "python3" }, @@ -284,7 +700,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.9" + "version": "3.13.7" } }, "nbformat": 4,