print("// Обучение начато //") sc = StandardScaler() sc.fit(X_train) X_train_std = sc.transform(X_train) X_test_std = sc.transform(X_test) print("// Обучение завершено //") # Логистическая регрессия X_combined_std = np.vstack((X_train_std, X_test_std)) y_combined = np.hstack((y_train, y_test)) lr = LogisticRegression(C=100.0, random_state=1) lr.fit(X_train_std, y_train) plot_decision_regions(X_combined_std, y_combined, classifier=lr, test_idx=range(105, 150)) plt.xlabel('Длина лепестка [std]') plt.ylabel('Ширина лепестка [std]') plt.legend(loc='upper left') plt.show() # Вероятности lr.predict_proba(X_test_std[:3, :]) # Настройка переобучения # weights, params = [], [] # for c in np.arange(-5, 5): # lr = LogisticRegression(C=10.**c, random_state=1)
from sklearn import datasets from sklearn.model_selection import train_test_split from LogisticRegressionGD.MyLogisticRegressionGD import LogisticRegressionGD from Algorithms.plotDecisionRegions import plot_decision_regions import matplotlib.pyplot as plt iris = datasets.load_iris() X = iris.data[:, [2, 3]] y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1, stratify=y) X_train_01_subset = X_train[(y_train == 0) | y_train == 1] y_train_01_subset = y_train[(y_train == 0) | y_train == 1] lrgd = LogisticRegressionGD(eta=0.05, n_iter=1000, random_state=1) lrgd.fit(X_train_01_subset, y_train_01_subset) plot_decision_regions(X=X_train_01_subset, y=y_train_01_subset, classifier=lrgd) plt.xlabel('Длина лепестка [std]') plt.ylabel('Ширина лепестка [std]') plt.legend(loc='upper left') plt.show()
from sklearn.svm import SVC from Algorithms.plotDecisionRegions import plot_decision_regions import matplotlib.pyplot as plt import numpy as np np.random.seed(1) X_xor = np.random.randn(200, 2) y_xor = np.logical_xor(X_xor[:, 0] > 0, X_xor[:, 1] > 0) y_xor = np.where(y_xor, 1, -1) # plt.scatter(X_xor[y_xor == 1, 0], # X_xor[y_xor == 1, 1], # c='b', marker='x', # label='1') # plt.scatter(X_xor[y_xor == -1, 0], # X_xor[y_xor == -1, 1], # c='r', marker='s', # label='-1') # plt.xlim([-3, 3]) # plt.ylim([-3, 3]) # plt.legend(loc='best') # plt.show() svm = SVC(kernel='rbf', random_state=1, gamma=0.10, C=10.0) svm.fit(X_xor, y_xor) plot_decision_regions(X_xor, y_xor, classifier=svm, test_idx=range(105, 150)) plt.xlabel('Длина лепестка [std]') plt.ylabel('Ширина лепестка [std]') plt.legend(loc='upper left') plt.show()