def support_vector_machine_rbf():
    X_xor, y_xor = create_random_data()
    svm = SVC(kernel='rbf', random_state=0, gamma=0.10, C=10.0)
    svm.fit(X_xor, y_xor)
    plot_decision_regions(X_xor, y_xor, classifier=svm)
    plt.legend(loc='upper left')
    plt.show()
def logistic_regression():
    X_train_std, X_test_std, y_train, y_test = get_data_std()
    X_combined_std, y_combined = create_combined_data(X_train_std, X_test_std, y_train, y_test)

    lr = LogisticRegression(C=1000.0, random_state=0)
    lr.fit(X_train_std, y_train)
    plot_decision_regions(X_combined_std, y_combined, classifier=lr, test_idx=range(105,150))
    plt.xlabel('petal length [standardized]')
    plt.ylabel('petal width [standardized]')
    plt.legend(loc='upper left')
    plt.show()
def check_svm_gamma(gamma):
    X_train_std, X_test_std, y_train, y_test = get_data_std()
    X_combined_std, y_combined = create_combined_data(X_train_std, X_test_std,
                                                      y_train, y_test)
    svm = SVC(kernel='rbf', random_state=0, gamma=gamma, C=1.0)
    svm.fit(X_train_std, y_train)
    plot_decision_regions(X_combined_std,
                          y_combined,
                          classifier=svm,
                          test_idx=range(105, 150))
    plt.xlabel('petal length [standardized]')
    plt.ylabel('petal width [standardized]')
    plt.legend(loc='upper left')
    plt.show()
def k_neighbor_classify():
    X_train_std, X_test_std, y_train, y_test = get_data_std()
    X_combined_std, y_combined = create_combined_data(X_train_std, X_test_std,
                                                      y_train, y_test)
    knn = KNeighborsClassifier(n_neighbors=5, p=2, metric='minkowski')
    knn.fit(X_train_std, y_train)
    plot_decision_regions(X_combined_std,
                          y_combined,
                          classifier=knn,
                          test_idx=range(105, 150))
    plt.xlabel('petal length [standardized]')
    plt.ylabel('petal width [standardized]')
    plt.legend(loc='upper left')
    plt.show()
def support_vector_machine():
    X_train_std, X_test_std, y_train, y_test = get_data_std()
    X_combined_std, y_combined = create_combined_data(X_train_std, X_test_std,
                                                      y_train, y_test)

    svm = SVC(kernel='linear', C=10.0, random_state=0)
    svm.fit(X_train_std, y_train)
    plot_decision_regions(X_combined_std,
                          y_combined,
                          classifier=svm,
                          test_idx=range(105, 150))
    plt.xlabel('petal length [standardized]')
    plt.ylabel('petal width [standardized]')
    plt.legend(loc='upper left')
    plt.show()
def skl():
    X_train_std, X_test_stc, y_train, y_test = get_data_std()

    ppn=Perceptron(n_iter=40, eta0=0.1, random_state=0, shuffle=True)
    ppn.fit(X_train_std, y_train)

    y_pred = ppn.predict(X_test_std)
    print('Accuracy: %.2f' % accuracy_score(y_test, y_pred))

    X_combined_std, y_combined = create_combined_data(X_train_std, X_test_std, y_train, y_test)
    plot_decision_regions(X=X_combined_std, y=y_combined, classifier=ppn,
                          test_idx=range(105, 150))
    plt.xlabel('petal length [standardized]')
    plt.ylabel('petal width [standardized]')
    plt.legend(loc='upper left')
    plt.show()
Beispiel #7
0
def create_random_forest_structure():
    X_train, X_test, y_train, y_test = get_data()
    X_combined, y_combined = create_combined_data(X_train, X_test, y_train,
                                                  y_test)
    forest = RandomForestClassifier(criterion='entropy',
                                    n_estimators=10,
                                    random_state=1,
                                    n_jobs=2)
    forest.fit(X_train, y_train)
    plot_decision_regions(X_combined,
                          y_combined,
                          classifier=forest,
                          test_idx=range(105, 150))
    plt.xlabel('petal length [cm]')
    plt.ylabel('petal width [cm]')
    plt.legend(loc='upper left')
    plt.show()
Beispiel #8
0
def iris_stochastic_descent(X, y):
    X_std = np.copy(X)
    for k in range(2):
        X_std[:, k] = (X[:, k] - X[:, k].mean()) / X[:, k].std()

    ada = AdalineSGD(n_iter=15, eta=0.01, random_state=1)
    ada.fit(X_std, y)
    plot_decision_regions(X_std, y, classifier=ada)
    plt.title('Adaline - Stochastic Gradient Descent')
    plt.xlabel('sepal length [standardized]')
    plt.ylabel('petal length [standardized]')
    plt.legend(loc='upper left')
    plt.show()

    plt.plot(range(1, len(ada.cost_) + 1), ada.cost_, marker='o')
    plt.xlabel('Epochs')
    plt.ylabel('Average Cost')
    plt.show()
Beispiel #9
0
def create_tree_structure(output_file):
    X_train, X_test, y_train, y_test = get_data()
    tree = DecisionTreeClassifier(criterion='entropy',
                                  max_depth=3,
                                  random_state=0)
    tree.fit(X_train, y_train)
    X_combined, y_combined = create_combined_data(X_train, X_test, y_train,
                                                  y_test)
    plot_decision_regions(X_combined,
                          y_combined,
                          classifier=tree,
                          test_idx=range(105, 150))
    export_graphviz(tree,
                    out_file=output_file,
                    feature_names=['petal length', 'petal width'])
    plt.xlabel('petal length [cm]')
    plt.ylabel('petal width [cm]')
    plt.legend(loc='upper left')
    plt.show()
Beispiel #10
0
def basic_logistic():

    lr = LogisticRegression(C=1000.0, random_state=0)
    lr.fit(X_train_std, y_train)

    plot_decision_regions(X_combined_std,
                          y_combined,
                          classifier=lr,
                          test_idx=range(105, 150))
    plt.xlabel('petal length [standardized]')
    plt.ylabel('petal width [standardized]')
    plt.legend(loc='upper left')
    plt.show()

    y_pred = lr.predict(X_test_std)
    print 'Misclassified samples:\t%d of %d\nMisclassification rate:\t%f%%' % (
        (y_test != y_pred).sum(), X_test_std.shape[0], 100 *
        ((y_test != y_pred).sum() / float(X_test_std.shape[0])))

    print 'Accuracy:%.2f' % accuracy_score(y_test, y_pred)
Beispiel #11
0
def plot_iris_decision_regions_with_classifier(classifier):
    plot_decision_regions(X_combined_std, y_combined, classifier=classifier, test_idx=range(105, 150))
    plt.xlabel('petal length [standardized]')
    plt.ylabel('petal width [standardized]')
    plt.legend(loc='upper left')
    plt.show()
Beispiel #12
0
    # Define data and classes and plot all
    X = np.array([[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3]])
    #    X = np.array([[1, 2], [1, 3], [2, 3], [2, 1], [3, 1], [3, 2]])
    y = np.array([-1, -1, -1, 1, 1, 1])

    perceptr.plot_data(X, y)

    # Train 1)Perceptron
    pct.fit(X, y)

    # Print weights
    print("Final weights values: ", end="")
    print(pct.w_)

    # Do prediction
    prediction = pct.predict(X)

    # plot weights updates for each epoch
    plt.plot(range(1, len(pct.errors_) + 1), pct.errors_, marker='o')
    plt.title("PERCEPTRON weights updates")
    plt.xlabel('Epochs')
    plt.ylabel('Number of updates')
    plt.show()

    # plot decision regions
    perceptr.plot_decision_regions(X, y, classifier=pct)
    plt.xlabel('x1 [cm]')
    plt.ylabel('x2 [cm]')
    plt.legend(loc='upper left')
    plt.show()
Beispiel #13
0
def xorSVM(y):
    svm = SVC(kernel='rbf', random_state=0, gamma=y, C=10.0)
    svm.fit(X_xor, y_xor)
    plot_decision_regions(X_xor, y_xor, classifier=svm)
    plt.legend(loc='upper left')
    plt.show()
plt.xlabel('z')
plt.ylabel('$\phi (z)$')

plt.show()
'''

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=0)

#feature scaling
sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)


X_combined_std = np.vstack((X_train_std, X_test_std))
y_combined = np.hstack((y_train, y_test))

lr = LogisticRegression(C=1000.0, random_state=0)
lr.fit(X_train, y_train)
plot_decision_regions(X_combined_std, y_combined, classifier=lr, test_idx=range(105, 150))
plt.xlabel('petal length [standardized]')
plt.xlabel('petal width [standardized]')
plt.legend(loc='upper left')
plt.show()

print(lr.predict_proba(X_test_std[0,:]))
Beispiel #15
0
X_std[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std()
X_std[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std()

# ada = AdalineGD(n_iter=10, eta=0.01).fit(X_std, y)
# plot_decision_regions(X_std, y, classifier=ada)
# plt.title("Adaline - Gradient Descent")
# plt.xlabel("sepal length [standardized]")
# plt.ylabel("petal length standardized[]")
# plt.legend(loc="upper left")
# plt.show()
#
# plt.plot(range(1, len(ada.cost_) + 1), ada.cost_, marker="o")
# plt.xlabel("Epochs")
# plt.ylabel("Sum-squared-error")
# plt.show()

# --- 2.6
ada = AdalineSGD(n_iter=15, eta=0.01, random_state=1)
ada.fit(X_std, y)
plot_decision_regions(X_std, y, classifier=ada)
plt.title("Adaline - Stochastic Gradient Descent")
plt.xlabel("sepal length [standardized]")
plt.ylabel("petal length standardized[]")
plt.legend(loc="upper left")
plt.show()

plt.plot(range(1, len(ada.cost_) + 1), ada.cost_, marker="o")
plt.xlabel("Epochs")
plt.ylabel("Average Cost")
plt.show()
Beispiel #16
0
__author__ = 'joe'
from collections import Counter, defaultdict
import sys, os

# SVMs attempt to maximize the margin
# the margin is the distance between the separating hyperplane and
# the training samples closest to this hyperplane
# (JH Note) - this seems to allow it to automatically avoid overfitting
# https://epub-imgs.scribdassets.com/35adi63ssg4rzd0i/images/image-YH3QRA9Z.jpg
import numpy as np
import matplotlib.pyplot as plt

from sklearn.metrics import accuracy_score
from iris_loadin import *
from sklearn.svm import SVC
from perceptron import plot_decision_regions

svm = SVC(kernel='linear', C=1.0, random_state=0)
svm.fit(X_train_std, y_train)
print accuracy_score(y_test, svm.predict(X_test_std))
plot_decision_regions(X_combined_std,
                      y_combined,
                      classifier=svm,
                      test_idx=range(105, 150))
plt.xlabel('petal length [standardized]')
plt.ylabel('petal width [standardized]')
plt.legend(loc='upper left')
plt.show()
Beispiel #17
0
df = pd.read_csv(
    'https://archive.ics.uci.edu/ml/'
    'machine-learning-databases/iris/iris.data',
    header=None)

y = df.iloc[50:150, 4].values
# setosa,virginica,versicolor
y = np.where(y == 'Iris-versicolor', -1, 1)
X = df.iloc[50:150, [0, 2]].values

# plt.scatter(X[:50, 0], X[:50, 1], color='red', marker='o', label='setosa')
# plt.scatter(X[50:100, 0], X[50:100, 1], color='blue', marker='x', label='versicolor')
#
# plt.xlabel('petal length [cm]')
# plt.ylabel('sepal length [cm]')
# plt.legend(loc='upper left')
# plt.show()

ppn = Perceptron(eta=0.1, n_iter=100, progress_bar=False)
ppn.fit(X, y)
# plt.plot(range(1, len(ppn.errors_) + 1), ppn.errors_, marker='o')
# plt.xlabel('Epochs')
# plt.ylabel('Number of misclassifications')
# plt.show()

plot_decision_regions(X, y, classifier=ppn)
plt.xlabel('petal length [cm]')
plt.ylabel('sepal length [cm]')
plt.legend(loc='upper left')
plt.show()