def main(): # import wind data import pandas as pd df_wine = pd.read_csv('wine.data',header=None) df_wine.columns=['Class label','Alcohol','Malic acid','Ash','Alcalinity of ash','Magnesium','Total phenols','Flavanoids' ,'Nonflavanoid phenols','Proanthocynins','Color intensity','Hue','OD280/OD315 of diluted wines', 'Proline'] df_wine = df_wine[df_wine['Class label'] !=1] # drop 1 class y = df_wine['Class label'].values X = df_wine[['Alcohol','OD280/OD315 of diluted wines']].values # encode the data and split from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import train_test_split le = LabelEncoder() y = le.fit_transform(y) y[y==0] = -1 X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=1,stratify=y) ada = Adaboost(N_classifiers=500,max_depth=1,learning_rate=.95,random_state=2) ada.train(X_train,y_train) #ada.predict(X_test) import matplotlib.pyplot as plt from Perceptron import plot_decision_regions plt.figure() plot_decision_regions(X,y,classifier=ada) plt.title('Adaboost') plt.xlabel('Alcohol') plt.ylabel('OD280/OD315 of diluted wines') plt.show()
def main(): # import data from sklearn import datasets iris = datasets.load_iris() X = iris.data[:, [2, 3]] y = iris.target from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1, stratify=y) nb = NaiveBayes() nb.fit(X_train, y_train) # plot decision regions import pandas as pd import matplotlib.pyplot as plt import numpy as np from Perceptron import plot_decision_regions #plt.figure() plot_decision_regions(X, y, classifier=nb) plt.title('Naive Bayes') plt.xlabel('sepal length [standardized]') plt.ylabel('petal length [standardized]') plt.show()
def main(): import pandas as pd import matplotlib.pyplot as plt import numpy as np from Perceptron import plot_decision_regions df = pd.read_csv('iris.data') df.tail() # extract setosa and versicolor y = df.iloc[0:100, 4].values y = np.where(y == 'Iris-setosa', 0, 1) # extract sepal length and petal length X = df.iloc[0:100, [0, 2]].values # standardize the 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() # instantiate the logistic regression classifier lda = LDA() lda.fit(X_std, y) # plot the decision regions plt.figure() plot_decision_regions(X_std, y, classifier=lda) plt.title(lda.__class__.__name__) plt.xlabel('sepal length [standardized]') plt.ylabel('petal length [standardized]') plt.show()
def main(): import pandas as pd import matplotlib.pyplot as plt import numpy as np from Perceptron import plot_decision_regions df = pd.read_csv('iris.data') df.tail() # extract setosa and versicolor 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 # standardize the 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() # instantiate the PEGASOS classifier pegasos = PEGASOS(0.1, m=20, Ni=40, random_state=1) pegasos.fit(X_std, y) #print(pegasos.theta) # plot the decision regions plt.figure() plot_decision_regions(X_std, y, classifier=pegasos) plt.title(pegasos.__class__.__name__) plt.xlabel('sepal length [standardized]') plt.ylabel('petal length [standardized]') plt.show()
def main(): import pandas as pd import matplotlib.pyplot as plt import numpy as np df = pd.read_csv('iris.data') df.tail() # extract setosa and versicolor y = df.iloc[0:100, 4].values y = np.where(y == 'Iris-setosa', 0, 1) # extract sepal length and petal length X = df.iloc[0:100, [0, 2]].values # standardize the 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() # instantiate the logistic regression classifier lrc = LogisticRegression(n_iter=70, eta=0.8) lrc.fit(X_std, y) # visualize the results plt.figure() plt.plot(lrc.errors) plt.xlabel('# of epochs') plt.ylabel('training error') plt.show() # plot the decision regions plt.figure() plot_decision_regions(X_std, y, classifier=lrc) plt.title('Batched Logistic Regression') plt.xlabel('sepal length [standardized]') plt.ylabel('petal length [standardized]') plt.show()
def main(): import pandas as pd import matplotlib.pyplot as plt import numpy as np from Perceptron import plot_decision_regions # import data from sklearn import datasets iris = datasets.load_iris() X = iris.data[:, [2, 3]] y = iris.target from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1, stratify=y) # test = BT_node((X,y),metric='entropy') # test.update() # test.info() # # test.lchild.update() # test.lchild.info() # instantiate the Decision Tree classifier dc = Decision_Tree(impurity_fun='entropy', max_depth=3) dc.fit(X_train, y_train, np.ones(y_train.shape) / len(y_train)) # plot the decision regions plt.figure() plot_decision_regions(X, y, classifier=dc) plt.title('Decision Tree') plt.xlabel('sepal length [standardized]') plt.ylabel('petal length [standardized]') plt.show()
ax[1].set_xlabel('Epochs') ax[1].set_ylabel('Sum-squared-error') ax[1].set_title('Adaline - Learning rate 0.0001') # plt.savefig('images/02_11.png', dpi=300) plt.show() ## データに対して標準化を行う 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 = 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') plt.xlabel('Epochs') plt.ylabel('Sum-squared-error') plt.tight_layout() plt.show()