<jupyter_start><jupyter_text>Lab 2A___**Task:** Train a Perceptron with Rosenblatt's algorithm          for given dataset.<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from perceptron import Perceptron
from sklearn.linear_model import Perceptron as skl_perc
from sklearn.model_selection import train_test_split
from misc.estimators import accuracy, contingency_table, precision, recall

def visualize_perceptron(coeffs: np.ndarray,
                         X: np.ndarray,
                         Y: np.ndarray) -> None:
      X1 = X[Y == 0]
      X2 = X[Y == 1]

      a, b, c = coeffs
      min_x = min(np.min(X1[0]), np.min(X2[0]))
      max_x = max(np.max(X1[0]), np.max(X2[0]))
      min_y = -(c + a * min_x) / b
      max_y = -(c + a * max_x) / b

      plt.scatter(X1[:, 0], X1[:, 1], marker="o")
      plt.scatter(X2[:, 0], X2[:, 1], marker="x")
      plt.plot([min_x, max_x], [min_y, max_y])

      # plt.show()<jupyter_output><empty_output><jupyter_text>Dataset reading and describing____Dataset is given in lab task_<jupyter_code>data = pd.read_csv("../datasets/data10.csv", sep=";", header=None).to_numpy()<jupyter_output><empty_output><jupyter_text>Dividing dataset on train and test data___Let's shuffle given data and divide it on train and test data in a ratio 4:1:<jupyter_code>X, Y = data[:, :-1], data[:, -1]

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=.2)<jupyter_output><empty_output><jupyter_text>Dataset visualization<jupyter_code>X1 = X[Y == 0]
X2 = X[Y == 1]
plt.scatter(X1[:, 0], X1[:, 1], marker="o")
plt.scatter(X2[:, 0], X2[:, 1], marker="x")<jupyter_output><empty_output><jupyter_text>Perceptron initialization and train<jupyter_code>p = Perceptron()
p.train(X_train, Y_train, .9)
p.w, p.b<jupyter_output><empty_output><jupyter_text>Perceptron visualization<jupyter_code>visualize_perceptron(np.append(p.w, p.b), X, Y)<jupyter_output><empty_output><jupyter_text>Estimations<jupyter_code>predictions = p.predict(X_test)<jupyter_output><empty_output><jupyter_text>Contingency matrix:<jupyter_code>contingency_table(Y_test, predictions)<jupyter_output><empty_output><jupyter_text>Accuracy, precision, recall:<jupyter_code>print(f"Accuracy: {accuracy(Y_test, predictions).round(2)}\n"
      f"Precision: {precision(Y_test, predictions).round(2)}\n"
      f"Recall: {recall(Y_test, predictions).round(2)}")<jupyter_output>Accuracy: 0.9
Precision: 0.75
Recall: 1.0<jupyter_text>Using perceptron from sklearn<jupyter_code>skl_perc = skl_perc().fit(X_train, Y_train)<jupyter_output><empty_output><jupyter_text>Estimations<jupyter_code>predictions = skl_perc.predict(X_test)<jupyter_output><empty_output><jupyter_text>Contingency matrix:<jupyter_code>contingency_table(Y_test, predictions)<jupyter_output><empty_output><jupyter_text>Accuracy, precision, recall:<jupyter_code>print(f"Accuracy: {accuracy(Y_test, predictions).round(2)}\n"
      f"Precision: {precision(Y_test, predictions).round(2)}\n"
      f"Recall: {recall(Y_test, predictions).round(2)}")<jupyter_output>Accuracy: 0.95
Precision: 0.86
Recall: 1.0
{'fraction_non_alphanumeric': 0.1474193548387097, 'fraction_numerical': 0.02, 'mean_word_length': 5.937360178970917, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '60811', 'license': 'MIT', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1210, 'n_words': 206, 'path': '2A/main.ipynb', 'repo_name': 'Pavel3P/Machine-Learning', 'size': '3100'}