diff --git a/doc/pub/DimRed/html/._DimRed-bs000.html b/doc/pub/DimRed/html/._DimRed-bs000.html index 84af171c4..706f14ce8 100644 --- a/doc/pub/DimRed/html/._DimRed-bs000.html +++ b/doc/pub/DimRed/html/._DimRed-bs000.html @@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source None, '___sec2'), ('Simple preprocessing examples, breast cancer data and ' - 'classification', + 'classification, Support Vector Machines', 2, None, '___sec3'), - ('Principal Component Analysis', 2, None, '___sec4'), - ('PCA and scikit-learn', 2, None, '___sec5'), - ('More on the PCA', 2, None, '___sec6'), - ('Incremental PCA', 2, None, '___sec7'), - ('Randomized PCA', 2, None, '___sec8'), - ('Kernel PCA', 2, None, '___sec9'), - ('LLE', 2, None, '___sec10'), - ('Other techniques', 2, None, '___sec11')]} + ('More on Cancer Data, now with Logistic Regression', + 2, + None, + '___sec4'), + ('Principal Component Analysis', 2, None, '___sec5'), + ('PCA and scikit-learn', 2, None, '___sec6'), + ('More on the PCA', 2, None, '___sec7'), + ('Incremental PCA', 2, None, '___sec8'), + ('Randomized PCA', 2, None, '___sec9'), + ('Kernel PCA', 2, None, '___sec10'), + ('LLE', 2, None, '___sec11'), + ('Other techniques', 2, None, '___sec12')]} end of tocinfo --> @@ -103,15 +107,16 @@ MathJax.Hub.Config({
  • Reducing the number of degrees of freedom, overarching view
  • Preprocessing our data
  • Simple preprocessing examples, Franke function and regression
  • -
  • Simple preprocessing examples, breast cancer data and classification
  • -
  • Principal Component Analysis
  • -
  • PCA and scikit-learn
  • -
  • More on the PCA
  • -
  • Incremental PCA
  • -
  • Randomized PCA
  • -
  • Kernel PCA
  • -
  • LLE
  • -
  • Other techniques
  • +
  • Simple preprocessing examples, breast cancer data and classification, Support Vector Machines
  • +
  • More on Cancer Data, now with Logistic Regression
  • +
  • Principal Component Analysis
  • +
  • PCA and scikit-learn
  • +
  • More on the PCA
  • +
  • Incremental PCA
  • +
  • Randomized PCA
  • +
  • Kernel PCA
  • +
  • LLE
  • +
  • Other techniques
  • @@ -170,7 +175,7 @@ MathJax.Hub.Config({
  • 9
  • 10
  • ...
  • -
  • 13
  • +
  • 14
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs001.html b/doc/pub/DimRed/html/._DimRed-bs001.html index 8027c7441..c4c517c4b 100644 --- a/doc/pub/DimRed/html/._DimRed-bs001.html +++ b/doc/pub/DimRed/html/._DimRed-bs001.html @@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source None, '___sec2'), ('Simple preprocessing examples, breast cancer data and ' - 'classification', + 'classification, Support Vector Machines', 2, None, '___sec3'), - ('Principal Component Analysis', 2, None, '___sec4'), - ('PCA and scikit-learn', 2, None, '___sec5'), - ('More on the PCA', 2, None, '___sec6'), - ('Incremental PCA', 2, None, '___sec7'), - ('Randomized PCA', 2, None, '___sec8'), - ('Kernel PCA', 2, None, '___sec9'), - ('LLE', 2, None, '___sec10'), - ('Other techniques', 2, None, '___sec11')]} + ('More on Cancer Data, now with Logistic Regression', + 2, + None, + '___sec4'), + ('Principal Component Analysis', 2, None, '___sec5'), + ('PCA and scikit-learn', 2, None, '___sec6'), + ('More on the PCA', 2, None, '___sec7'), + ('Incremental PCA', 2, None, '___sec8'), + ('Randomized PCA', 2, None, '___sec9'), + ('Kernel PCA', 2, None, '___sec10'), + ('LLE', 2, None, '___sec11'), + ('Other techniques', 2, None, '___sec12')]} end of tocinfo --> @@ -103,15 +107,16 @@ MathJax.Hub.Config({
  • Reducing the number of degrees of freedom, overarching view
  • Preprocessing our data
  • Simple preprocessing examples, Franke function and regression
  • -
  • Simple preprocessing examples, breast cancer data and classification
  • -
  • Principal Component Analysis
  • -
  • PCA and scikit-learn
  • -
  • More on the PCA
  • -
  • Incremental PCA
  • -
  • Randomized PCA
  • -
  • Kernel PCA
  • -
  • LLE
  • -
  • Other techniques
  • +
  • Simple preprocessing examples, breast cancer data and classification, Support Vector Machines
  • +
  • More on Cancer Data, now with Logistic Regression
  • +
  • Principal Component Analysis
  • +
  • PCA and scikit-learn
  • +
  • More on the PCA
  • +
  • Incremental PCA
  • +
  • Randomized PCA
  • +
  • Kernel PCA
  • +
  • LLE
  • +
  • Other techniques
  • @@ -165,7 +170,7 @@ reduction techniques: the principal component analysis PCA, Kernel PCA, and Loca
  • 10
  • 11
  • ...
  • -
  • 13
  • +
  • 14
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs002.html b/doc/pub/DimRed/html/._DimRed-bs002.html index 9b0341c36..0b9829477 100644 --- a/doc/pub/DimRed/html/._DimRed-bs002.html +++ b/doc/pub/DimRed/html/._DimRed-bs002.html @@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source None, '___sec2'), ('Simple preprocessing examples, breast cancer data and ' - 'classification', + 'classification, Support Vector Machines', 2, None, '___sec3'), - ('Principal Component Analysis', 2, None, '___sec4'), - ('PCA and scikit-learn', 2, None, '___sec5'), - ('More on the PCA', 2, None, '___sec6'), - ('Incremental PCA', 2, None, '___sec7'), - ('Randomized PCA', 2, None, '___sec8'), - ('Kernel PCA', 2, None, '___sec9'), - ('LLE', 2, None, '___sec10'), - ('Other techniques', 2, None, '___sec11')]} + ('More on Cancer Data, now with Logistic Regression', + 2, + None, + '___sec4'), + ('Principal Component Analysis', 2, None, '___sec5'), + ('PCA and scikit-learn', 2, None, '___sec6'), + ('More on the PCA', 2, None, '___sec7'), + ('Incremental PCA', 2, None, '___sec8'), + ('Randomized PCA', 2, None, '___sec9'), + ('Kernel PCA', 2, None, '___sec10'), + ('LLE', 2, None, '___sec11'), + ('Other techniques', 2, None, '___sec12')]} end of tocinfo --> @@ -103,15 +107,16 @@ MathJax.Hub.Config({
  • Reducing the number of degrees of freedom, overarching view
  • Preprocessing our data
  • Simple preprocessing examples, Franke function and regression
  • -
  • Simple preprocessing examples, breast cancer data and classification
  • -
  • Principal Component Analysis
  • -
  • PCA and scikit-learn
  • -
  • More on the PCA
  • -
  • Incremental PCA
  • -
  • Randomized PCA
  • -
  • Kernel PCA
  • -
  • LLE
  • -
  • Other techniques
  • +
  • Simple preprocessing examples, breast cancer data and classification, Support Vector Machines
  • +
  • More on Cancer Data, now with Logistic Regression
  • +
  • Principal Component Analysis
  • +
  • PCA and scikit-learn
  • +
  • More on the PCA
  • +
  • Incremental PCA
  • +
  • Randomized PCA
  • +
  • Kernel PCA
  • +
  • LLE
  • +
  • Other techniques
  • @@ -167,7 +172,7 @@ This scaling has the drawback that it does not ensure that we have a particular
  • 11
  • 12
  • ...
  • -
  • 13
  • +
  • 14
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs003.html b/doc/pub/DimRed/html/._DimRed-bs003.html index b57689b38..6b99934de 100644 --- a/doc/pub/DimRed/html/._DimRed-bs003.html +++ b/doc/pub/DimRed/html/._DimRed-bs003.html @@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source None, '___sec2'), ('Simple preprocessing examples, breast cancer data and ' - 'classification', + 'classification, Support Vector Machines', 2, None, '___sec3'), - ('Principal Component Analysis', 2, None, '___sec4'), - ('PCA and scikit-learn', 2, None, '___sec5'), - ('More on the PCA', 2, None, '___sec6'), - ('Incremental PCA', 2, None, '___sec7'), - ('Randomized PCA', 2, None, '___sec8'), - ('Kernel PCA', 2, None, '___sec9'), - ('LLE', 2, None, '___sec10'), - ('Other techniques', 2, None, '___sec11')]} + ('More on Cancer Data, now with Logistic Regression', + 2, + None, + '___sec4'), + ('Principal Component Analysis', 2, None, '___sec5'), + ('PCA and scikit-learn', 2, None, '___sec6'), + ('More on the PCA', 2, None, '___sec7'), + ('Incremental PCA', 2, None, '___sec8'), + ('Randomized PCA', 2, None, '___sec9'), + ('Kernel PCA', 2, None, '___sec10'), + ('LLE', 2, None, '___sec11'), + ('Other techniques', 2, None, '___sec12')]} end of tocinfo --> @@ -103,15 +107,16 @@ MathJax.Hub.Config({
  • Reducing the number of degrees of freedom, overarching view
  • Preprocessing our data
  • Simple preprocessing examples, Franke function and regression
  • -
  • Simple preprocessing examples, breast cancer data and classification
  • -
  • Principal Component Analysis
  • -
  • PCA and scikit-learn
  • -
  • More on the PCA
  • -
  • Incremental PCA
  • -
  • Randomized PCA
  • -
  • Kernel PCA
  • -
  • LLE
  • -
  • Other techniques
  • +
  • Simple preprocessing examples, breast cancer data and classification, Support Vector Machines
  • +
  • More on Cancer Data, now with Logistic Regression
  • +
  • Principal Component Analysis
  • +
  • PCA and scikit-learn
  • +
  • More on the PCA
  • +
  • Incremental PCA
  • +
  • Randomized PCA
  • +
  • Kernel PCA
  • +
  • LLE
  • +
  • Other techniques
  • @@ -245,6 +250,8 @@ svm.fit(X_train_scaled, y_train)
  • 11
  • 12
  • 13
  • +
  • ...
  • +
  • 14
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs004.html b/doc/pub/DimRed/html/._DimRed-bs004.html index e7bf415b7..80787b95d 100644 --- a/doc/pub/DimRed/html/._DimRed-bs004.html +++ b/doc/pub/DimRed/html/._DimRed-bs004.html @@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source None, '___sec2'), ('Simple preprocessing examples, breast cancer data and ' - 'classification', + 'classification, Support Vector Machines', 2, None, '___sec3'), - ('Principal Component Analysis', 2, None, '___sec4'), - ('PCA and scikit-learn', 2, None, '___sec5'), - ('More on the PCA', 2, None, '___sec6'), - ('Incremental PCA', 2, None, '___sec7'), - ('Randomized PCA', 2, None, '___sec8'), - ('Kernel PCA', 2, None, '___sec9'), - ('LLE', 2, None, '___sec10'), - ('Other techniques', 2, None, '___sec11')]} + ('More on Cancer Data, now with Logistic Regression', + 2, + None, + '___sec4'), + ('Principal Component Analysis', 2, None, '___sec5'), + ('PCA and scikit-learn', 2, None, '___sec6'), + ('More on the PCA', 2, None, '___sec7'), + ('Incremental PCA', 2, None, '___sec8'), + ('Randomized PCA', 2, None, '___sec9'), + ('Kernel PCA', 2, None, '___sec10'), + ('LLE', 2, None, '___sec11'), + ('Other techniques', 2, None, '___sec12')]} end of tocinfo --> @@ -103,15 +107,16 @@ MathJax.Hub.Config({
  • Reducing the number of degrees of freedom, overarching view
  • Preprocessing our data
  • Simple preprocessing examples, Franke function and regression
  • -
  • Simple preprocessing examples, breast cancer data and classification
  • -
  • Principal Component Analysis
  • -
  • PCA and scikit-learn
  • -
  • More on the PCA
  • -
  • Incremental PCA
  • -
  • Randomized PCA
  • -
  • Kernel PCA
  • -
  • LLE
  • -
  • Other techniques
  • +
  • Simple preprocessing examples, breast cancer data and classification, Support Vector Machines
  • +
  • More on Cancer Data, now with Logistic Regression
  • +
  • Principal Component Analysis
  • +
  • PCA and scikit-learn
  • +
  • More on the PCA
  • +
  • Incremental PCA
  • +
  • Randomized PCA
  • +
  • Kernel PCA
  • +
  • LLE
  • +
  • Other techniques
  • @@ -127,7 +132,7 @@ MathJax.Hub.Config({ -

    Simple preprocessing examples, breast cancer data and classification

    +

    Simple preprocessing examples, breast cancer data and classification, Support Vector Machines

    We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification @@ -193,6 +198,7 @@ svm.fit(X_train_scaled, y_train)

  • 11
  • 12
  • 13
  • +
  • 14
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs005.html b/doc/pub/DimRed/html/._DimRed-bs005.html index 1ca4766d5..902a33e89 100644 --- a/doc/pub/DimRed/html/._DimRed-bs005.html +++ b/doc/pub/DimRed/html/._DimRed-bs005.html @@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source None, '___sec2'), ('Simple preprocessing examples, breast cancer data and ' - 'classification', + 'classification, Support Vector Machines', 2, None, '___sec3'), - ('Principal Component Analysis', 2, None, '___sec4'), - ('PCA and scikit-learn', 2, None, '___sec5'), - ('More on the PCA', 2, None, '___sec6'), - ('Incremental PCA', 2, None, '___sec7'), - ('Randomized PCA', 2, None, '___sec8'), - ('Kernel PCA', 2, None, '___sec9'), - ('LLE', 2, None, '___sec10'), - ('Other techniques', 2, None, '___sec11')]} + ('More on Cancer Data, now with Logistic Regression', + 2, + None, + '___sec4'), + ('Principal Component Analysis', 2, None, '___sec5'), + ('PCA and scikit-learn', 2, None, '___sec6'), + ('More on the PCA', 2, None, '___sec7'), + ('Incremental PCA', 2, None, '___sec8'), + ('Randomized PCA', 2, None, '___sec9'), + ('Kernel PCA', 2, None, '___sec10'), + ('LLE', 2, None, '___sec11'), + ('Other techniques', 2, None, '___sec12')]} end of tocinfo --> @@ -103,15 +107,16 @@ MathJax.Hub.Config({
  • Reducing the number of degrees of freedom, overarching view
  • Preprocessing our data
  • Simple preprocessing examples, Franke function and regression
  • -
  • Simple preprocessing examples, breast cancer data and classification
  • -
  • Principal Component Analysis
  • -
  • PCA and scikit-learn
  • -
  • More on the PCA
  • -
  • Incremental PCA
  • -
  • Randomized PCA
  • -
  • Kernel PCA
  • -
  • LLE
  • -
  • Other techniques
  • +
  • Simple preprocessing examples, breast cancer data and classification, Support Vector Machines
  • +
  • More on Cancer Data, now with Logistic Regression
  • +
  • Principal Component Analysis
  • +
  • PCA and scikit-learn
  • +
  • More on the PCA
  • +
  • Incremental PCA
  • +
  • Randomized PCA
  • +
  • Kernel PCA
  • +
  • LLE
  • +
  • Other techniques
  • @@ -127,38 +132,57 @@ MathJax.Hub.Config({ -

    Principal Component Analysis

    -
    -
    -

    -Principal Component Analysis (PCA) is by far the most popular dimensionality reduction algorithm. -First it identifies the hyperplane that lies closest to the data, and then it projects the data onto it. - -

    -The following Python code uses NumPy’s svd() function to obtain all the principal components of the -training set, then extracts the first two principal components +

    More on Cancer Data, now with Logistic Regression

    -

    X_centered = X - X.mean(axis=0)
    -U, s, V = np.linalg.svd(X_centered)
    -c1 = V.T[:, 0]
    -c2 = V.T[:, 1]
    -
    -

    -PCA assumes that the dataset is centered around the origin. Scikit-Learn’s PCA classes take care of centering -the data for you. However, if you implement PCA yourself (as in the preceding example), or if you use other libraries, don’t -forget to center the data first. +

    import matplotlib.pyplot as plt
    +import numpy as np
    +from sklearn.model_selection import  train_test_split 
    +from sklearn.datasets import load_breast_cancer
    +from sklearn.linear_model import LogisticRegression
    +cancer = load_breast_cancer()
     
    -

    -Once you have identified all the principal components, you can reduce the dimensionality of the dataset -down to \( d \) dimensions by projecting it onto the hyperplane defined by the first \( d \) principal components. -Selecting this hyperplane ensures that the projection will preserve as much variance as possible. -

    +fig, axes = plt.subplots(15,2,figsize=(10,20)) +male = cancer.data[cancer.target == 0] +bene = cancer.data[cancer.target == 1] +ax = axes.ravel() - -

    W2 = V.T[:, :2]
    -X2D = X_centered.dot(W2)
    +for i in range(30):
    +    _, bins = np.histogram(cancer.data[:,i], bins =50)
    +    ax[i].hist(male[:,i], bins = bins, alpha = 0.5)
    +    ax[i].hist(bene[:,i], bins = bins, alpha = 0.5)
    +    ax[i].set_title(cancer.feature_names[i])
    +    ax[i].set_yticks(())
    +ax[0].set_xlabel("Feature magnitude")
    +ax[0].set_ylabel("Frequency")
    +ax[0].legend(["Male", "Bene"], loc ="best")
    +fig.tight_layout()
    +plt.show()
    +
    +# Set up training data
    +X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
    +# Perform Logistic Regression (replace with own code) 
    +logreg = LogisticRegression()
    +logreg.fit(X_train, y_train)
    +print("Test set accuracy: {:.2f}".format(logreg.score(X_test,y_test)))
    +
    +# Scale data
    +from sklearn.preprocessing import StandardScaler
    +scaler = StandardScaler()
    +scaler.fit(X_train)
    +X_train_scaled = scaler.transform(X_train)
    +X_test_scaled = scaler.transform(X_test)
    +logreg.fit(X_train_scaled, y_train)
    +#svm.fit(X_train_scaled, y_train)
    +print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
    +
    +# Now add PCA
    +from sklearn.decomposition import PCA
    +pca = PCA(n_components = 2)
    +pca.fit(X_train_scaled)
    +
    +X_pca = pca.transform(X_train_scaled)
     

    @@ -178,6 +202,7 @@ X2D = X_centered11

  • 12
  • 13
  • +
  • 14
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs006.html b/doc/pub/DimRed/html/._DimRed-bs006.html index 76dff32c4..788009051 100644 --- a/doc/pub/DimRed/html/._DimRed-bs006.html +++ b/doc/pub/DimRed/html/._DimRed-bs006.html @@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source None, '___sec2'), ('Simple preprocessing examples, breast cancer data and ' - 'classification', + 'classification, Support Vector Machines', 2, None, '___sec3'), - ('Principal Component Analysis', 2, None, '___sec4'), - ('PCA and scikit-learn', 2, None, '___sec5'), - ('More on the PCA', 2, None, '___sec6'), - ('Incremental PCA', 2, None, '___sec7'), - ('Randomized PCA', 2, None, '___sec8'), - ('Kernel PCA', 2, None, '___sec9'), - ('LLE', 2, None, '___sec10'), - ('Other techniques', 2, None, '___sec11')]} + ('More on Cancer Data, now with Logistic Regression', + 2, + None, + '___sec4'), + ('Principal Component Analysis', 2, None, '___sec5'), + ('PCA and scikit-learn', 2, None, '___sec6'), + ('More on the PCA', 2, None, '___sec7'), + ('Incremental PCA', 2, None, '___sec8'), + ('Randomized PCA', 2, None, '___sec9'), + ('Kernel PCA', 2, None, '___sec10'), + ('LLE', 2, None, '___sec11'), + ('Other techniques', 2, None, '___sec12')]} end of tocinfo --> @@ -103,15 +107,16 @@ MathJax.Hub.Config({
  • Reducing the number of degrees of freedom, overarching view
  • Preprocessing our data
  • Simple preprocessing examples, Franke function and regression
  • -
  • Simple preprocessing examples, breast cancer data and classification
  • -
  • Principal Component Analysis
  • -
  • PCA and scikit-learn
  • -
  • More on the PCA
  • -
  • Incremental PCA
  • -
  • Randomized PCA
  • -
  • Kernel PCA
  • -
  • LLE
  • -
  • Other techniques
  • +
  • Simple preprocessing examples, breast cancer data and classification, Support Vector Machines
  • +
  • More on Cancer Data, now with Logistic Regression
  • +
  • Principal Component Analysis
  • +
  • PCA and scikit-learn
  • +
  • More on the PCA
  • +
  • Incremental PCA
  • +
  • Randomized PCA
  • +
  • Kernel PCA
  • +
  • LLE
  • +
  • Other techniques
  • @@ -125,36 +130,41 @@ MathJax.Hub.Config({

     

     

     

    - + -

    PCA and scikit-learn

    +

    Principal Component Analysis

    +
    +
    +

    +Principal Component Analysis (PCA) is by far the most popular dimensionality reduction algorithm. +First it identifies the hyperplane that lies closest to the data, and then it projects the data onto it.

    -Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The -following code applies PCA to reduce the dimensionality of the dataset down to two dimensions (note -that it automatically takes care of centering the data): +The following Python code uses NumPy’s svd() function to obtain all the principal components of the +training set, then extracts the first two principal components

    -

    from sklearn.decomposition import PCA
    -pca = PCA(n_components = 2)
    -X2D = pca.fit_transform(X)
    +
    X_centered = X - X.mean(axis=0)
    +U, s, V = np.linalg.svd(X_centered)
    +c1 = V.T[:, 0]
    +c2 = V.T[:, 1]
     

    -After fitting the PCA transformer to the dataset, you can access the principal components using the -components variable (note that it contains the PCs as horizontal vectors, so, for example, the first -principal component is equal to +PCA assumes that the dataset is centered around the origin. Scikit-Learn’s PCA classes take care of centering +the data for you. However, if you implement PCA yourself (as in the preceding example), or if you use other libraries, don’t +forget to center the data first. + +

    +Once you have identified all the principal components, you can reduce the dimensionality of the dataset +down to \( d \) dimensions by projecting it onto the hyperplane defined by the first \( d \) principal components. +Selecting this hyperplane ensures that the projection will preserve as much variance as possible.

    -

    pca.components_.T[:, 0]).
    +
    W2 = V.T[:, :2]
    +X2D = X_centered.dot(W2)
     
    -

    -Another very useful piece of information is the explained variance ratio of each principal component, -available via the \( explained\_variance\_ratio \) variable. It indicates the proportion of the dataset’s -variance that lies along the axis of each principal component. -More material to come here. -

    @@ -173,6 +183,7 @@ More material to come here.

  • 11
  • 12
  • 13
  • +
  • 14
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs007.html b/doc/pub/DimRed/html/._DimRed-bs007.html index b93647590..8f57f5294 100644 --- a/doc/pub/DimRed/html/._DimRed-bs007.html +++ b/doc/pub/DimRed/html/._DimRed-bs007.html @@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source None, '___sec2'), ('Simple preprocessing examples, breast cancer data and ' - 'classification', + 'classification, Support Vector Machines', 2, None, '___sec3'), - ('Principal Component Analysis', 2, None, '___sec4'), - ('PCA and scikit-learn', 2, None, '___sec5'), - ('More on the PCA', 2, None, '___sec6'), - ('Incremental PCA', 2, None, '___sec7'), - ('Randomized PCA', 2, None, '___sec8'), - ('Kernel PCA', 2, None, '___sec9'), - ('LLE', 2, None, '___sec10'), - ('Other techniques', 2, None, '___sec11')]} + ('More on Cancer Data, now with Logistic Regression', + 2, + None, + '___sec4'), + ('Principal Component Analysis', 2, None, '___sec5'), + ('PCA and scikit-learn', 2, None, '___sec6'), + ('More on the PCA', 2, None, '___sec7'), + ('Incremental PCA', 2, None, '___sec8'), + ('Randomized PCA', 2, None, '___sec9'), + ('Kernel PCA', 2, None, '___sec10'), + ('LLE', 2, None, '___sec11'), + ('Other techniques', 2, None, '___sec12')]} end of tocinfo --> @@ -103,15 +107,16 @@ MathJax.Hub.Config({
  • Reducing the number of degrees of freedom, overarching view
  • Preprocessing our data
  • Simple preprocessing examples, Franke function and regression
  • -
  • Simple preprocessing examples, breast cancer data and classification
  • -
  • Principal Component Analysis
  • -
  • PCA and scikit-learn
  • -
  • More on the PCA
  • -
  • Incremental PCA
  • -
  • Randomized PCA
  • -
  • Kernel PCA
  • -
  • LLE
  • -
  • Other techniques
  • +
  • Simple preprocessing examples, breast cancer data and classification, Support Vector Machines
  • +
  • More on Cancer Data, now with Logistic Regression
  • +
  • Principal Component Analysis
  • +
  • PCA and scikit-learn
  • +
  • More on the PCA
  • +
  • Incremental PCA
  • +
  • Randomized PCA
  • +
  • Kernel PCA
  • +
  • LLE
  • +
  • Other techniques
  • @@ -125,33 +130,36 @@ MathJax.Hub.Config({

     

     

     

    - + -

    More on the PCA

    -Instead of arbitrarily choosing the number of dimensions to reduce down to, it is generally preferable to -choose the number of dimensions that add up to a sufficiently large portion of the variance (e.g., 95%). -Unless, of course, you are reducing dimensionality for data visualization — in that case you will -generally want to reduce the dimensionality down to 2 or 3. -The following code computes PCA without reducing dimensionality, then computes the minimum number -of dimensions required to preserve 95% of the training set’s variance: +

    PCA and scikit-learn

    + +

    +Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The +following code applies PCA to reduce the dimensionality of the dataset down to two dimensions (note +that it automatically takes care of centering the data):

    -

    pca = PCA()
    -pca.fit(X)
    -cumsum = np.cumsum(pca.explained_variance_ratio_)
    -d = np.argmax(cumsum >= 0.95) + 1
    +
    from sklearn.decomposition import PCA
    +pca = PCA(n_components = 2)
    +X2D = pca.fit_transform(X)
     

    -You could then set \( n\_components=d \) and run PCA again. However, there is a much better option: instead -of specifying the number of principal components you want to preserve, you can set \( n\_components \) to be -a float between 0.0 and 1.0, indicating the ratio of variance you wish to preserve: +After fitting the PCA transformer to the dataset, you can access the principal components using the +components variable (note that it contains the PCs as horizontal vectors, so, for example, the first +principal component is equal to

    -

    pca = PCA(n_components=0.95)
    -X_reduced = pca.fit_transform(X)
    +
    pca.components_.T[:, 0]).
     
    +

    +Another very useful piece of information is the explained variance ratio of each principal component, +available via the \( explained\_variance\_ratio \) variable. It indicates the proportion of the dataset’s +variance that lies along the axis of each principal component. +More material to come here. +

    @@ -170,6 +178,7 @@ X_reduced = pca

  • 11
  • 12
  • 13
  • +
  • 14
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs008.html b/doc/pub/DimRed/html/._DimRed-bs008.html index 9c9289160..2770b2efb 100644 --- a/doc/pub/DimRed/html/._DimRed-bs008.html +++ b/doc/pub/DimRed/html/._DimRed-bs008.html @@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source None, '___sec2'), ('Simple preprocessing examples, breast cancer data and ' - 'classification', + 'classification, Support Vector Machines', 2, None, '___sec3'), - ('Principal Component Analysis', 2, None, '___sec4'), - ('PCA and scikit-learn', 2, None, '___sec5'), - ('More on the PCA', 2, None, '___sec6'), - ('Incremental PCA', 2, None, '___sec7'), - ('Randomized PCA', 2, None, '___sec8'), - ('Kernel PCA', 2, None, '___sec9'), - ('LLE', 2, None, '___sec10'), - ('Other techniques', 2, None, '___sec11')]} + ('More on Cancer Data, now with Logistic Regression', + 2, + None, + '___sec4'), + ('Principal Component Analysis', 2, None, '___sec5'), + ('PCA and scikit-learn', 2, None, '___sec6'), + ('More on the PCA', 2, None, '___sec7'), + ('Incremental PCA', 2, None, '___sec8'), + ('Randomized PCA', 2, None, '___sec9'), + ('Kernel PCA', 2, None, '___sec10'), + ('LLE', 2, None, '___sec11'), + ('Other techniques', 2, None, '___sec12')]} end of tocinfo --> @@ -103,15 +107,16 @@ MathJax.Hub.Config({
  • Reducing the number of degrees of freedom, overarching view
  • Preprocessing our data
  • Simple preprocessing examples, Franke function and regression
  • -
  • Simple preprocessing examples, breast cancer data and classification
  • -
  • Principal Component Analysis
  • -
  • PCA and scikit-learn
  • -
  • More on the PCA
  • -
  • Incremental PCA
  • -
  • Randomized PCA
  • -
  • Kernel PCA
  • -
  • LLE
  • -
  • Other techniques
  • +
  • Simple preprocessing examples, breast cancer data and classification, Support Vector Machines
  • +
  • More on Cancer Data, now with Logistic Regression
  • +
  • Principal Component Analysis
  • +
  • PCA and scikit-learn
  • +
  • More on the PCA
  • +
  • Incremental PCA
  • +
  • Randomized PCA
  • +
  • Kernel PCA
  • +
  • LLE
  • +
  • Other techniques
  • @@ -127,13 +132,31 @@ MathJax.Hub.Config({ -

    Incremental PCA

    -One problem with the preceding implementation of PCA is that it requires the whole training set to fit in -memory in order for the SVD algorithm to run. Fortunately, Incremental PCA (IPCA) algorithms have -been developed: you can split the training set into mini-batches and feed an IPCA algorithm one minibatch -at a time. This is useful for large training sets, and also to apply PCA online (i.e., on the fly, as new -instances arrive). +

    More on the PCA

    +Instead of arbitrarily choosing the number of dimensions to reduce down to, it is generally preferable to +choose the number of dimensions that add up to a sufficiently large portion of the variance (e.g., 95%). +Unless, of course, you are reducing dimensionality for data visualization — in that case you will +generally want to reduce the dimensionality down to 2 or 3. +The following code computes PCA without reducing dimensionality, then computes the minimum number +of dimensions required to preserve 95% of the training set’s variance: +

    + +

    pca = PCA()
    +pca.fit(X)
    +cumsum = np.cumsum(pca.explained_variance_ratio_)
    +d = np.argmax(cumsum >= 0.95) + 1
    +
    +

    +You could then set \( n\_components=d \) and run PCA again. However, there is a much better option: instead +of specifying the number of principal components you want to preserve, you can set \( n\_components \) to be +a float between 0.0 and 1.0, indicating the ratio of variance you wish to preserve: +

    + + +

    pca = PCA(n_components=0.95)
    +X_reduced = pca.fit_transform(X)
    +

    @@ -152,6 +175,7 @@ instances arrive).

  • 11
  • 12
  • 13
  • +
  • 14
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs009.html b/doc/pub/DimRed/html/._DimRed-bs009.html index dd7fef739..068bb5444 100644 --- a/doc/pub/DimRed/html/._DimRed-bs009.html +++ b/doc/pub/DimRed/html/._DimRed-bs009.html @@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source None, '___sec2'), ('Simple preprocessing examples, breast cancer data and ' - 'classification', + 'classification, Support Vector Machines', 2, None, '___sec3'), - ('Principal Component Analysis', 2, None, '___sec4'), - ('PCA and scikit-learn', 2, None, '___sec5'), - ('More on the PCA', 2, None, '___sec6'), - ('Incremental PCA', 2, None, '___sec7'), - ('Randomized PCA', 2, None, '___sec8'), - ('Kernel PCA', 2, None, '___sec9'), - ('LLE', 2, None, '___sec10'), - ('Other techniques', 2, None, '___sec11')]} + ('More on Cancer Data, now with Logistic Regression', + 2, + None, + '___sec4'), + ('Principal Component Analysis', 2, None, '___sec5'), + ('PCA and scikit-learn', 2, None, '___sec6'), + ('More on the PCA', 2, None, '___sec7'), + ('Incremental PCA', 2, None, '___sec8'), + ('Randomized PCA', 2, None, '___sec9'), + ('Kernel PCA', 2, None, '___sec10'), + ('LLE', 2, None, '___sec11'), + ('Other techniques', 2, None, '___sec12')]} end of tocinfo --> @@ -103,15 +107,16 @@ MathJax.Hub.Config({
  • Reducing the number of degrees of freedom, overarching view
  • Preprocessing our data
  • Simple preprocessing examples, Franke function and regression
  • -
  • Simple preprocessing examples, breast cancer data and classification
  • -
  • Principal Component Analysis
  • -
  • PCA and scikit-learn
  • -
  • More on the PCA
  • -
  • Incremental PCA
  • -
  • Randomized PCA
  • -
  • Kernel PCA
  • -
  • LLE
  • -
  • Other techniques
  • +
  • Simple preprocessing examples, breast cancer data and classification, Support Vector Machines
  • +
  • More on Cancer Data, now with Logistic Regression
  • +
  • Principal Component Analysis
  • +
  • PCA and scikit-learn
  • +
  • More on the PCA
  • +
  • Incremental PCA
  • +
  • Randomized PCA
  • +
  • Kernel PCA
  • +
  • LLE
  • +
  • Other techniques
  • @@ -127,18 +132,12 @@ MathJax.Hub.Config({ -

    Randomized PCA

    - -

    -Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic -algorithm that quickly finds an approximation of the first d principal components. Its computational -complexity is \( O(m \times d^2)+O(d^3) \), instead of \( O(m \times n^2) + O(n^3) \), so it is dramatically faster than the -previous algorithms when \( d \) is much smaller than \( n \). - -

    -

    -
    - +

    Incremental PCA

    +One problem with the preceding implementation of PCA is that it requires the whole training set to fit in +memory in order for the SVD algorithm to run. Fortunately, Incremental PCA (IPCA) algorithms have +been developed: you can split the training set into mini-batches and feed an IPCA algorithm one minibatch +at a time. This is useful for large training sets, and also to apply PCA online (i.e., on the fly, as new +instances arrive).

    @@ -158,6 +157,7 @@ previous algorithms when \( d \) is much smaller than \( n \).

  • 11
  • 12
  • 13
  • +
  • 14
  • »
  • diff --git a/doc/pub/DimRed/html/DimRed-bs.html b/doc/pub/DimRed/html/DimRed-bs.html index 84af171c4..706f14ce8 100644 --- a/doc/pub/DimRed/html/DimRed-bs.html +++ b/doc/pub/DimRed/html/DimRed-bs.html @@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source None, '___sec2'), ('Simple preprocessing examples, breast cancer data and ' - 'classification', + 'classification, Support Vector Machines', 2, None, '___sec3'), - ('Principal Component Analysis', 2, None, '___sec4'), - ('PCA and scikit-learn', 2, None, '___sec5'), - ('More on the PCA', 2, None, '___sec6'), - ('Incremental PCA', 2, None, '___sec7'), - ('Randomized PCA', 2, None, '___sec8'), - ('Kernel PCA', 2, None, '___sec9'), - ('LLE', 2, None, '___sec10'), - ('Other techniques', 2, None, '___sec11')]} + ('More on Cancer Data, now with Logistic Regression', + 2, + None, + '___sec4'), + ('Principal Component Analysis', 2, None, '___sec5'), + ('PCA and scikit-learn', 2, None, '___sec6'), + ('More on the PCA', 2, None, '___sec7'), + ('Incremental PCA', 2, None, '___sec8'), + ('Randomized PCA', 2, None, '___sec9'), + ('Kernel PCA', 2, None, '___sec10'), + ('LLE', 2, None, '___sec11'), + ('Other techniques', 2, None, '___sec12')]} end of tocinfo --> @@ -103,15 +107,16 @@ MathJax.Hub.Config({
  • Reducing the number of degrees of freedom, overarching view
  • Preprocessing our data
  • Simple preprocessing examples, Franke function and regression
  • -
  • Simple preprocessing examples, breast cancer data and classification
  • -
  • Principal Component Analysis
  • -
  • PCA and scikit-learn
  • -
  • More on the PCA
  • -
  • Incremental PCA
  • -
  • Randomized PCA
  • -
  • Kernel PCA
  • -
  • LLE
  • -
  • Other techniques
  • +
  • Simple preprocessing examples, breast cancer data and classification, Support Vector Machines
  • +
  • More on Cancer Data, now with Logistic Regression
  • +
  • Principal Component Analysis
  • +
  • PCA and scikit-learn
  • +
  • More on the PCA
  • +
  • Incremental PCA
  • +
  • Randomized PCA
  • +
  • Kernel PCA
  • +
  • LLE
  • +
  • Other techniques
  • @@ -170,7 +175,7 @@ MathJax.Hub.Config({
  • 9
  • 10
  • ...
  • -
  • 13
  • +
  • 14
  • »
  • diff --git a/doc/pub/DimRed/html/DimRed-reveal.html b/doc/pub/DimRed/html/DimRed-reveal.html index ba5ca09ac..bda964130 100644 --- a/doc/pub/DimRed/html/DimRed-reveal.html +++ b/doc/pub/DimRed/html/DimRed-reveal.html @@ -304,7 +304,7 @@ svm.fit(X_train_scaled, y_train)
    -

    Simple preprocessing examples, breast cancer data and classification

    +

    Simple preprocessing examples, breast cancer data and classification, Support Vector Machines

    We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification @@ -356,7 +356,63 @@ svm.fit(X_train_scaled, y_train)

    -

    Principal Component Analysis

    +

    More on Cancer Data, now with Logistic Regression

    +

    + + +

    import matplotlib.pyplot as plt
    +import numpy as np
    +from sklearn.model_selection import  train_test_split 
    +from sklearn.datasets import load_breast_cancer
    +from sklearn.linear_model import LogisticRegression
    +cancer = load_breast_cancer()
    +
    +fig, axes = plt.subplots(15,2,figsize=(10,20))
    +male = cancer.data[cancer.target == 0]
    +bene = cancer.data[cancer.target == 1]
    +ax = axes.ravel()
    +
    +for i in range(30):
    +    _, bins = np.histogram(cancer.data[:,i], bins =50)
    +    ax[i].hist(male[:,i], bins = bins, alpha = 0.5)
    +    ax[i].hist(bene[:,i], bins = bins, alpha = 0.5)
    +    ax[i].set_title(cancer.feature_names[i])
    +    ax[i].set_yticks(())
    +ax[0].set_xlabel("Feature magnitude")
    +ax[0].set_ylabel("Frequency")
    +ax[0].legend(["Male", "Bene"], loc ="best")
    +fig.tight_layout()
    +plt.show()
    +
    +# Set up training data
    +X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
    +# Perform Logistic Regression (replace with own code) 
    +logreg = LogisticRegression()
    +logreg.fit(X_train, y_train)
    +print("Test set accuracy: {:.2f}".format(logreg.score(X_test,y_test)))
    +
    +# Scale data
    +from sklearn.preprocessing import StandardScaler
    +scaler = StandardScaler()
    +scaler.fit(X_train)
    +X_train_scaled = scaler.transform(X_train)
    +X_test_scaled = scaler.transform(X_test)
    +logreg.fit(X_train_scaled, y_train)
    +#svm.fit(X_train_scaled, y_train)
    +print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
    +
    +# Now add PCA
    +from sklearn.decomposition import PCA
    +pca = PCA(n_components = 2)
    +pca.fit(X_train_scaled)
    +
    +X_pca = pca.transform(X_train_scaled)
    +
    +
    + + +
    +

    Principal Component Analysis

    @@ -393,7 +449,7 @@ X2D = X_centered.dot(W2)

    -

    PCA and scikit-learn

    +

    PCA and scikit-learn

    Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The @@ -424,7 +480,7 @@ More material to come here.

    -

    More on the PCA

    +

    More on the PCA

    Instead of arbitrarily choosing the number of dimensions to reduce down to, it is generally preferable to choose the number of dimensions that add up to a sufficiently large portion of the variance (e.g., 95%). Unless, of course, you are reducing dimensionality for data visualization — in that case you will @@ -453,7 +509,7 @@ X_reduced = pca.fit_transform(X)
    -

    Incremental PCA

    +

    Incremental PCA

    One problem with the preceding implementation of PCA is that it requires the whole training set to fit in memory in order for the SVD algorithm to run. Fortunately, Incremental PCA (IPCA) algorithms have been developed: you can split the training set into mini-batches and feed an IPCA algorithm one minibatch @@ -463,7 +519,7 @@ instances arrive).
    -

    Randomized PCA

    +

    Randomized PCA

    Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic @@ -477,7 +533,7 @@ previous algorithms when \( d \) is much smaller than \( n \).

    -

    Kernel PCA

    +

    Kernel PCA

    @@ -503,7 +559,7 @@ X_reduced = rbf_pca.fit_transform(X)

    -

    LLE

    +

    LLE

    Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction @@ -515,7 +571,7 @@ these local relationships are best preserved (more details shortly).

    -

    Other techniques

    +

    Other techniques

    There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn. diff --git a/doc/pub/DimRed/html/DimRed-solarized.html b/doc/pub/DimRed/html/DimRed-solarized.html index 829228a99..f18a7b72b 100644 --- a/doc/pub/DimRed/html/DimRed-solarized.html +++ b/doc/pub/DimRed/html/DimRed-solarized.html @@ -71,18 +71,22 @@ div { text-align: justify; text-justify: inter-word; } None, '___sec2'), ('Simple preprocessing examples, breast cancer data and ' - 'classification', + 'classification, Support Vector Machines', 2, None, '___sec3'), - ('Principal Component Analysis', 2, None, '___sec4'), - ('PCA and scikit-learn', 2, None, '___sec5'), - ('More on the PCA', 2, None, '___sec6'), - ('Incremental PCA', 2, None, '___sec7'), - ('Randomized PCA', 2, None, '___sec8'), - ('Kernel PCA', 2, None, '___sec9'), - ('LLE', 2, None, '___sec10'), - ('Other techniques', 2, None, '___sec11')]} + ('More on Cancer Data, now with Logistic Regression', + 2, + None, + '___sec4'), + ('Principal Component Analysis', 2, None, '___sec5'), + ('PCA and scikit-learn', 2, None, '___sec6'), + ('More on the PCA', 2, None, '___sec7'), + ('Incremental PCA', 2, None, '___sec8'), + ('Randomized PCA', 2, None, '___sec9'), + ('Kernel PCA', 2, None, '___sec10'), + ('LLE', 2, None, '___sec11'), + ('Other techniques', 2, None, '___sec12')]} end of tocinfo --> @@ -279,7 +283,7 @@ svm.fit(X_train_scaled, y_train)











    -

    Simple preprocessing examples, breast cancer data and classification

    +

    Simple preprocessing examples, breast cancer data and classification, Support Vector Machines

    We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification @@ -330,7 +334,62 @@ svm.fit(X_train_scaled, y_train)











    -

    Principal Component Analysis

    +

    More on Cancer Data, now with Logistic Regression

    +

    + + +

    import matplotlib.pyplot as plt
    +import numpy as np
    +from sklearn.model_selection import  train_test_split 
    +from sklearn.datasets import load_breast_cancer
    +from sklearn.linear_model import LogisticRegression
    +cancer = load_breast_cancer()
    +
    +fig, axes = plt.subplots(15,2,figsize=(10,20))
    +male = cancer.data[cancer.target == 0]
    +bene = cancer.data[cancer.target == 1]
    +ax = axes.ravel()
    +
    +for i in range(30):
    +    _, bins = np.histogram(cancer.data[:,i], bins =50)
    +    ax[i].hist(male[:,i], bins = bins, alpha = 0.5)
    +    ax[i].hist(bene[:,i], bins = bins, alpha = 0.5)
    +    ax[i].set_title(cancer.feature_names[i])
    +    ax[i].set_yticks(())
    +ax[0].set_xlabel("Feature magnitude")
    +ax[0].set_ylabel("Frequency")
    +ax[0].legend(["Male", "Bene"], loc ="best")
    +fig.tight_layout()
    +plt.show()
    +
    +# Set up training data
    +X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
    +# Perform Logistic Regression (replace with own code) 
    +logreg = LogisticRegression()
    +logreg.fit(X_train, y_train)
    +print("Test set accuracy: {:.2f}".format(logreg.score(X_test,y_test)))
    +
    +# Scale data
    +from sklearn.preprocessing import StandardScaler
    +scaler = StandardScaler()
    +scaler.fit(X_train)
    +X_train_scaled = scaler.transform(X_train)
    +X_test_scaled = scaler.transform(X_test)
    +logreg.fit(X_train_scaled, y_train)
    +#svm.fit(X_train_scaled, y_train)
    +print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
    +
    +# Now add PCA
    +from sklearn.decomposition import PCA
    +pca = PCA(n_components = 2)
    +pca.fit(X_train_scaled)
    +
    +X_pca = pca.transform(X_train_scaled)
    +
    +

    +









    + +

    Principal Component Analysis

    @@ -366,7 +425,7 @@ X2D = X_centered.dot(W2)

    -

    PCA and scikit-learn

    +

    PCA and scikit-learn

    Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The @@ -397,7 +456,7 @@ More material to come here.











    -

    More on the PCA

    +

    More on the PCA

    Instead of arbitrarily choosing the number of dimensions to reduce down to, it is generally preferable to choose the number of dimensions that add up to a sufficiently large portion of the variance (e.g., 95%). Unless, of course, you are reducing dimensionality for data visualization — in that case you will @@ -425,7 +484,7 @@ X_reduced = pca.fit_transform(X)











    -

    Incremental PCA

    +

    Incremental PCA

    One problem with the preceding implementation of PCA is that it requires the whole training set to fit in memory in order for the SVD algorithm to run. Fortunately, Incremental PCA (IPCA) algorithms have been developed: you can split the training set into mini-batches and feed an IPCA algorithm one minibatch @@ -435,7 +494,7 @@ instances arrive).











    -

    Randomized PCA

    +

    Randomized PCA

    Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic @@ -450,7 +509,7 @@ previous algorithms when \( d \) is much smaller than \( n \).











    -

    Kernel PCA

    +

    Kernel PCA

    @@ -479,7 +538,7 @@ X_reduced = rbf_pca.fit_transform(X)











    -

    LLE

    +

    LLE

    Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction @@ -491,7 +550,7 @@ these local relationships are best preserved (more details shortly).











    -

    Other techniques

    +

    Other techniques

    There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn. diff --git a/doc/pub/DimRed/html/DimRed.html b/doc/pub/DimRed/html/DimRed.html index 421408fe5..b339bc54e 100644 --- a/doc/pub/DimRed/html/DimRed.html +++ b/doc/pub/DimRed/html/DimRed.html @@ -76,18 +76,22 @@ div { text-align: justify; text-justify: inter-word; } None, '___sec2'), ('Simple preprocessing examples, breast cancer data and ' - 'classification', + 'classification, Support Vector Machines', 2, None, '___sec3'), - ('Principal Component Analysis', 2, None, '___sec4'), - ('PCA and scikit-learn', 2, None, '___sec5'), - ('More on the PCA', 2, None, '___sec6'), - ('Incremental PCA', 2, None, '___sec7'), - ('Randomized PCA', 2, None, '___sec8'), - ('Kernel PCA', 2, None, '___sec9'), - ('LLE', 2, None, '___sec10'), - ('Other techniques', 2, None, '___sec11')]} + ('More on Cancer Data, now with Logistic Regression', + 2, + None, + '___sec4'), + ('Principal Component Analysis', 2, None, '___sec5'), + ('PCA and scikit-learn', 2, None, '___sec6'), + ('More on the PCA', 2, None, '___sec7'), + ('Incremental PCA', 2, None, '___sec8'), + ('Randomized PCA', 2, None, '___sec9'), + ('Kernel PCA', 2, None, '___sec10'), + ('LLE', 2, None, '___sec11'), + ('Other techniques', 2, None, '___sec12')]} end of tocinfo --> @@ -284,7 +288,7 @@ svm.fit(X_train_scaled, y_train)











    -

    Simple preprocessing examples, breast cancer data and classification

    +

    Simple preprocessing examples, breast cancer data and classification, Support Vector Machines

    We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification @@ -335,7 +339,62 @@ svm.fit(X_train_scaled, y_train)











    -

    Principal Component Analysis

    +

    More on Cancer Data, now with Logistic Regression

    +

    + + +

    import matplotlib.pyplot as plt
    +import numpy as np
    +from sklearn.model_selection import  train_test_split 
    +from sklearn.datasets import load_breast_cancer
    +from sklearn.linear_model import LogisticRegression
    +cancer = load_breast_cancer()
    +
    +fig, axes = plt.subplots(15,2,figsize=(10,20))
    +male = cancer.data[cancer.target == 0]
    +bene = cancer.data[cancer.target == 1]
    +ax = axes.ravel()
    +
    +for i in range(30):
    +    _, bins = np.histogram(cancer.data[:,i], bins =50)
    +    ax[i].hist(male[:,i], bins = bins, alpha = 0.5)
    +    ax[i].hist(bene[:,i], bins = bins, alpha = 0.5)
    +    ax[i].set_title(cancer.feature_names[i])
    +    ax[i].set_yticks(())
    +ax[0].set_xlabel("Feature magnitude")
    +ax[0].set_ylabel("Frequency")
    +ax[0].legend(["Male", "Bene"], loc ="best")
    +fig.tight_layout()
    +plt.show()
    +
    +# Set up training data
    +X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
    +# Perform Logistic Regression (replace with own code) 
    +logreg = LogisticRegression()
    +logreg.fit(X_train, y_train)
    +print("Test set accuracy: {:.2f}".format(logreg.score(X_test,y_test)))
    +
    +# Scale data
    +from sklearn.preprocessing import StandardScaler
    +scaler = StandardScaler()
    +scaler.fit(X_train)
    +X_train_scaled = scaler.transform(X_train)
    +X_test_scaled = scaler.transform(X_test)
    +logreg.fit(X_train_scaled, y_train)
    +#svm.fit(X_train_scaled, y_train)
    +print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
    +
    +# Now add PCA
    +from sklearn.decomposition import PCA
    +pca = PCA(n_components = 2)
    +pca.fit(X_train_scaled)
    +
    +X_pca = pca.transform(X_train_scaled)
    +
    +

    +









    + +

    Principal Component Analysis

    @@ -371,7 +430,7 @@ X2D = X_centered -

    PCA and scikit-learn

    +

    PCA and scikit-learn

    Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The @@ -402,7 +461,7 @@ More material to come here.











    -

    More on the PCA

    +

    More on the PCA

    Instead of arbitrarily choosing the number of dimensions to reduce down to, it is generally preferable to choose the number of dimensions that add up to a sufficiently large portion of the variance (e.g., 95%). Unless, of course, you are reducing dimensionality for data visualization — in that case you will @@ -430,7 +489,7 @@ X_reduced = pca











    -

    Incremental PCA

    +

    Incremental PCA

    One problem with the preceding implementation of PCA is that it requires the whole training set to fit in memory in order for the SVD algorithm to run. Fortunately, Incremental PCA (IPCA) algorithms have been developed: you can split the training set into mini-batches and feed an IPCA algorithm one minibatch @@ -440,7 +499,7 @@ instances arrive).











    -

    Randomized PCA

    +

    Randomized PCA

    Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic @@ -455,7 +514,7 @@ previous algorithms when \( d \) is much smaller than \( n \).











    -

    Kernel PCA

    +

    Kernel PCA

    @@ -484,7 +543,7 @@ X_reduced = rbf_pcaLLE +

    LLE

    Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction @@ -496,7 +555,7 @@ these local relationships are best preserved (more details shortly).











    -

    Other techniques

    +

    Other techniques

    There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn. diff --git a/doc/pub/DimRed/ipynb/DimRed.ipynb b/doc/pub/DimRed/ipynb/DimRed.ipynb index 370f5479a..eec995584 100644 --- a/doc/pub/DimRed/ipynb/DimRed.ipynb +++ b/doc/pub/DimRed/ipynb/DimRed.ipynb @@ -158,7 +158,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Simple preprocessing examples, breast cancer data and classification\n", + "## Simple preprocessing examples, breast cancer data and classification, Support Vector Machines\n", "\n", "We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification" ] @@ -212,6 +212,70 @@ "print(\"Test set accuracy scaled data: {:.2f}\".format(svm.score(X_test_scaled,y_test)))" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## More on Cancer Data, now with Logistic Regression" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "from sklearn.model_selection import train_test_split \n", + "from sklearn.datasets import load_breast_cancer\n", + "from sklearn.linear_model import LogisticRegression\n", + "cancer = load_breast_cancer()\n", + "\n", + "fig, axes = plt.subplots(15,2,figsize=(10,20))\n", + "male = cancer.data[cancer.target == 0]\n", + "bene = cancer.data[cancer.target == 1]\n", + "ax = axes.ravel()\n", + "\n", + "for i in range(30):\n", + " _, bins = np.histogram(cancer.data[:,i], bins =50)\n", + " ax[i].hist(male[:,i], bins = bins, alpha = 0.5)\n", + " ax[i].hist(bene[:,i], bins = bins, alpha = 0.5)\n", + " ax[i].set_title(cancer.feature_names[i])\n", + " ax[i].set_yticks(())\n", + "ax[0].set_xlabel(\"Feature magnitude\")\n", + "ax[0].set_ylabel(\"Frequency\")\n", + "ax[0].legend([\"Male\", \"Bene\"], loc =\"best\")\n", + "fig.tight_layout()\n", + "plt.show()\n", + "\n", + "# Set up training data\n", + "X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)\n", + "# Perform Logistic Regression (replace with own code) \n", + "logreg = LogisticRegression()\n", + "logreg.fit(X_train, y_train)\n", + "print(\"Test set accuracy: {:.2f}\".format(logreg.score(X_test,y_test)))\n", + "\n", + "# Scale data\n", + "from sklearn.preprocessing import StandardScaler\n", + "scaler = StandardScaler()\n", + "scaler.fit(X_train)\n", + "X_train_scaled = scaler.transform(X_train)\n", + "X_test_scaled = scaler.transform(X_test)\n", + "logreg.fit(X_train_scaled, y_train)\n", + "#svm.fit(X_train_scaled, y_train)\n", + "print(\"Test set accuracy scaled data: {:.2f}\".format(logreg.score(X_test_scaled,y_test)))\n", + "\n", + "# Now add PCA\n", + "from sklearn.decomposition import PCA\n", + "pca = PCA(n_components = 2)\n", + "pca.fit(X_train_scaled)\n", + "\n", + "X_pca = pca.transform(X_train_scaled)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -226,7 +290,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -253,7 +317,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -277,7 +341,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -299,7 +363,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": { "collapsed": false }, @@ -328,7 +392,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": { "collapsed": false }, @@ -351,7 +415,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": { "collapsed": false }, @@ -397,7 +461,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "metadata": { "collapsed": false }, diff --git a/doc/pub/DimRed/ipynb/ipynb-DimRed-src.tar.gz b/doc/pub/DimRed/ipynb/ipynb-DimRed-src.tar.gz index 25c4809a5..9db24fa25 100644 Binary files a/doc/pub/DimRed/ipynb/ipynb-DimRed-src.tar.gz and b/doc/pub/DimRed/ipynb/ipynb-DimRed-src.tar.gz differ diff --git a/doc/pub/DimRed/pdf/DimRed-minted.pdf b/doc/pub/DimRed/pdf/DimRed-minted.pdf index 6a752715d..0beed3110 100644 Binary files a/doc/pub/DimRed/pdf/DimRed-minted.pdf and b/doc/pub/DimRed/pdf/DimRed-minted.pdf differ diff --git a/doc/src/DimRed/DimRed.do.txt b/doc/src/DimRed/DimRed.do.txt index 052f91f72..6f5848873 100644 --- a/doc/src/DimRed/DimRed.do.txt +++ b/doc/src/DimRed/DimRed.do.txt @@ -139,7 +139,7 @@ print("R2 score for scaled data: {:.2f}".format(svm.score(X_test_scaled,y_test) !split -===== Simple preprocessing examples, breast cancer data and classification ===== +===== Simple preprocessing examples, breast cancer data and classification, Support Vector Machines ===== We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification @@ -188,6 +188,59 @@ print("Test set accuracy scaled data: {:.2f}".format(svm.score(X_test_scaled,y_t !ec +!split +===== More on Cancer Data, now with Logistic Regression ===== +!bc pycod +import matplotlib.pyplot as plt +import numpy as np +from sklearn.model_selection import train_test_split +from sklearn.datasets import load_breast_cancer +from sklearn.linear_model import LogisticRegression +cancer = load_breast_cancer() + +fig, axes = plt.subplots(15,2,figsize=(10,20)) +male = cancer.data[cancer.target == 0] +bene = cancer.data[cancer.target == 1] +ax = axes.ravel() + +for i in range(30): + _, bins = np.histogram(cancer.data[:,i], bins =50) + ax[i].hist(male[:,i], bins = bins, alpha = 0.5) + ax[i].hist(bene[:,i], bins = bins, alpha = 0.5) + ax[i].set_title(cancer.feature_names[i]) + ax[i].set_yticks(()) +ax[0].set_xlabel("Feature magnitude") +ax[0].set_ylabel("Frequency") +ax[0].legend(["Male", "Bene"], loc ="best") +fig.tight_layout() +plt.show() + +# Set up training data +X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0) +# Perform Logistic Regression (replace with own code) +logreg = LogisticRegression() +logreg.fit(X_train, y_train) +print("Test set accuracy: {:.2f}".format(logreg.score(X_test,y_test))) + +# Scale data +from sklearn.preprocessing import StandardScaler +scaler = StandardScaler() +scaler.fit(X_train) +X_train_scaled = scaler.transform(X_train) +X_test_scaled = scaler.transform(X_test) +logreg.fit(X_train_scaled, y_train) +#svm.fit(X_train_scaled, y_train) +print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) + +# Now add PCA +from sklearn.decomposition import PCA +pca = PCA(n_components = 2) +pca.fit(X_train_scaled) + +X_pca = pca.transform(X_train_scaled) + + +!ec !split ===== Principal Component Analysis ===== diff --git a/doc/src/DimRed/cancer.py b/doc/src/DimRed/cancer.py new file mode 100644 index 000000000..e1c06c60c --- /dev/null +++ b/doc/src/DimRed/cancer.py @@ -0,0 +1,43 @@ +import matplotlib.pyplot as plt +import numpy as np +from sklearn.model_selection import train_test_split +from sklearn.datasets import load_breast_cancer +from sklearn.svm import SVC +from sklearn.linear_model import LogisticRegression +cancer = load_breast_cancer() + +X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0) +print(X_train.shape) +print(X_test.shape) + +logreg = LogisticRegression() +logreg.fit(X_train, y_train) + +#svm = SVC(C=100) +#svm.fit(X_train, y_train) +print("Test set accuracy: {:.2f}".format(logreg.score(X_test,y_test))) + +from sklearn.preprocessing import MinMaxScaler, StandardScaler + +scaler = StandardScaler() +scaler.fit(X_train) +X_train_scaled = scaler.transform(X_train) +X_test_scaled = scaler.transform(X_test) + +print("Feature min values before scaling:\n {}".format(X_train.min(axis=0))) +print("Feature max values before scaling:\n {}".format(X_train.max(axis=0))) + +print("Feature min values before scaling:\n {}".format(X_train_scaled.min(axis=0))) +print("Feature max values before scaling:\n {}".format(X_train_scaled.max(axis=0))) + +logreg.fit(X_train_scaled, y_train) +#svm.fit(X_train_scaled, y_train) +print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) + +scaler = StandardScaler() +scaler.fit(X_train) +X_train_scaled = scaler.transform(X_train) +X_test_scaled = scaler.transform(X_test) +logreg.fit(X_train_scaled, y_train) +#svm.fit(X_train_scaled, y_train) +print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test))) diff --git a/doc/src/DimRed/cancerlogreg.py b/doc/src/DimRed/cancerlogreg.py new file mode 100644 index 000000000..45f03f36a --- /dev/null +++ b/doc/src/DimRed/cancerlogreg.py @@ -0,0 +1,40 @@ +import matplotlib.pyplot as plt +import numpy as np +from sklearn.model_selection import train_test_split +from sklearn.datasets import load_breast_cancer +from sklearn.linear_model import LogisticRegression +cancer = load_breast_cancer() + +fig, axes = plt.subplots(15,2,figsize=(10,20)) +malignant = cancer.data[cancer.target == 0] +benign = cancer.data[cancer.target == 1] +ax = axes.ravel() + +for i in range(30): + _, bins = np.histogram(cancer.data[:,i], bins =50) + ax[i].hist(malignant[:,i], bins = bins, alpha = 0.5) + ax[i].hist(benign[:,i], bins = bins, alpha = 0.5) + ax[i].set_title(cancer.feature_names[i]) + ax[i].set_yticks(()) +ax[0].set_xlabel("Feature magnitude") +ax[0].set_ylabel("Frequency") +ax[0].legend(["Malignant", "Benign"], loc ="best") +fig.tight_layout() +plt.show() + +X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0) +print(X_train.shape) +print(X_test.shape) + +logreg = LogisticRegression() +logreg.fit(X_train, y_train) +print("Test set accuracy from Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test))) + +from sklearn.preprocessing import MinMaxScaler, StandardScaler +scaler = StandardScaler() +scaler.fit(X_train) +X_train_scaled = scaler.transform(X_train) +X_test_scaled = scaler.transform(X_test) + +logreg.fit(X_train_scaled, y_train) +print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))