Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- import seaborn as sns
- import pandas as pd
- from sklearn.datasets import make_blobs
- from sklearn.decomposition import PCA
- from sklearn.model_selection import train_test_split
- from sklearn.metrics import silhouette_score, accuracy_score
- from random import random
- def activation_function(x):
- #f(x)=1/(1+e^(-x))
- # sigmoid, tanh, relu, leakyrelu
- return 1/(1+np.exp(-x))
- class Perceptron:
- def __init__(self, learning_rate=0.003, n_iters=15041, return_stop=False):
- self.lr = learning_rate
- self.n_iters = n_iters
- self.activation_function = activation_function
- self.weights = None
- self.bias = None
- self.misclassified_counts = []
- self.return_stop = return_stop
- def fit(self, X, Y):
- n_samples, n_features = X.shape
- self.weights = np.array([random() for _ in range(n_features)])
- self.bias = random()
- y_ = np.where(Y > 0, 1, 0)
- for iter_ in range(self.n_iters):
- n_misclassified = 0
- for index, x_i in enumerate(X):
- linear_output = np.dot(x_i, self.weights) + self.bias
- y_predicted = self.activation_function(linear_output)
- perceptron_update = self.lr * (y_[index] - y_predicted)
- self.weights += perceptron_update * x_i
- self.bias += perceptron_update
- if (y_predicted > 0.5) != y_[index]:
- n_misclassified += 1
- self.misclassified_counts.append(n_misclassified)
- if n_misclassified == 0:
- print(f"Навчання зупинено на епохі {iter_}")
- if self.return_stop != False:
- return iter_
- break
- def predict(self, x):
- linear_output = np.dot(x, self.weights) + self.bias
- y_predicted = self.activation_function(linear_output)
- return np.where(y_predicted > 0.5, 1, 0)
- N = 1000 # Кількість наборів
- M = 40 # Довжина кожного набору
- data = np.random.randint(0, 2, size=(N, M))
- target = np.where(np.mean(data, axis=1) < 0.5, 0, 1)
- bias = np.ones((N, 1))
- data_with_bias = np.hstack((data, bias))
- df = pd.DataFrame(data_with_bias, columns=[f'x{i}' for i in range(1, M+1)] + ['bias'])
- df['target'] = target
- pca = PCA(n_components=2)
- X_pca = pca.fit_transform(data_with_bias)
- df_pca = pd.DataFrame(X_pca, columns=[f'PCA{i+1}' for i in range(2)])
- df_pca['target'] = target
- sns.pairplot(df_pca, hue='target', palette='dark')
- plt.show()
- print(df.target.value_counts())
- df
- x_train, x_test, y_train, y_test = train_test_split(df.iloc[:, :-1], df['target'], test_size=0.2, random_state=42)
- model = Perceptron(learning_rate=0.1, n_iters=15000)
- model.fit(X=x_train.to_numpy(), Y=y_train.to_numpy())
- y_pred = model.predict(x_test)
- y_pred_train = model.predict(x_train)
- preds = model.predict(df.iloc[:, :-1])
- df['preds'] = preds
- df
- train_accuracy = accuracy_score(y_true=y_train, y_pred=y_pred_train)
- accuracy = accuracy_score(y_true=y_test, y_pred=y_pred)
- print(f'Точність на навчальній вибірці: {accuracy*100}%\nТочність на тренувальній вибірці: {train_accuracy*100}%')
- plt.figure(figsize=(10, 5))
- plt.title('Залежнiсть кiлькостi помилок вiд номера епохи навчання')
- plt.plot(np.arange(1, len(model.misclassified_counts)+1), model.misclassified_counts, '-')
- plt.xlabel('Номер епохи')
- plt.ylabel('Кількість помилок')
- plt.grid()
- plt.show()
- learning_rates = np.linspace(0.001, 1, 100)
- epochs_needed = []
- for lr in learning_rates:
- print(lr)
- model = Perceptron(learning_rate=lr, n_iters=15000, return_stop=1)
- epochs = model.fit(X=x_train.to_numpy(), Y=y_train.to_numpy())
- epochs_needed.append(epochs)
- plt.figure(figsize=(10, 6))
- plt.plot(learning_rates, epochs_needed)
- plt.xlabel("Крок навчання (learning rate)")
- plt.ylabel("Кількість епох для повної класифікації")
- plt.title("Залежність кількості епох від кроку навчання")
- plt.grid(True)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement