Пример #1
0
def main():

    df = pd.read_csv(
        'https://archive.ics.uci.edu/ml/'
        'machine-learning-databases/iris/iris.data',
        header=None)

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

    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()

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

    plt.figure()
    plt.plot(range(1, len(ada.cost_) + 1), ada.cost_, marker='o')
    plt.xlabel('Epochs')
    plt.ylabel('Sum-squared-error')
    plt.show()
    '''
X = df.iloc[:,[1,2]].values
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()

############
# analysis #
############

ppn = Perceptron.Perceptron(eta=0.1,n_iter=10)
ppn.fit(X,y)

ada = AdalineGD.AdalineGD(eta=0.01, n_iter=15)
ada.fit(X_std, y)

adaS = AdalineSGD.AdalineSGD(eta=0.01, n_iter=15, random_state=1)
adaS.fit(X_std,y)

########
# plot #
########

plt.plot(range(1,len(ppn.errors_) + 1), ppn.errors_, marker='o')
plt.xlabel('Epochs')
plt.ylabel('Number of misclassifications')
plt.show()

plt.plot(range(1,len(ada.cost_) + 1), ada.cost_, marker='o')
plt.xlabel('Epochs')
plt.ylabel('Sum-squared-error')
plt.show()
Пример #3
0
                    y=X[y == cl, 1],
                    alpha=0.8,
                    c=colors[idx],
                    marker=markers[idx],
                    label=cl,
                    edgecolor='black')


#データコピー
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()

#確率的勾配降下法によるadalineの学習(標準化後、学習率eta = 0.01)
ada = AdalineSGD.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('Adline Stocahstic Gradient Descent')
#軸のラベル設定
plt.xlabel('sepal length [standardized]')
plt.ylabel('petal length [standardized]')
#凡例の設置
plt.legend(loc='upper left')
#図の表示 tight_layout() 複数グラフのときのサイズ調整
plt.tight_layout()
plt.show()
Пример #4
0
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import AdalineSGD as ppn

import plot_decision_regions as pdr

df=pd.read_csv('iris.data',header=None)
y = df.iloc[0:100,4].values
y = np.where(y == 'Iris-versicolor' ,-1,1)
X = df.iloc[0:100,[0,2]].values # get first 100 rows and col0 and col2
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()
ppn3 = ppn.AdalineSGD(0.01 , 15, True, 1)
ppn3.fit(X_std,y)

pdr.plot_decision_regions(X_std, y, classifier=ppn3)
plt.xlabel('sepal length [standardized]')
plt.ylabel('petal length [standardized]')
plt.title('Adaline - Stochastic Gradient Descent')
plt.legend(loc="upper left")
plt.show()

plt.plot(range(1,len(ppn3.cost_)+1),ppn3.cost_,marker='o')
plt.xlabel('Epochs')
plt.ylabel('Average Cost')
plt.show()


import matplotlib.pyplot as plt
import numpy as np

# grabs the first 100 class labels corresponding to setosa and versicolor.
y = df.iloc[0:100, 4].values

# coverts class labels to -1 (for setosa) and 1 (for the rest, i.e., versicolor).
y = np.where(y == 'Iris-setosa', -1, 1)

# grabs sepal length and petal length, two features in columns 0 and 2.
X = df.iloc[0:100, [0, 2]].values

# creates Adaline using the class we made
from AdalineSGD import *
ada = AdalineSGD(eta=0.01, n_iter=15, random_state=1)

# standardizes dataset X
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()

# trains the Adaline on our standardized Iris data
ada.fit(X_std, y)

# plots the decision regions and data
from DecisionRegionPlotter import *
plot_decision_regions(X_std, y, classifier=ada)
plt.title('Adaline - Stochastic Gradient Descent')
plt.xlabel('sepal length [standardized]')
plt.ylabel('petal length [standardized]')
Пример #6
0
    header=None)

# setosa와 versicolor 선택
# setosa이면 -1을 할당해준다. 자동적으로 versicolor이면 1을 할당해준다.
y = df.iloc[0:100, 4].values
y = np.where(y == 'Iris-setosa', -1, 1)

# 꽃받침 길이와 꽃잎 길이를 추출합니다.
# pandas의 values 메서드를 사용해서 numpy array를 반환받는다.
X = df.iloc[0:100, [0, 2]].values

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()

ada = sad.AdalineSGD(n_iter=15, eta=0.01)
ada.fit(X_std, y)

plot_decision_regions(X_std, y, classifier=ada)
plt.title('Adaline - Stochastic Gradient Descent')
plt.xlabel('sepal length [normalized]')
plt.ylabel('petal length [normalized]')
plt.legend(loc='upper left')
# tight_layout()을 안하면 그래프가 어정쩡하게 나옴
plt.tight_layout()
plt.show()

plt.plot(range(1, len(ada.cost_) + 1), ada.cost_, marker='o')
plt.xlabel('Epochs')
plt.ylabel('Average Cost')
plt.show()