import numpy as np import matplotlib.pyplot as plt from sklearn.svm import SVC 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) 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() svm = SVC(kernel='rbf', random_state=0, gamma=0.10, C=10.0) svm.fit(X_xor, y_xor) from pdr import plot_decision_regions plot_decision_regions(X=X_xor, y=y_xor, classifier=svm) plt.legend(loc='upper left') plt.show()
test_size=0.3, random_state=0) 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)) svm = SVC(kernel='linear', C=1.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() from sklearn.linear_model import SGDClassifier SGDsvm = SGDClassifier(loss='hinge') SGDsvm.fit(X_train_std, y_train) plot_decision_regions(X_combined_std, y_combined, classifier=SGDsvm, test_idx=range(105, 150)) plt.xlabel('petal length [standardized]')
from sklearn.ensemble import RandomForestClassifier forest = RandomForestClassifier(criterion='entropy', n_estimators=10, random_state=0) from sklearn import datasets import numpy as np iris = datasets.load_iris() X = iris.data[:, [2, 3]] y = iris.target from sklearn.cross_validation import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) forest.fit(X_train, y_train) X_combined = np.vstack((X_train, X_test)) y_combined = np.hstack((y_train, y_test)) from pdr import plot_decision_regions import matplotlib.pyplot as plt plot_decision_regions(X=X_combined, y=y_combined, classifier=forest, test_idx=range(105,150)) plt.title('Random Forest') plt.xlabel('sepal length') plt.ylabel('petal length') plt.legend(loc='upper left') plt.show()
#Splitting data sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) #Combining data X_combined = np.vstack((X_train, X_test)) y_combined = np.hstack((y_train, y_test)) #Standardization sc = StandardScaler() sc.fit(X_train) X_train_std = sc.transform(X_train) X_test_std = sc.transform(X_test) #KNN model knn = KNeighborsClassifier(n_neighbors=5, p=2, metric='minkowski') knn.fit(X_train_std, y_train) #Plotting Decision regions plot_decision_regions(X_combined, y_combined, classifier=knn, test_idx=range(105, 150)) plt.xlabel('petal length [cm]') plt.ylabel('petal width [cm]') plt.title('KNN') plt.show()
from sklearn.tree import DecisionTreeClassifier tree = DecisionTreeClassifier(criterion='entropy', max_depth=3, random_state=0) from sklearn import datasets import numpy as np iris = datasets.load_iris() X = iris.data[:, [2, 3]] y = iris.target from sklearn.cross_validation import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) tree.fit(X_train, y_train) X_combined = np.vstack((X_train, X_test)) y_combined = np.hstack((y_train, y_test)) from pdr import plot_decision_regions import matplotlib.pyplot as plt plot_decision_regions(X=X_combined, y=y_combined, classifier=tree, test_idx=range(105,150)) plt.title('Decision Tree') plt.xlabel('sepal length') plt.ylabel('petal length') plt.legend(loc='upper left') plt.show()
plt.figure(0) 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) 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() plt.figure(1) svm = SVC(kernel = 'rbf', random_state = 0, gamma = 1.0, C = 10.0) svm.fit(X_xor, y_xor) plot_decision_regions(X_xor, y_xor, classifier = svm) plt.title('Kernel SVM') plt.legend(loc = 'upper left') plt.show() 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 = 0) 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))
#Standardize ss = StandardScaler() X_train_std = ss.fit_transform(X_train) X_test_std = ss.transform(X_test) #Initializing LDA and fit the training data lda = LDA(n_components=2) X_train_lda = lda.fit_transform(X_train_std, y_train) #LR training lr = LogisticRegression() lr.fit(X_train_lda, y_train) #Plot the decision regions of training set plot_decision_regions(X_train_lda, y_train, classifier=lr) plt.xlabel('LD 1') plt.ylabel('LD 2') plt.title('LDA on training set') plt.legend(loc='best') plt.show() #Plot the decision regions of test set X_test_lda = lda.fit_transform(X_test_std, y_test) plot_decision_regions(X_test_lda, y_test, classifier=lr) plt.xlabel('LD 1') plt.ylabel('LD 2') plt.title('LDA on test set') plt.legend(loc='best') plt.show()
y = df.iloc[0:100, 4].values y = np.where(y == 'Iris-setosa', -1, 1) X = df.iloc[0:100, [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') plt.ylabel('sepal length') plt.legend(loc = 'upper left') plt.show() # get the perceptron model model = Perceptron(eta = 0.1, n_iter = 10) # train the model model.fit(X, y) # plot the training error plt.plot(range(1, len(model.errors_) + 1), model.errors_, marker = 'o') plt.xlabel('Epochs') plt.ylabel('Number of misclassifications') plt.show() # create decision regions pdr.plot_decision_regions(X, y, classifier = model) plt.xlabel('sepal length [cm]') plt.ylabel('petal length [cm]') plt.legend(loc = 'upper left') plt.show()
import numpy as np import matplotlib.pyplot as plt from sklearn.svm import SVC 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) 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() svm = SVC(kernel='rbf', random_state=0, gamma=0.10, C=10.0) svm.fit(X_xor, y_xor) from pdr import plot_decision_regions plot_decision_regions(X=X_xor, y=y_xor, classifier=svm) plt.legend(loc='upper left') plt.show()
# Train the model model1.fit(X_std, y) # Plot the training error plt.plot(range(1, len(model1.cost_) + 1), model1.cost_, marker='o', color='red') plt.xlabel('Epochs') plt.ylabel('Sum-squared-error') plt.show() # Plot the decision boundary pdr.plot_decision_regions(X_std, y, classifier=model1) plt.title('Adaline - Gradient Descent') plt.xlabel('sepal length [standardized') plt.ylabel('petal length [standardized]') plt.legend(loc='upper left') plt.show() # Create the AdalineSGD model model2 = AdalineSGD(n_iter=15, eta=0.01, random_state=1) # Train the model model2.fit(X_std, y) # Plot the training errors of both of the models plt.plot(range(1, len(model2.cost_) + 1),