def main(): iris = load_iris() # we only take the first two features. We could avoid this ugly # slicing by using a two-dim dataset X = iris.data[:, :2] y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4) model = KNeighborsClassifier(n_neighbors=5) model.fit(X_train, y_train) y_pred = model.predict(X_test) accuracy = calculate_accuracy_score(y_test, y_pred) print("Accuracy Score: {:.2%}".format(accuracy)) h = .05 # step size in the mesh cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF']) cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF']) x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) Z = model.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.pcolormesh(xx, yy, Z, cmap=cmap_light) plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold, edgecolor='k', s=20) plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max()) plt.savefig('./examples/example_KNeighborsClassifier.png')
def main(): iris = load_iris() X = iris.data y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4) model = XGBoost(n_estimators=300, learning_rate=0.001) model.fit(X_train, y_train) y_pred = model.predict(X_test) accuracy = calculate_accuracy_score(y_test, y_pred) print("Accuracy Score: {:.2%}".format(accuracy))
def main(): iris = load_iris() X = iris.data y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4) clf = GradientBoostingClassifier(n_estimators=200, learning_rate=.5) clf.fit(X_train, y_train) y_pred = clf.predict(X_test) accuracy = calculate_accuracy_score(y_test, y_pred) print("Accuracy Score: {:.2%}".format(accuracy))
def main(): iris = load_iris() X = iris.data y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4) model = RandomForestClassifier(n_estimators=200, max_depth=10) model.fit(X_train, y_train) y_pred = model.predict(X_test) accuracy = calculate_accuracy_score(y_test, y_pred) print("Accuracy Score: {:.2%}".format(accuracy))
def main(): iris = load_iris() X = iris.data y = iris.target target_names = iris.target_names lda = LinearDiscriminantAnalysis(n_components=2) pca = PCA(n_components=2) X_r = pca.fit(X).transform(X) X_r2 = lda.fit(X, y).transform(X) # plot plt.figure(figsize=(15, 6)) # pca, left plt.subplot(121) colors = ['navy', 'turquoise', 'darkorange'] lw = 2 for color, i, target_name in zip(colors, [0, 1, 2], target_names): plt.scatter(X_r[y == i, 0], X_r[y == i, 1], color=color, alpha=.8, lw=lw, label=target_name) plt.legend(loc='best', shadow=False, scatterpoints=1) plt.title('PCA of IRIS dataset') # lda, right plt.subplot(122) for color, i, target_name in zip(colors, [0, 1, 2], target_names): plt.scatter(X_r2[y == i, 0], X_r2[y == i, 1], alpha=.8, color=color, label=target_name) plt.legend(loc='best', shadow=False, scatterpoints=1) plt.title('LDA of IRIS dataset') plt.savefig('./examples/example_PCA_LDA.png')
def main(): # Example 1 X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]) Y = np.array([1, 1, 1, 2, 2, 2]) model = GaussianNB() model.fit(X, Y) print(model.predict([[-0.8, -1]])) # Example 2 iris = load_iris() X = normalize(iris.data) y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4) model = GaussianNB() model.fit(X_train, y_train) y_pred = model.predict(X_test) accuracy = calculate_accuracy_score(y_test, y_pred) print("Accuracy Score: {:.2%}".format(accuracy))
def main(): iris = load_iris() X = iris["data"][:, 3:] # petal width y = (iris["target"] == 2).astype(np.int) # 1 if Iris-Virginica, else 0 model = LogisticRegression(max_iter=1000, learning_rate=0.001, fit_intercept=True) model.fit(X, y) y_pred = model.predict(X) accuracy = calculate_accuracy_score(y, y_pred) print("Accuracy Score: {:.2%}".format(accuracy)) X_new = np.linspace(0, 3, 1000).reshape(-1, 1) y_proba = model.predict_proba(X_new) plt.plot(X_new, y_proba[:, 0], "m--", label="Not Iris-Virginica") plt.plot(X_new, y_proba[:, 1], "c-", label="Iris-Virginica") plt.plot(X[y == 0], y[y == 0], "m.") plt.plot(X[y == 1], y[y == 1], "c.") plt.legend() plt.savefig("./examples/example_LogisticRegression.png")
def main(): # Example 1 X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]]) kmeans = KMeans(n_clusters=2) kmeans.fit(X) print(kmeans.cluster_centers_) print(kmeans.labels_) print(kmeans.inertia_) print(kmeans.predict([[0, 0], [12, 3]])) # Example 2 iris = load_iris() X = iris.data y = iris.target kmeans = KMeans(n_clusters=3) kmeans.fit(X) labels = kmeans.labels_ fig = plt.figure(figsize=(12, 5)) ax = fig.add_subplot(1, 2, 1, projection='3d', elev=48, azim=134) for name, label in [('Setosa', 0), ('Versicolour', 1), ('Virginica', 2)]: ax.text3D(X[y == label, 3].mean(), X[y == label, 0].mean(), X[y == label, 2].mean() + 2, name, horizontalalignment='center', bbox=dict(alpha=.2, edgecolor='w', facecolor='w')) ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=labels, edgecolor='k') ax.w_xaxis.set_ticklabels([]) ax.w_yaxis.set_ticklabels([]) ax.w_zaxis.set_ticklabels([]) ax.set_xlabel('Petal width') ax.set_ylabel('Sepal length') ax.set_zlabel('Petal length') ax.set_title('3 clusters') ax.dist = 12 ax = fig.add_subplot(1, 2, 2, projection='3d', elev=48, azim=134) for name, label in [('Setosa', 0), ('Versicolour', 1), ('Virginica', 2)]: ax.text3D(X[y == label, 3].mean(), X[y == label, 0].mean(), X[y == label, 2].mean() + 2, name, horizontalalignment='center', bbox=dict(alpha=.2, edgecolor='w', facecolor='w')) y = np.choose(y, [1, 2, 0]).astype(np.float) ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=y, edgecolor='k') ax.w_xaxis.set_ticklabels([]) ax.w_yaxis.set_ticklabels([]) ax.w_zaxis.set_ticklabels([]) ax.set_xlabel('Petal width') ax.set_ylabel('Sepal length') ax.set_zlabel('Petal length') ax.set_title('Ground Truth') ax.dist = 12 plt.savefig("./examples/example_KMeans.png")