コード例 #1
0
            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.tight_layout()
plt.show()

svm = SVC(kernel='rbf', C=1.0, random_state=1, gamma='scale')
svm.fit(X_xor, y_xor)
plot_decision_regions(X_xor, y_xor, classifier=svm)
plt.legend(loc="upper left")
plt.tight_layout()
plt.show()

mm_s = MinMaxScaler()
iris = 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)
コード例 #2
0
print("y_test :", np.bincount(y_test))

from sklearn.preprocessing import MinMaxScaler
mm_s = MinMaxScaler()
mm_s.fit(X)
X_train_std = mm_s.transform(X_train)
X_test_std = mm_s.transform(X_test)

from sklearn.linear_model import Perceptron
from sklearn.metrics import accuracy_score

ppn = Perceptron(max_iter=40, eta0=0.1, tol=1e-3, random_state=10)
ppn.fit(X_train_std, y_train)
y_pred = ppn.predict(X_test_std)
print("잘못분류된 샘플개수: ", (y_test != y_pred).sum())
print("Accuracy: ", round((y_test == y_pred).sum() / len(y_test), 2))
print("Accuracy: ", ppn.score(X_test_std, y_test))
print("Accuracy: ", accuracy_score(y_pred, y_test))

from model.plot import plot_decision_regions
import matplotlib.pyplot as plt

X_std = np.vstack((X_train_std, X_test_std))
y = np.hstack((y_train, y_test))
plot_decision_regions(X_std, y, test_idx=range(50, 150), classifier=ppn)
plt.ylabel("petal length [standardized]")
plt.ylabel("petal width [standardized]")
plt.legend(loc="upper left")
plt.tight_layout()
plt.show()
コード例 #3
0
iris = load_iris()
X = iris.data[:,[2,3]]
y = iris.target

mm_s = MinMaxScaler()
mm_s.fit(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state =1, stratify=y)
X_train_std = mm_s.transform(X_train)
X_test_std = mm_s.transform(X_test)
tree.fit(X_train_std, y_train)

X_std = np.vstack((X_train_std, X_test_std))
y = np.hstack((y_train, y_test))

plot_decision_regions(X_std, y, classifier=tree, test_idx=range(105, 150))
plt.xlabel("petal length [cm]")
plt.ylabel("petal width [cm]")
plt.tight_layout()
plt.show()

from pydotplus import graph_from_dot_data
from sklearn.tree import export_graphviz

dot_data = export_graphviz(tree,
                        filled=True,
                        rounded=True,
                        class_names=['Setosa', 'Versicolor', 'Virgnica'],
                        feature_names=['petal length', 'petal width'],
                        out_file=None)
コード例 #4
0
X = pd.DataFrame(iris.data, columns=iris.feature_names).iloc[0:100, [0,2]].values
y = iris.target[0:100]
y = np.where(y == 0, 1, -1)

plt.scatter(df.iloc[:49, 0], df.iloc[:49, 2],
            color='red', marker='o', label='setosa')
plt.scatter(df.iloc[50:101,0], df.iloc[50:101, 2],
            color='blue', marker='x', label='versicolor')
plt.scatter(df.iloc[102:,0], df.iloc[102:, 2],
            color='green', marker='^', label='virginica')
plt.xlabel('sepal length[cm]')
plt.ylabel('petal length[cm]')
plt.legend(loc='upper left')
plt.show()

ppn = Perceptron(eta=0.01, n_iter=10)
ppn.fit(X,y)
plt.plot(range(1, len(ppn.errors_) + 1),
        ppn.errors_, marker='o')
plt.xlabel('Epoch')
plt.ylabel('Number of errors')
plt.show()

from model.plot import plot_decision_regions

plot_decision_regions(X, y, classifier=ppn)
plt.xlabel('sepal length [cm]')
plt.ylabel('petal length [cm]')
plt.legend(loc='upper left')
コード例 #5
0
iris = 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)

mm_s.fit(X)
X_train_std = mm_s.transform(X_train)
X_test_std = mm_s.transform(X_test)

svm = SVC(kernel='linear', C=1.0, random_state=1)
svm.fit(X_train_std, y_train)

X_std = np.vstack((X_train_std, X_test_std))
y = np.hstack((y_train, y_test))
plot_decision_regions(X_std, y, classifier=svm, test_idx=range(105, 150))
plt.ylabel("petal length [standardized]")
plt.ylabel("petal width [standardized]")
plt.legend(loc="upper left")
plt.tight_layout()
plt.show()

from sklearn.linear_model import SGDClassifier

ppn = SGDClassifier(loss="perceptron")
lr = SGDClassifier(loss="log")
svm = SGDClassifier(loss="hinge")

ppn.fit(X_train_std, y_train)
plot_decision_regions(X_std, y, classifier=ppn, test_idx=range(105, 150))
plt.ylabel("petal length [standardized]")
コード例 #6
0
ada2 = AdalineGD(eta=0.0001, n_iter=10).fit(X, y)
ax[1].plot(range(1, len(ada2.cost_)+1),
            np.log(ada2.cost_), marker='o',
            color='blue')
ax[1].set_xlabel('Epoch')
ax[1].set_ylabel('log(sum-square-error)')
ax[1].set_title('Adaline learning rate 0.0001')

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

ada = AdalineGD(n_iter=15, eta=0.01)
ada.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.tight_layout()
plt.show()

plt.plot(range(1, len(ada.cost_)+1),
            ada.cost_, marker='o',
            color='blue')
plt.xlabel('Epoch')
plt.ylabel('sum-square-error')
plt.title('Adaline learning rate 0.01 standardized data')

from model.adaline import AdalineSGD
コード例 #7
0
plt.tight_layout()
plt.show()

#########################################################################
######## PCA : Pricipal Component Analysis with sckit-learn      ########
#########################################################################

from sklearn.linear_model import LogisticRegression
from sklearn.decomposition import PCA

pca = PCA(n_components=2)
x_train_pca = pca.fit_transform(X_train_std)
x_test_pca = pca.fit_transform(X_test_std)
lr = LogisticRegression(solver='liblinear', multi_class='auto')
lr.fit(x_train_pca, y_train)
plot_decision_regions(X_train_pca, y_train, classifier=lr)
plt.xlabel('PC 1')
plt.ylabel('PC 2')
plt.legend(loc='lower left')
plt.tight_layout()
plt.show()

#########################################################################
######## LDA : linear Discriminant Analysis                      ########
#########################################################################
np.set_printoptions(precision=4)  # 부동소숫점 출력부분 조정
mean_vecs = []
for label in range(1, 4):
    mean_vecs.append(np.mean(X_train_std[y_train == label], axis=0))
    print('MV {}: {}\n'.format(label, mean_vecs[label - 1]))
コード例 #8
0
mm_s = MinMaxScaler()
iris = load_iris()

y = iris.target[(iris.target == 0)|(iris.target == 1)]
X = iris.data[(iris.target == 0)|(iris.target == 1)][:,[2,3]]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state =1, stratify=y)

mm_s.fit(X)
X_train_std = mm_s.transform(X_train)
X_test_std = mm_s.transform(X_test)

legd = LogisticsRegressionGD(eta=0.01, n_iter=1000, random_state=1)
legd.fit(X_train_std, y_train)

plot_decision_regions(X_train_std, y_train, classifier=legd)
plt.ylabel("petal length [standardized]")
plt.ylabel("petal width [standardized]")
plt.legend(loc="upper left")
plt.tight_layout()
plt.show()


from sklearn.linear_model import LogisticRegression

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_std = mm_s.transform(X_train)
X_test_std = mm_s.transform(X_test)