import pylab as pl from scikits.learn import datasets from scikits.learn.pca import PCA from scikits.learn.lda import LDA iris = datasets.load_iris() X = iris.data y = iris.target target_names = iris.target_names pca = PCA(n_components=2) X_r = pca.fit(X).transform(X) lda = LDA(n_components=2) X_r2 = lda.fit(X, y).transform(X) # Percentage of variance explained for each components print 'explained variance ratio (first two components):', \ pca.explained_variance_ratio_ pl.figure() pl.subplot(2, 1, 1) for c, i, target_name in zip("rgb", [0, 1, 2], target_names): pl.scatter(X_r[y == i, 0], X_r[y == i, 1], c=c, label=target_name) pl.legend() pl.title('PCA of IRIS dataset') pl.subplot(2, 1, 2) for c, i, target_name in zip("rgb", [0, 1, 2], target_names):
################################################################################ # load sample dataset from scikits.learn.datasets import load_iris iris = load_iris() X = iris.data[:, :2] # Take only 2 dimensions y = iris.target X = X[y > 0] y = y[y > 0] y -= 1 target_names = iris.target_names[1:] ################################################################################ # LDA lda = LDA() y_pred = lda.fit(X, y, store_covariance=True).predict(X) # QDA qda = QDA() y_pred = qda.fit(X, y, store_covariances=True).predict(X) ############################################################################### # Plot results def plot_ellipse(splot, mean, cov, color): v, w = linalg.eigh(cov) u = w[0] / linalg.norm(w[0]) angle = np.arctan(u[1] / u[0]) angle = 180 * angle / np.pi # convert to degrees