def PLOT_DB(lda_first, qda_first, X, y, X_test, y_test):

    plt.figure(1)

    # Plot the LDA
    plt.subplot(1, 2, 1)
    plt.subplots_adjust(bottom=0.25, right=0.75)
    plot_decision_regions(X=X,
                          y=y,
                          clf=lda_first,
                          colors='limegreen,red',
                          legend=2)
    title_string1 = "Linear Decision Boundry \nAccuracy is : " + str(str(
        lda_first.score(X_test, y_test) * 100) + ' %')
    plt.title(title_string1)
    plt.scatter(lda_first.means_[0][0], qda_first.means_[0][1], c='blue',
                marker='+', linewidth='5', s=180, label="Class 0 mean")
    plt.scatter(lda_first.means_[1][0], qda_first.means_[1][1], c='yellow',
                marker='+', linewidth='5', s=180, label="Class 1 mean")
    plt.xlabel("Variable " + '$x_1$')
    plt.ylabel("Variable " + '$x_2$')
    plt.legend(loc='upper left')

    plt.subplot(1, 2, 2)
    plt.subplots_adjust(bottom=0.25, right=0.75)
    plot_decision_regions(X=X,
                          y=y,
                          clf=qda_first,
                          colors='limegreen,red',
                          legend=2)
    plt.title("Quadratic Decision Boundry \nAccuracy is : " + str(str(
        qda_first.score(X_test, y_test) * 100) + ' %'))
    plt.scatter(qda_first.means_[0][0], qda_first.means_[0][1], c='blue',
                marker='+', linewidth='5', s=180, label="Class 0 mean")
    plt.scatter(qda_first.means_[1][0], qda_first.means_[1][1], c='yellow',
                marker='+', linewidth='5', s=180, label="Class 1 mean")
    plt.xlabel("Variable " + '$x_1$')
    plt.ylabel("Variable " + "$x_2$")
    plt.legend(loc='upper left')

    plt.tight_layout()
Ejemplo n.º 2
0
plt.xlabel('sepal length ')
plt.ylabel('petal length')
plt.show()

plt.plot(range(1, len(ppn.errors_)+1), ppn.errors_, marker='o')
plt.xlabel('Iterations')
plt.ylabel('Missclassifications')
plt.show()"""

import matplotlib.pyplot as plt
from mlxtend.evaluate import plot_decision_regions
# versicolor, virginica, setosa
y2 = df.iloc[45:80, 4].values
y2 = np.where(y2 == 'Iris-virginica', -1, 1)

# sepal width and petal width
X2 = df.iloc[45:80,  [1,3]].values

ppn = Perceptron(epochs=10, eta=0.01)
ppn.train(X2, y2)

y2 = df.iloc[0:150, 4].values
y2 = np.where(y2 == 'Iris-virginica', -1, 1)
X2 = df.iloc[0:150, [1,3]].values
plot_decision_regions(X2, y2, clf=ppn)
plt.show()

plt.plot(range(1, len(ppn.errors_)+1), ppn.errors_, marker='o')
plt.xlabel('Iterations')
plt.ylabel('Missclassifications')
plt.show()
Ejemplo n.º 3
0
                  n_features=X.shape[1],
                  n_hidden=50,
                  l2=0.00,
                  l1=0.0,
                  epochs=300,
                  eta=0.01,
                  alpha=0.0,
                  decrease_const=0.0,
                  minibatches=1,
                  shuffle_init=False,
                  shuffle_epoch=False,
                  random_seed=1,
                  print_progress=3)

nn = nn.fit(X, y)

y_pred = nn.predict(X)

acc = np.sum(y == y_pred, axis=0) / X.shape[0]
print('Accuracy: %.2f%%' % (acc * 100))

plot_decision_regions(X, y, clf=nn, legend=2)
plt.title("Logistic regression - gd")
plt.show()

plt.plot(range(len(nn.cost_)), nn.cost_)
plt.ylim([0, 300])
plt.xlabel('Epochs')
plt.ylabel('Cost')
plt.show()
Ejemplo n.º 4
0
lr = LogisticRegression(eta = 0.1,
                        l2_lambda=0.0,
                        epochs=500,
                        #minibatches=1, # 1 for Gradient Descent
                        #minibatches=len(y), #  len(y) for SGD learning
                        minibatches=5, # 100/5 = 20 -> minibatch-s
                        random_seed=1,
                        print_progress=3)
lr.fit(X, y)


y_pred = lr.predict(X)
print('Last 3 Class Labels: %s' % y_pred[-3:])
y_pred = lr.predict_proba(X)
print('Last 3 Class Labels: %s' % y_pred[-3:])


plot_decision_regions(X, y, clf=lr)
plt.title("Logistic regression - gd")
plt.show()

plt.plot(range(len(lr.cost_)), lr.cost_)
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.show()




Ejemplo n.º 5
0
def test_pass():
    sr.fit(X[:, :2], y)
    plot_decision_regions(X=X[:, :2], y=y, clf=sr)
Ejemplo n.º 6
0
def test_ylist():
    sr.fit(X[:, :2], y)
    plot_decision_regions(X=X[:, :2], y=list(y), clf=sr)
Ejemplo n.º 7
0
X = X[0:100]
y = y[0:100]

X[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std()
X[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std()

lr = LogisticRegression(
    eta=0.1,
    l2_lambda=0.0,
    epochs=500,
    #minibatches=1, # 1 for Gradient Descent
    #minibatches=len(y), #  len(y) for SGD learning
    minibatches=5,  # 100/5 = 20 -> minibatch-s
    random_seed=1,
    print_progress=3)
lr.fit(X, y)

y_pred = lr.predict(X)
print('Last 3 Class Labels: %s' % y_pred[-3:])
y_pred = lr.predict_proba(X)
print('Last 3 Class Labels: %s' % y_pred[-3:])

plot_decision_regions(X, y, clf=lr)
plt.title("Logistic regression - gd")
plt.show()

plt.plot(range(len(lr.cost_)), lr.cost_)
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.show()
import numpy as np
from sklearn.cross_validation import train_test_split, cross_val_score
from sklearn.datasets import load_boston, load_breast_cancer, load_iris, make_moons, make_gaussian_quantiles
from sklearn.metrics import mean_squared_error

from mlxtend.evaluate import plot_decision_regions
import matplotlib.pyplot as plt

from pines.estimators import DecisionTreeRegressor, DecisionTreeClassifier
from pines.tree_builders import TreeType

if __name__ == '__main__':
    model = DecisionTreeClassifier(max_n_splits=3, max_depth=10, tree_type=TreeType.OBLIVIOUS)
    X, y = make_gaussian_quantiles(n_samples=10000, n_classes=4)

    model.fit(X, y)
    print(model.tree_)
    plot_decision_regions(X, y, clf=model, res=0.02, legend=2)
    plt.savefig('decision_boundary.png')
Ejemplo n.º 9
0
from sklearn.cross_validation import train_test_split
from mlxtend.evaluate import plot_decision_regions
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.svm import SVC

# Loading some example data
iris = datasets.load_iris()
X = iris.data[:, [0,2]]
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=0) 

# Training a classifier
svm = SVC(C=0.5, kernel='rbf')
svm.fit(X_train, y_train)

# Plotting decision regions

plot_decision_regions(X, y, clf=svm, X_highlight=X_test, res=0.02, legend=2)

# Adding axes annotations
plt.xlabel('sepal length [standardized]')
plt.ylabel('petal length [standardized]')
plt.title('SVM on Iris')
plt.show()
import matplotlib.pyplot as plt
from sklearn import datasets, svm
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import Perceptron
from sklearn.metrics import accuracy_score
from mlxtend.evaluate import plot_decision_regions

np.random.seed(0)

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)

clf = svm.SVC(kernel='rbf', random_state=0, gamma=0.3, C=10.0)
clf.fit(X_xor, y_xor)

plot_decision_regions(X_xor, y_xor, clf=clf)


# 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.ylim(-3.0)
# plt.legend()
plt.show()
Ejemplo n.º 11
0
# Gradient Descent

nn1 = MLP(hidden_layers=[50],
          l2 = 0.00,
          l1 = 0.0,
          epochs=150,
          eta=0.05,
          momentum=0.1,
          decrease_const=0.0,
          minibatches=1,
          random_seed=1,
          print_progress=3)

nn1 = nn1.fit(X_std, y)
fig = plot_decision_regions(X=X_std, y=y, clf=nn1, legend=2)
plt.title('Multi-layer perception w. 1 hidden layer (logistic sigmod)')
plt.show()

plt.plot(range(len(nn1.cost_)), nn1.cost_)
plt.ylabel("Cost")
plt.xlabel("Epochs")
plt.show()

print 'Accuracy: %.2f%%' % (100 * nn1.score(X_std, y))


# Stochastic Gradient Descent

nn2 = MLP(hidden_layers=[50],
          l2=0.00,
Ejemplo n.º 12
0

# Gradient Descent

nn1 = TfMultiLayerPerceptron(eta=0.5,
                             epochs=20,
                             hidden_layers=[10],
                             activations=['logistic'],
                             optimizer='gradientdescent',
                             print_progress=3,
                             minibatches=1,
                             random_seed=1)


nn1 = nn1.fit(X_std, y)
fig = plot_decision_regions(X=X_std, y=y, clf=nn1, legend=2)
plt.title('Multi-layer perception w. 1 hidden layer (logistic sigmod)')
plt.show()

plt.plot(range(len(nn1.cost_)), nn1.cost_)
plt.ylabel("Cost")
plt.xlabel("Epochs")
plt.show()

print 'Accuracy: %.2f%%' % (100 * nn1.score(X_std, y))


# Stochastic Gradient Descent

nn2 = TfMultiLayerPerceptron(eta=0.5,
                             epochs=20,
Ejemplo n.º 13
0
                update = self.eta * (target - self.predict(xi))
                self.w_[1:] +=  update * xi
                self.w_[0] +=  update
                errors += int(update != 0.0)
            self.errors_.append(errors)
        return self

    def net_input(self, X):
        return np.dot(X, self.w_[1:]) + self.w_[0]

    def predict(self, X):
        return np.where(self.net_input(X) >= 0.0, 1, 0)


X = np.array([[-1,-1],[1,1],[-1,1],[1,-1]])
Y = np.array([1,1,0,0])
p = Perceptron(epochs=10, eta=1)
p.train(X, Y)

print('Weights: %s' % p.w_)
plot_decision_regions(X, Y, clf=p)
plt.title('Perceptron')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

plt.plot(range(1, len(p.errors_)+1), p.errors_, marker='o')
plt.xlabel('Iterations')
plt.ylabel('Missclassifications')
plt.show()
Ejemplo n.º 14
0
    def predict(self, X):
        np.sign(np.dot(X, self.weight_vector))


def read_data_set(self):
    return np.genfromtxt(self.data_set, delimiter=",", comments="#")


if __name__ == "__main__":
    arg_learning_rate = sys.argv[1]
    arg_maximum_iterations = sys.argv[2]
    df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=None)

    y = df.iloc[0:100, 4].values
    y = np.where(y == 'Iris-setosa', -1, 1)

    # extract sepal length and petal length
    X = df.iloc[0:100, [0, 2]].values
    perceptron = Perceptron(float(arg_learning_rate), int(arg_maximum_iterations))
    perceptron.fit(X, y)
    print X
    plot_decision_regions(X, y, clf=perceptron)
    plt.title('Perceptron')
    plt.xlabel('sepal length [cm]')
    plt.ylabel('petal length [cm]')
    plt.show()
    print X
    #perceptron.plot(X, y)
    # X_test = iris.data[100:]
    # print perceptron.predict(X_test)
Ejemplo n.º 15
0
X = X[:, [0, 3]]
X = X[0:100]
y = y[0:100]

X[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()
X[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()


# Closed Form Solution
ada = Adaline(epochs=30,
              eta=0.01,
              minibatches=None,
              random_seed=1)

ada.fit(X, y)
plot_decision_regions(X, y, clf=ada)
plt.title('Adaline - Stochastic Gradient Descent')
plt.show()



# (Stochastic) Gradient Descent
ada2 = Adaline(epochs=30,
               eta=0.01,
               minibatches=1, # 1 for GD learning
               #minibatches=len(y), # len(y) for SGD learning
               #minibatches=5, # for SGD learning w. minibatch size 20
               random_seed=1,
               print_progress=3)
ada2.fit(X, y)
plot_decision_regions(X, y, clf=ada2)
Ejemplo n.º 16
0
print('y', y)

# sepal length and petal length
X = np.array([[float(r[0]), float(r[2])] for r in df[0:100]])

print('X', X)

ppn = Perceptron(epochs=10, eta=0.01)

#print('ppn', ppn)

ppn.train(X, y)
print('Weights: %s' % ppn.w_)

plot_decision_regions(X, y, clf=ppn)
plt.title('Perceptron')
plt.xlabel('sepal length [cm]')
plt.ylabel('petal length [cm]')
plt.show()

plt. clf()
plt.plot(range(1, len(ppn.errors_)+1), ppn.errors_, marker='o')
plt.xlabel('Iterations')
plt.ylabel('Missclassifications')
plt.show()


# versicolor and virginica
y2 = np.array([-1 if r[4] == 'Iris-virginica' else 1 for r in df[50:150]])
Ejemplo n.º 17
0
plt.title('Adaline - Learning rate 0.01')
plt.show()

ada = AdalineGD(epochs=10, eta=0.0001).train(X, y)
plt.plot(range(1, len(ada.cost_) + 1), ada.cost_, marker='o')
plt.xlabel('Iterations')
plt.ylabel('Sum-squared-error')
plt.title('Adaline - Learning rate 0.0001')
plt.show()

#Standardize features
X_std = np.copy(X)
X_std[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std()
X_std[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std()

import matplotlib.pyplot as plt
from mlxtend.evaluate import plot_decision_regions

ada = AdalineGD(epochs=15, eta=0.01)

ada.train(X_std, y)
plot_decision_regions(X_std, y, clf=ada)
plt.title('Adaline - Gradient Descent')
plt.xlabel('sepal length [standardized]')
plt.ylabel('petal length [standardized]')
plt.show()

plt.plot(range(1, len(ada.cost_) + 1), ada.cost_, marker='o')
plt.xlabel('Iterations')
plt.ylabel('Sum-squared-error')
plt.show()
Ejemplo n.º 18
0
                  l2 = 0.00,
                  l1 = 0.0,
                  epochs  = 300,
                  eta = 0.01,
                  alpha = 0.0,
                  decrease_const = 0.0,
                  minibatches = 1,
                  shuffle_init = False,
                  shuffle_epoch = False,
                  random_seed = 1,
                  print_progress = 3)

nn = nn.fit(X, y)

y_pred = nn.predict(X)

acc = np.sum(y == y_pred, axis=0) / X.shape[0]
print('Accuracy: %.2f%%' % (acc * 100))


plot_decision_regions(X, y, clf=nn, legend=2)
plt.title("Logistic regression - gd")
plt.show()

plt.plot(range(len(nn.cost_)), nn.cost_)
plt.ylim([0, 300])
plt.xlabel('Epochs')
plt.ylabel('Cost')
plt.show()

                                                    random_state=0)

# Feature scaling - computes mean and standard deviation
sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)

# Feed perceptron
ppn = Perceptron(n_iter=40, eta0=0.1, random_state=0)
ppn.fit(X_train_std, y_train)
y_pred = ppn.predict(X_test_std)
print('Missclassified samples: %d' % (y_test != y_pred).sum())
print('Accuracy: %.2f' % accuracy_score(y_test, y_pred))

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

print len(X_test.shape)
plot_decision_regions(X_combined_std,
                      y_combined,
                      clf=ppn,
                      res=0.02,
                      legend=2,
                      X_highlight=X_test)

plt.xlabel('petal length [standardized]')
plt.ylabel('petal width [standardized]')
plt.legend(loc='upper left')
plt.show()
Ejemplo n.º 20
0
    'Sample code number', 'Clump Thickness ', 'Uniformity of Cell Size',
    'Uniformity of Cell Shape', 'Marginal Adhesion',
    'Single Epithelial Cell Size ', 'Bare Nuclei ', 'Bland Chromatin',
    'Normal Nucleoli', 'Mitoses', 'Class'
]
data = pd.read_csv('../breast-cancer-wisconsin.data', names=column)

data.replace(to_replace='?', value=np.nan, inplace=True)
data = data.dropna()

x_train, x_test, y_train, x_test = train_test_split(data[column[1:10]],
                                                    data[column[10]],
                                                    test_size=0.25)
ss_train = StandardScaler()
x_train = ss_train.fit_transform(x_train)
x_test = ss_train.transform(x_test)

lr = LogisticRegression(C=100.0, random_state=1)
lr.fit(x_train, y_train)

plot_decision_regions(X=x_test,
                      y=x_test,
                      classifier=lr,
                      test_idx=range(105, 150))
plt.xlabel('petal length [standardized]')
plt.ylabel('petal width [standardized]')
plt.legend(loc='upper left')
plt.tight_layout()
# plt.savefig('images/03_06.png', dpi=300)
plt.show()
def test_pass():
    sr.fit(X[:, :2], y)
    plot_decision_regions(X=X[:, :2], y=y, clf=sr)
import numpy as np
from sklearn.cross_validation import train_test_split, cross_val_score
from sklearn.datasets import load_boston, load_breast_cancer, load_iris, make_moons, make_gaussian_quantiles
from sklearn.metrics import mean_squared_error

from mlxtend.evaluate import plot_decision_regions
import matplotlib.pyplot as plt

from pines.estimators import DecisionTreeRegressor, DecisionTreeClassifier
from pines.tree_builders import TreeType

if __name__ == '__main__':
    model = DecisionTreeClassifier(max_n_splits=3,
                                   max_depth=10,
                                   tree_type=TreeType.OBLIVIOUS)
    X, y = make_gaussian_quantiles(n_samples=10000, n_classes=4)

    model.fit(X, y)
    print(model.tree_)
    plot_decision_regions(X, y, clf=model, res=0.02, legend=2)
    plt.savefig('decision_boundary.png')
Ejemplo n.º 23
0
def test_y_dim_fail():
    sr.fit(X, y)
    plot_decision_regions(X=X, y=X, clf=sr)
sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)

# Feed perceptron
ppn = Perceptron(n_iter=40, eta0=0.1, random_state=0)
ppn.fit(X_train_std, y_train)
y_pred = ppn.predict(X_test_std)
print('Missclassified samples: %d' % (y_test != y_pred).sum())
print('Accuracy: %.2f' % accuracy_score(y_test, y_pred))


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

print len(X_test.shape)
plot_decision_regions(
    X_combined_std,
    y_combined,
    clf=ppn,
    res=0.02,
    legend=2,
    X_highlight=X_test
)

plt.xlabel('petal length [standardized]')
plt.ylabel('petal width [standardized]')
plt.legend(loc='upper left')
plt.show()
Ejemplo n.º 25
0
from sklearn import datasets
import matplotlib.pyplot as plt
import seaborn
from mlxtend.evaluate import plot_decision_regions
import matplotlib.gridspec as gridspec
import itertools
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier

iris = datasets.load_iris()
X, y = iris.data[:, 1:3], iris.target

clf1 = LogisticRegression(random_state=1)
clf2 = RandomForestClassifier(random_state=1)
clf3 = GaussianNB()

gs = gridspec.GridSpec(2, 2)

fig = plt.figure(figsize=(10,8))

for clf, lab, grd in zip([clf1, clf3 ],
                         ['Logistic Regression', 'Naive Bayes'],
                         itertools.product([0,1], repeat=2)):
        clf.fit(X, y)
        ax = plt.subplot(gs[grd[0], grd[1]])
        fig = plot_decision_regions(X=X, y=y, clf=clf)
        plt.title(lab)

plt.savefig('figure/decision_boundary.svg')
Ejemplo n.º 26
0
 def plot_decision_boundary(self, X, y):
     #plot the boundary
     plot_decision_regions(X, y, clf = self)
     plt.show()
Ejemplo n.º 27
0
# setosa and versicolor
y = df.iloc[0:100, 4].values
y = np.where(y == 'Iris-setosa', -1, 1)

# sepal length and petal length
X = df.iloc[0:100, [0,2]].values


import matplotlib.pyplot as plt
from mlxtend.evaluate import plot_decision_regions
ppn = Perceptron(epochs=10, eta=0.1)

ppn.train(X, y)
print('Weights: %s' % ppn.w_)
plot_decision_regions(X, y, clf=ppn)
plt.title('Perceptron')
plt.xlabel('sepal length [cm]')
plt.ylabel('petal length [cm]')
plt.show()

plt.plot(range(1, len(ppn.errors_)+1), ppn.errors_, marker='o')
plt.xlabel('Iterations')
plt.ylabel('Missclassifications')
plt.show()

# versicolor and virginica
y2 = df.iloc[50:150, 4].values
y2 = np.where(y2 == 'Iris-virginica', -1, 1)

# sepal width and petal width
Ejemplo n.º 28
0
                update = self.eta * (target - self.predict(xi))
                self.w_[1:] += update * xi
                self.w_[0] += update
                errors += int(update != 0.0)
            self.errors_.append(errors)
        return self

    def net_input(self, X):
        return np.dot(X, self.w_[1:]) + self.w_[0]

    def predict(self, X):
        return np.where(self.net_input(X) >= 0.0, 1, 0)


X = np.array([[-1, -1], [1, 1], [-1, 1], [1, -1]])
Y = np.array([1, 1, 0, 0])
p = Perceptron(epochs=10, eta=1)
p.train(X, Y)

print('Weights: %s' % p.w_)
plot_decision_regions(X, Y, clf=p)
plt.title('Perceptron')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

plt.plot(range(1, len(p.errors_) + 1), p.errors_, marker='o')
plt.xlabel('Iterations')
plt.ylabel('Missclassifications')
plt.show()
Ejemplo n.º 29
0
df = pd.read_csv(
    'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',
    header=None)
#在线获取鸢尾花数据集
y = df.iloc[0:100, 4].values
y = np.where(y == 'Iris-setosa', -1, 1)  #很显然这里对应的是前面的设置的目标的
X = df.iloc[0:100, [0, 2]].values

import matplotlib.pyplot as plt
from mlxtend.evaluate import plot_decision_regions

ppn = Perceptron(epochs=10, eta=0.1)

ppn.train(X, y)
print 'Weight:%s', ppn.w_
plot_decision_regions(X, y, clf=ppn)
plt.title('Perceptron')
plt.xlabel('sepal length [cm]')
plt.ylabel('penal length [cm]')
plt.show()

plt.plot(range(1, len(ppn.errors_) + 1), ppn.errors_, marker='+')
plt.xlabel('Iterations')
plt.ylabel('Missclassifications')
plt.show()

########################################
y2 = df.iloc[50:150, 4].values
y2 = np.where(y2 == 'Iris-setosa', -1, 1)
X2 = df.iloc[50:150, [1, 3]].values
ppn = Perceptron(epochs=25, eta=0.01)
Ejemplo n.º 30
0
from mlxtend.evaluate import plot_decision_regions
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import itertools

clf1 = LogisticRegression(random_state=1)
clf2 = RandomForestClassifier(random_state=1)
clf3 = GaussianNB()
clf4 = SVC()

iris = datasets.load_iris()
X = iris.data[:, [0,2]]
y = iris.target

gs = gridspec.GridSpec(2, 2)

fig = plt.figure(figsize=(10, 8))

for clf, lab, grd in zip([clf1, clf2, clf3, clf4],
                         ['Logistic Regression', 'Random Forest', 'Naive Bayes', 'SVM'],
                         itertools.product([0, 1], repeat=2)):
  clf.fit(X, y)
  ax = plt.subplot(gs[grd[0], grd[1]])
  fig = plot_decision_regions(X=X, y=y, clf=clf, legend=2)
  plt.title(lab)

plt.show()


Ejemplo n.º 31
0
from sklearn.ensemble import RandomForestClassifier
from mlxtend.classifier import EnsembleVoteClassifier
from mlxtend.data import iris_data
from mlxtend.evaluate import plot_decision_regions

# Initializing Classifiers
clf1 = LogisticRegression(random_state=0)
clf2 = RandomForestClassifier(random_state=0)
clf3 = SVC(random_state=0, probability=True)
eclf = EnsembleVoteClassifier(clfs=[clf1, clf2, clf3],
                              weights=[2, 1, 1],
                              voting='soft')

# Loading some example data
X, y = iris_data()
X = X[:, [0, 2]]

# Plotting Decision Regions
gs = gridspec.GridSpec(2, 2)
fig = plt.figure(figsize=(10, 8))

for clf, lab, grd in zip(
    [clf1, clf2, clf3, eclf],
    ['Logistic Regression', 'Random Forest', 'Naive Bayes', 'Ensemble'],
        itertools.product([0, 1], repeat=2)):
    clf.fit(X, y)
    ax = plt.subplot(gs[grd[0], grd[1]])
    fig = plot_decision_regions(X=X, y=y, clf=clf, legend=2)
    plt.title(lab)
plt.show()
Ejemplo n.º 32
0
import matplotlib.pyplot as plt
from sklearn import datasets, svm
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import Perceptron
from sklearn.metrics import accuracy_score
from mlxtend.evaluate import plot_decision_regions

np.random.seed(0)

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)

clf = svm.SVC(kernel='rbf', random_state=0, gamma=0.3, C=10.0)
clf.fit(X_xor, y_xor)

plot_decision_regions(X_xor, y_xor, clf=clf)

# 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.ylim(-3.0)
# plt.legend()
plt.show()
Ejemplo n.º 33
0
from mlxtend.evaluate import plot_decision_regions
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.svm import SVC

# Loading some example data
iris = datasets.load_iris()
X = iris.data[:, [0,2]]
y = iris.target

# Training a classifier
svm = SVC(C=0.5, kernel='linear')
svm.fit(X,y)

# Plotting decision regions
plot_decision_regions(X, y, clf=svm, res=0.02, legend=2)

# Adding axes annotations
plt.xlabel('sepal length [cm]')
plt.ylabel('petal length [cm]')
plt.title('SVM on Iris')
plt.show()
Ejemplo n.º 34
0
# Loading Data

X, y = iris_data()
X = X[:, [0, 3]] # sepal length and petal width
X = X[0:100] # class 0 and class 1
y = y[0:100] # class 0 and class 1

# standardize
X[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()
X[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()
# Note that this implementation of the Perceptron expects binary class labels in {0, 1}.


# Rosenblatt Perceptron

ppn = Perceptron(epochs=5, # num of passes, default 50
                 eta=0.05, # learning rate 0.0 ~ 1.0, default 0.1
                 random_seed=0,
                 print_progress=3)
ppn.fit(X, y)

plot_decision_regions(X, y, clf=ppn)
plt.title('Perceptron - Rosenblatt Perceptron Rule')
plt.show()

print('Bias & Weights: %s' % ppn.w_)

plt.plot(range(len(ppn.cost_)), ppn.cost_)
plt.xlabel('Iterations')
plt.ylabel('Missclassifications')
plt.show()
Ejemplo n.º 35
0
                                              y,
                                              cv=3,
                                              scoring='accuracy')
    print 'Accuracy: %0.2f (+/- %0.2f [%s]' % (scores.mean(), scores.std(),
                                               label)

gs = gridspec.GridSpec(2, 2)
fig = plt.figure(figsize=(10, 8))

for clf, lab, grd in zip(
    [clf1, clf2, clf3, sclf],
    ['KNN', 'Random Forest', 'Naive bayes', 'StackingClassifier'],
        itertools.product([0, 1], repeat=2)):
    clf.fit(X, y)
    ax = plt.subplot(gs[grd[0], grd[1]])
    fig = plot_decision_regions(X=X, y=y, clf=clf)
    plt.title(lab)

plt.show()

# Stacked classification and grid search

params = {
    'kneighborsclassifier__n_neighbors': [1, 5],
    'randomforestclassifier__n_estimators': [10, 50],
    'meta-logisticregression__C': [0.1, 10.0]
}

grid = GridSearchCV(estimator=sclf, param_grid=params, cv=5, refit=True)

grid.fit(X, y)
Ejemplo n.º 36
0
from mlxtend.evaluate import plot_decision_regions
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.svm import SVC

# Loading some example data
iris = datasets.load_iris()
X = iris.data[:, 2]
X = X[:, None]
y = iris.target

# Training a classifier
svm = SVC(C=0.5, kernel='linear')
svm.fit(X,y)

# Plotting decision regions
plot_decision_regions(X, y, clf=svm, res=0.02, legend=2)

# Adding axes annotations
plt.xlabel('sepal length [cm]')
plt.title('SVM on Iris')

plt.show()
Ejemplo n.º 37
0
X_std[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()

# # choose different learning rate
# ada = AdalineGD(iter_num=10, alpha=0.01).train(X, y)
# plt.plot(range(1, len(ada.cost_)+1), np.log10(ada.cost_), marker='o')
# plt.xlabel('Iterations')
# plt.ylabel('log(Sum-squared-error)')
# plt.title('Adaline - Learning rate 0.01')
# plt.show()

# ada = AdalineGD(iter_num=10, alpha=0.0001).train(X, y)
# plt.plot(range(1, len(ada.cost_)+1), ada.cost_, marker='o')
# plt.xlabel('Iterations')
# plt.ylabel('Sum-squared-error')
# plt.title('Adaline - Learning rate 0.0001')
# plt.show()


ada = AdalineGD(iter_num=15, alpha=0.01)
ada.train(X_std, y)
plot_decision_regions(X_std, y, clf=ada)
plt.title('Adaline - Gradient Descent')
plt.xlabel('sepal length [standardized]')
plt.ylabel('petal length [standardized]')
plt.show()

plt.plot(range(1, len( ada.cost_)+1), ada.cost_, marker='o')
plt.xlabel('Iterations')
plt.ylabel('Sum-squared-error')
plt.show()
Ejemplo n.º 38
0
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.svm  import SVC
from IPython.display import Image

# <codecell>

iris = datasets.load_iris()
x = iris.data[:,[0,2]]
y = iris.target
svm = SVC(C=0.5,kernel='linear')
svm.fit(x,y)

# <codecell>

plot_decision_regions(x,y,clf = svm, res=0.1,legend = 2)
plt.xlabel('sepal length [cm]')
plt.ylabel('petal length [cm]')
plt.title('SVM on Iris')
plt.show()

# <markdowncell>

# # 问题
# 
# ## 这里是x.shape[1] 必须等于2,目前没有处理高维数据的。

# <codecell>