Beispiel #1
0
def regularization_and_probabilities():
    X = np.array([[1.78862847, 0.43650985], [0.09649747, -1.8634927],
                  [-0.2773882, -0.35475898], [-3.08274148, 2.37299932],
                  [-3.04381817, 2.52278197], [-1.31386475, 0.88462238],
                  [-2.11868196, 4.70957306], [-2.94996636, 2.59532259],
                  [-3.54535995, 1.45352268], [0.98236743, -1.10106763],
                  [-1.18504653, -0.2056499], [-1.51385164, 3.23671627],
                  [-4.02378514, 2.2870068], [0.62524497, -0.16051336],
                  [-3.76883635, 2.76996928], [0.74505627, 1.97611078],
                  [-1.24412333, -0.62641691], [-0.80376609, -2.41908317],
                  [-0.92379202, -1.02387576], [1.12397796, -0.13191423]])

    y = np.array([
        -1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1
    ])

    # Set the regularization strength
    model = LogisticRegression(C=1)
    # Fit and plot
    model.fit(X, y)
    plot_classifier(X, y, model, proba=True)
    # Predict probabilities on training points
    prob = model.predict_proba(X)
    print("Maximum predicted probability", np.max(prob))

    # Set the regularization strength
    model = LogisticRegression(C=0.1)
    # Fit and plot
    model.fit(X, y)
    plot_classifier(X, y, model, proba=True)
    # Predict probabilities on training points
    prob = model.predict_proba(X)
    print("Maximum predicted probability", np.max(prob))
def changing_the_model_coefficients(X, y):
    model = LogisticRegression()
    model.fit(X, y)
    model.coef_ = np.array([[-1, 1]])
    model.intercept_ = np.array([-3])

    util.plot_classifier(X, y, model)
    num_err = np.sum(y != model.predict(X))
    print("Number of errors:", num_err)
def plot_4_classifiers(X, y, clfs):

    # Set-up 2x2 grid for plotting.
    fig, sub = plt.subplots(2, 2)
    plt.subplots_adjust(wspace=0.2, hspace=0.2)

    for clf, ax, title in zip(
            clfs, sub.flatten(),
        ("(1) LogisticRegression", "(2) LinearSVC", "(3) SVC", "(4) KNN")):
        # clf.fit(X, y)
        plot_classifier(X, y, clf, ax, ticks=True)
        ax.set_title(title)
    plt.show()
Beispiel #4
0
def effect_of_removing_examples(X, y):
    # Train a linear SVM
    svm = SVC(kernel="linear")
    svm.fit(X, y)
    plot_classifier(X, y, svm, lims=(11, 15, 0, 6))

    # Make a new data set keeping only the support vectors
    print("Number of original examples", len(X))
    print("Number of support vectors", len(svm.support_))
    X_small = X[svm.support_]
    y_small = y[svm.support_]

    # Train a new SVM using only the support vectors
    svm_small = SVC(kernel="linear")
    svm_small.fit(X_small, y_small)
    plot_classifier(X_small, y_small, svm_small, lims=(11, 15, 0, 6))
def visualizing_multi_class_logistic_regression(X_train, y_train):
    lr_mn = LogisticRegression(C=100,
                               multi_class='multinomial',
                               solver='lbfgs')
    lr_ovr = LogisticRegression(C=100, multi_class='ovr', solver='liblinear')
    lr_mn.fit(X_train, y_train)
    lr_ovr.fit(X_train, y_train)

    # Print training accuracies
    print("Softmax     training accuracy:", lr_mn.score(X_train, y_train))
    print("One-vs-rest training accuracy:", lr_ovr.score(X_train, y_train))

    # Create the binary classifier (class 1 vs. rest)
    lr_class_1 = LogisticRegression(C=100)
    lr_class_1.fit(X_train, y_train == 1)

    # Plot the binary classifier (class 1 vs. rest)
    plot_classifier(X_train, y_train == 1, lr_class_1)
def one_vs_rest_svm(X_train, y_train):
    # Create/plot the binary classifier (class 1 vs. rest)
    svm_class_1 = SVC()
    svm_class_1.fit(X_train, y_train == 1)
    plot_classifier(X_train, y_train == 1, svm_class_1)