from sklearn import datasets from sklearn import linear_model import numpy as np import matplotlib.pyplot as plt def sigmoid(x): return 1 / (1 + np.exp(-x)) # Load dataset and pick up the needed iris = datasets.load_iris() X = iris.data[:, :2] Y = iris.target B = iris.feature_names[:2] # Initialize the classifier logreg = linear_model.LogisticRegression(C=1e5) logreg.fit(X, Y) coef = logreg.coef_ intercept = logreg.intercept_ # Matrix build res = .05 x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5 y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5 xx, yy = np.meshgrid(np.arange(x_min, x_max, res), np.arange(y_min, y_max, res)) Z = logreg.predict(np.c_[xx.ravel(), yy.ravel()]) print(logreg.score(X, Y)) Z = Z.reshape(xx.shape) # Visualization plt.figure(1, figsize=(8, 6)) plt.xlim(x_min, x_max) plt.ylim(y_min, y_max) plt.xlabel(B[0]) plt.ylabel(B[1]) plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Set1) plt.scatter(X[:, 0], X[:, 1], c=Y, edgecolors='k', cmap=plt.cm.Set1) plt.show()
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn import linear_model from mpl_toolkits.mplot3d import Axes3D def sigmoid(x): return 1 / (1 + np.exp(-x)) def linear_com(data, c, i): return data[0] * c[0] + data[1] * c[1] + i # Load dataset and pick up the needed iris = datasets.load_iris() X = iris.data[:, :2] Y = iris.target B = iris.feature_names[:2] # Initialize the classifier logreg = linear_model.LogisticRegression(C=1e5) logreg.fit(X, Y) coef = logreg.coef_ intercept = logreg.intercept_ # Matrix build res = .1 x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5 y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5 xx, yy = np.meshgrid(np.arange(x_min, x_max, res), np.arange(y_min, y_max, res)) xi = xx.ravel() yi = yy.ravel() tot = len(xi) val = [] for i in range(3): tmp = [] for p in range(tot): tmp.append(sigmoid(linear_com([xi[p], yi[p]], coef[i], intercept[i]))) tmp = np.array(tmp) tmp = tmp.reshape(xx.shape) val.append(tmp) val = np.array(val) # Visualization fig = plt.figure(1, figsize=(15, 4)) for i in range(3): ax = fig.add_subplot(1, 3, i + 1, projection='3d') ax.plot_surface(xx, yy, val[i], cmap=plt.cm.coolwarm, linewidth=0, antialiased=False) ax.set_xlabel(B[0]) ax.set_ylabel(B[1]) ax.set_zlabel('possibility of type ' + str(i)) plt.show()
import numpy as np import matplotlib.pyplot as plt from sklearn import linear_model def sigmoid(x): return 1 / (1 + np.exp(-x)) # Synthesize the normal distributed data with Gaussian noise n_samples = 30 np.random.seed(0) X = np.random.normal(size=n_samples) X[X > 0] *= 4 X += .3 * np.random.normal(size=n_samples) ''' The author put the classification before noise added. What's the difference and why? ''' Y = (X > 0).astype(np.float) X_test = np.linspace(-5, 10, 300) # Initialize the classifier X = X[:, np.newaxis] logreg = linear_model.LogisticRegression(C=1e5) logreg.fit(X, Y) # Visualization plt.figure(1, figsize=(8, 6)) plt.ylabel('Class') plt.xlabel('X') plt.xticks(range(-5, 10)) plt.yticks([0, 0.5, 1]) plt.ylim(-.25, 1.25) plt.xlim(-4, 10) plt.scatter(X.ravel(), Y, c='k') coef = logreg.coef_.ravel()[0] intercept = logreg.intercept_.ravel()[0] plt.plot(X_test, sigmoid(coef * X_test + intercept), label='Regression Curve') plt.axhline(.5, c='.5', linestyle='--', label='Decision Boundary') font = { 'family' : 'serif', 'color' : 'k', 'weight' : 'normal', 'size' : 12 } plt.text(5, 0.25, 'Reg(x) = Sigmoid(%.2f * x + %.2f)'%(coef, intercept), horizontalalignment='center', fontdict=font) plt.legend() plt.show()