import sys import matplotlib.pyplot as plt from sklearn.datasets import load_breast_cancer from sklearn.model_selection import train_test_split sys.path.append('..') from mlscratch.models.logistic_regression import LogisticRegression from mlscratch.metrics import accuracy_score if __name__ == '__main__': X, y = load_breast_cancer(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y) model = LogisticRegression() model.fit(X_train, y_train) y_pred = model.predict(X_test) acc = accuracy_score(y_test, y_pred) print(f'Accuracy: {acc}') plt.plot(model.train_metric_list) plt.title('Training BCE') plt.xlabel('Iterations') plt.ylabel('Binary Cross Entropy') plt.show()
import sys sys.path.append('../') import numpy as np from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from mlscratch.models.k_neighbor import KNN from mlscratch.metrics import accuracy_score if __name__ == '__main__': X, y = load_iris(return_X_y=True) X = StandardScaler().fit_transform(X) X_train, X_valid, y_train, y_valid = train_test_split(X, y, shuffle=True, random_state=27) model = KNN() model.fit(X_train, y_train) y_pred_proba = model.predict(X_valid) y_pred = np.round(y_pred_proba) acc = accuracy_score(y_valid, y_pred) print(f'Accuracy: {acc}')
def test_return(self): acc = metrics.accuracy_score(self.y_true, self.y_pred) self.assertAlmostEqual(acc, 0.75) self.assertIsInstance(acc, np.float64)
import sys from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.preprocessing import OneHotEncoder sys.path.append('..') from mlscratch.models.multilayer_perceptron import MLP from mlscratch.metrics import accuracy_score if __name__ == '__main__': X, y = load_iris(return_X_y=True) encoder = OneHotEncoder() y = encoder.fit_transform(y.reshape(-1, 1)).toarray() X_train, X_valid, y_train, y_valid = train_test_split(X, y, shuffle=True, random_state=27) model = MLP(num_hidden=64) model.fit(X_train, y_train) y_pred = model.predict(X_valid) acc = accuracy_score(y_valid.argmax(axis=1), y_pred.argmax(axis=1)) print(f'Accuracy: {acc}')