diff --git a/doc/pub/DimRed/html/._DimRed-bs000.html b/doc/pub/DimRed/html/._DimRed-bs000.html index bbc53cf32..57a5f55b6 100644 --- a/doc/pub/DimRed/html/._DimRed-bs000.html +++ b/doc/pub/DimRed/html/._DimRed-bs000.html @@ -46,14 +46,15 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('Principal Component Analysis', 2, None, '___sec2'), - ('PCA and scikit-learn', 2, None, '___sec3'), - ('More on the PCA', 2, None, '___sec4'), - ('Incremental PCA', 2, None, '___sec5'), - ('Randomized PCA', 2, None, '___sec6'), - ('Kernel PCA', 2, None, '___sec7'), - ('LLE', 2, None, '___sec8'), - ('Other techniques', 2, None, '___sec9')]} + ('Simple preprocessing examples', 2, None, '___sec2'), + ('Principal Component Analysis', 2, None, '___sec3'), + ('PCA and scikit-learn', 2, None, '___sec4'), + ('More on the PCA', 2, None, '___sec5'), + ('Incremental PCA', 2, None, '___sec6'), + ('Randomized PCA', 2, None, '___sec7'), + ('Kernel PCA', 2, None, '___sec8'), + ('LLE', 2, None, '___sec9'), + ('Other techniques', 2, None, '___sec10')]} end of tocinfo --> @@ -93,14 +94,15 @@ MathJax.Hub.Config({ @@ -135,7 +137,7 @@ MathJax.Hub.Config({
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

-

Oct 10, 2019

+

Oct 12, 2019


@@ -159,7 +161,7 @@ MathJax.Hub.Config({

  • 9
  • 10
  • ...
  • -
  • 11
  • +
  • 12
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs001.html b/doc/pub/DimRed/html/._DimRed-bs001.html index c7963e4f4..02f65d120 100644 --- a/doc/pub/DimRed/html/._DimRed-bs001.html +++ b/doc/pub/DimRed/html/._DimRed-bs001.html @@ -46,14 +46,15 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('Principal Component Analysis', 2, None, '___sec2'), - ('PCA and scikit-learn', 2, None, '___sec3'), - ('More on the PCA', 2, None, '___sec4'), - ('Incremental PCA', 2, None, '___sec5'), - ('Randomized PCA', 2, None, '___sec6'), - ('Kernel PCA', 2, None, '___sec7'), - ('LLE', 2, None, '___sec8'), - ('Other techniques', 2, None, '___sec9')]} + ('Simple preprocessing examples', 2, None, '___sec2'), + ('Principal Component Analysis', 2, None, '___sec3'), + ('PCA and scikit-learn', 2, None, '___sec4'), + ('More on the PCA', 2, None, '___sec5'), + ('Incremental PCA', 2, None, '___sec6'), + ('Randomized PCA', 2, None, '___sec7'), + ('Kernel PCA', 2, None, '___sec8'), + ('LLE', 2, None, '___sec9'), + ('Other techniques', 2, None, '___sec10')]} end of tocinfo --> @@ -93,14 +94,15 @@ MathJax.Hub.Config({ @@ -153,6 +155,8 @@ reduction techniques: the principal component analysis PCA, Kernel PCA, and Loca
  • 9
  • 10
  • 11
  • +
  • ...
  • +
  • 12
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs002.html b/doc/pub/DimRed/html/._DimRed-bs002.html index a720c0ad3..ca0c8c84d 100644 --- a/doc/pub/DimRed/html/._DimRed-bs002.html +++ b/doc/pub/DimRed/html/._DimRed-bs002.html @@ -46,14 +46,15 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('Principal Component Analysis', 2, None, '___sec2'), - ('PCA and scikit-learn', 2, None, '___sec3'), - ('More on the PCA', 2, None, '___sec4'), - ('Incremental PCA', 2, None, '___sec5'), - ('Randomized PCA', 2, None, '___sec6'), - ('Kernel PCA', 2, None, '___sec7'), - ('LLE', 2, None, '___sec8'), - ('Other techniques', 2, None, '___sec9')]} + ('Simple preprocessing examples', 2, None, '___sec2'), + ('Principal Component Analysis', 2, None, '___sec3'), + ('PCA and scikit-learn', 2, None, '___sec4'), + ('More on the PCA', 2, None, '___sec5'), + ('Incremental PCA', 2, None, '___sec6'), + ('Randomized PCA', 2, None, '___sec7'), + ('Kernel PCA', 2, None, '___sec8'), + ('LLE', 2, None, '___sec9'), + ('Other techniques', 2, None, '___sec10')]} end of tocinfo --> @@ -93,14 +94,15 @@ MathJax.Hub.Config({ @@ -123,15 +125,15 @@ MathJax.Hub.Config({

    Before we proceed however, we will discuss how to preprocess our -data. Till now and in connection with project 1 not met so many cases +data. Till now and in connection with our previous examples we have not met so many cases where we are too sensitive to the scaling of our data. Normally the data may need a rescaling and/or may be sensitive to extreme values. Scaling the data renders our inputs much more suitable for the -algorithms we want to emply. +algorithms we want to employ.

    Scikit-Learn has several functions which allow us to rescale the data, normally resulting in much better results in terms of various accuracy scores. The StandardScaler function in Scikit-Learn ensures that for each feature/predictor we study the mean value is zero and the variance is zero (every column in the design/feature matrix). -This scaling has the drawback that it does not ensure that we have a particular maximum or minumum in our data set. Another function included in Scikit-Learn is the MinMaxScaler which ensures that all features are exactly between \( 0 \) and \( 1 \). The Normalizer function scale each column of the design matrix so that +This scaling has the drawback that it does not ensure that we have a particular maximum or minumum in our data set. Another function included in Scikit-Learn is the MinMaxScaler which ensures that all features are exactly between \( 0 \) and \( 1 \). The Normalizer function scales each column of the design matrix by its Euclidean norm.

    @@ -154,6 +156,7 @@ This scaling has the drawback that it does not ensure that we have a particular

  • 9
  • 10
  • 11
  • +
  • 12
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs003.html b/doc/pub/DimRed/html/._DimRed-bs003.html index c0ba3abc6..a64e6b72c 100644 --- a/doc/pub/DimRed/html/._DimRed-bs003.html +++ b/doc/pub/DimRed/html/._DimRed-bs003.html @@ -46,14 +46,15 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('Principal Component Analysis', 2, None, '___sec2'), - ('PCA and scikit-learn', 2, None, '___sec3'), - ('More on the PCA', 2, None, '___sec4'), - ('Incremental PCA', 2, None, '___sec5'), - ('Randomized PCA', 2, None, '___sec6'), - ('Kernel PCA', 2, None, '___sec7'), - ('LLE', 2, None, '___sec8'), - ('Other techniques', 2, None, '___sec9')]} + ('Simple preprocessing examples', 2, None, '___sec2'), + ('Principal Component Analysis', 2, None, '___sec3'), + ('PCA and scikit-learn', 2, None, '___sec4'), + ('More on the PCA', 2, None, '___sec5'), + ('Incremental PCA', 2, None, '___sec6'), + ('Randomized PCA', 2, None, '___sec7'), + ('Kernel PCA', 2, None, '___sec8'), + ('LLE', 2, None, '___sec9'), + ('Other techniques', 2, None, '___sec10')]} end of tocinfo --> @@ -93,14 +94,15 @@ MathJax.Hub.Config({ @@ -116,38 +118,54 @@ 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. +

    Simple preprocessing examples

    -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 +We show here how we can use a simple regression case (our nuclear binding energies discussed earlier). +Rescaling our data with different +

    -

    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.svm import SVC
    +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. -

    +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) - -

    W2 = V.T[:, :2]
    -X2D = X_centered.dot(W2)
    +svm = SVC(C=100)
    +svm.fit(X_train, y_train)
    +print("Test set accuracy: {:.2f}".format(svm.score(X_test,y_test)))
    +
    +from sklearn.preprocessing import MinMaxScaler, StandardScaler
    +
    +scaler = MinMaxScaler()
    +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)))
    +
    +
    +svm.fit(X_train_scaled, y_train)
    +print("Test set accuracy scaled data: {:.2f}".format(svm.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)
    +
    +svm.fit(X_train_scaled, y_train)
    +print("Test set accuracy scaled data: {:.2f}".format(svm.score(X_test_scaled,y_test)))
     

    @@ -165,6 +183,7 @@ X2D = X_centered9

  • 10
  • 11
  • +
  • 12
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs004.html b/doc/pub/DimRed/html/._DimRed-bs004.html index e94811a79..9bd20ef9e 100644 --- a/doc/pub/DimRed/html/._DimRed-bs004.html +++ b/doc/pub/DimRed/html/._DimRed-bs004.html @@ -46,14 +46,15 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('Principal Component Analysis', 2, None, '___sec2'), - ('PCA and scikit-learn', 2, None, '___sec3'), - ('More on the PCA', 2, None, '___sec4'), - ('Incremental PCA', 2, None, '___sec5'), - ('Randomized PCA', 2, None, '___sec6'), - ('Kernel PCA', 2, None, '___sec7'), - ('LLE', 2, None, '___sec8'), - ('Other techniques', 2, None, '___sec9')]} + ('Simple preprocessing examples', 2, None, '___sec2'), + ('Principal Component Analysis', 2, None, '___sec3'), + ('PCA and scikit-learn', 2, None, '___sec4'), + ('More on the PCA', 2, None, '___sec5'), + ('Incremental PCA', 2, None, '___sec6'), + ('Randomized PCA', 2, None, '___sec7'), + ('Kernel PCA', 2, None, '___sec8'), + ('LLE', 2, None, '___sec9'), + ('Other techniques', 2, None, '___sec10')]} end of tocinfo --> @@ -93,14 +94,15 @@ MathJax.Hub.Config({ @@ -114,36 +116,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. -

    @@ -160,6 +167,7 @@ More material to come here.

  • 9
  • 10
  • 11
  • +
  • 12
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs005.html b/doc/pub/DimRed/html/._DimRed-bs005.html index 5d9967a80..fea5bd2b7 100644 --- a/doc/pub/DimRed/html/._DimRed-bs005.html +++ b/doc/pub/DimRed/html/._DimRed-bs005.html @@ -46,14 +46,15 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('Principal Component Analysis', 2, None, '___sec2'), - ('PCA and scikit-learn', 2, None, '___sec3'), - ('More on the PCA', 2, None, '___sec4'), - ('Incremental PCA', 2, None, '___sec5'), - ('Randomized PCA', 2, None, '___sec6'), - ('Kernel PCA', 2, None, '___sec7'), - ('LLE', 2, None, '___sec8'), - ('Other techniques', 2, None, '___sec9')]} + ('Simple preprocessing examples', 2, None, '___sec2'), + ('Principal Component Analysis', 2, None, '___sec3'), + ('PCA and scikit-learn', 2, None, '___sec4'), + ('More on the PCA', 2, None, '___sec5'), + ('Incremental PCA', 2, None, '___sec6'), + ('Randomized PCA', 2, None, '___sec7'), + ('Kernel PCA', 2, None, '___sec8'), + ('LLE', 2, None, '___sec9'), + ('Other techniques', 2, None, '___sec10')]} end of tocinfo --> @@ -93,14 +94,15 @@ MathJax.Hub.Config({ @@ -114,33 +116,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. +

    @@ -157,6 +162,7 @@ X_reduced = pca

  • 9
  • 10
  • 11
  • +
  • 12
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs006.html b/doc/pub/DimRed/html/._DimRed-bs006.html index 8376feceb..93c298869 100644 --- a/doc/pub/DimRed/html/._DimRed-bs006.html +++ b/doc/pub/DimRed/html/._DimRed-bs006.html @@ -46,14 +46,15 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('Principal Component Analysis', 2, None, '___sec2'), - ('PCA and scikit-learn', 2, None, '___sec3'), - ('More on the PCA', 2, None, '___sec4'), - ('Incremental PCA', 2, None, '___sec5'), - ('Randomized PCA', 2, None, '___sec6'), - ('Kernel PCA', 2, None, '___sec7'), - ('LLE', 2, None, '___sec8'), - ('Other techniques', 2, None, '___sec9')]} + ('Simple preprocessing examples', 2, None, '___sec2'), + ('Principal Component Analysis', 2, None, '___sec3'), + ('PCA and scikit-learn', 2, None, '___sec4'), + ('More on the PCA', 2, None, '___sec5'), + ('Incremental PCA', 2, None, '___sec6'), + ('Randomized PCA', 2, None, '___sec7'), + ('Kernel PCA', 2, None, '___sec8'), + ('LLE', 2, None, '___sec9'), + ('Other techniques', 2, None, '___sec10')]} end of tocinfo --> @@ -93,14 +94,15 @@ MathJax.Hub.Config({ @@ -116,13 +118,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)
    +

    @@ -139,6 +159,7 @@ instances arrive).

  • 9
  • 10
  • 11
  • +
  • 12
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs007.html b/doc/pub/DimRed/html/._DimRed-bs007.html index a064b2321..3d7461c0c 100644 --- a/doc/pub/DimRed/html/._DimRed-bs007.html +++ b/doc/pub/DimRed/html/._DimRed-bs007.html @@ -46,14 +46,15 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('Principal Component Analysis', 2, None, '___sec2'), - ('PCA and scikit-learn', 2, None, '___sec3'), - ('More on the PCA', 2, None, '___sec4'), - ('Incremental PCA', 2, None, '___sec5'), - ('Randomized PCA', 2, None, '___sec6'), - ('Kernel PCA', 2, None, '___sec7'), - ('LLE', 2, None, '___sec8'), - ('Other techniques', 2, None, '___sec9')]} + ('Simple preprocessing examples', 2, None, '___sec2'), + ('Principal Component Analysis', 2, None, '___sec3'), + ('PCA and scikit-learn', 2, None, '___sec4'), + ('More on the PCA', 2, None, '___sec5'), + ('Incremental PCA', 2, None, '___sec6'), + ('Randomized PCA', 2, None, '___sec7'), + ('Kernel PCA', 2, None, '___sec8'), + ('LLE', 2, None, '___sec9'), + ('Other techniques', 2, None, '___sec10')]} end of tocinfo --> @@ -93,14 +94,15 @@ MathJax.Hub.Config({ @@ -116,18 +118,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).

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

  • 9
  • 10
  • 11
  • +
  • 12
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs008.html b/doc/pub/DimRed/html/._DimRed-bs008.html index cb888f35f..c26ea557e 100644 --- a/doc/pub/DimRed/html/._DimRed-bs008.html +++ b/doc/pub/DimRed/html/._DimRed-bs008.html @@ -46,14 +46,15 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('Principal Component Analysis', 2, None, '___sec2'), - ('PCA and scikit-learn', 2, None, '___sec3'), - ('More on the PCA', 2, None, '___sec4'), - ('Incremental PCA', 2, None, '___sec5'), - ('Randomized PCA', 2, None, '___sec6'), - ('Kernel PCA', 2, None, '___sec7'), - ('LLE', 2, None, '___sec8'), - ('Other techniques', 2, None, '___sec9')]} + ('Simple preprocessing examples', 2, None, '___sec2'), + ('Principal Component Analysis', 2, None, '___sec3'), + ('PCA and scikit-learn', 2, None, '___sec4'), + ('More on the PCA', 2, None, '___sec5'), + ('Incremental PCA', 2, None, '___sec6'), + ('Randomized PCA', 2, None, '___sec7'), + ('Kernel PCA', 2, None, '___sec8'), + ('LLE', 2, None, '___sec9'), + ('Other techniques', 2, None, '___sec10')]} end of tocinfo --> @@ -93,14 +94,15 @@ MathJax.Hub.Config({ @@ -116,28 +118,14 @@ MathJax.Hub.Config({ -

    Kernel PCA

    -
    -
    -

    +

    Randomized PCA

    -The kernel trick is a mathematical technique that implicitly maps instances into a -very high-dimensional space (called the feature space), enabling nonlinear classification and regression -with Support Vector Machines. Recall that a linear decision boundary in the high-dimensional feature -space corresponds to a complex nonlinear decision boundary in the original space. -It turns out that the same trick can be applied to PCA, making it possible to perform complex nonlinear -projections for dimensionality reduction. This is called Kernel PCA (kPCA). It is often good at -preserving clusters of instances after projection, or sometimes even unrolling datasets that lie close to a -twisted manifold. -For example, the following code uses Scikit-Learn’s KernelPCA class to perform kPCA with an -

    +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 \). - -

    from sklearn.decomposition import KernelPCA
    -rbf_pca = KernelPCA(n_components = 2, kernel="rbf", gamma=0.04)
    -X_reduced = rbf_pca.fit_transform(X)
    -

    @@ -159,6 +147,7 @@ X_reduced = rbf_pca9
  • 10
  • 11
  • +
  • 12
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs009.html b/doc/pub/DimRed/html/._DimRed-bs009.html index e95aeda9c..f6dd448e3 100644 --- a/doc/pub/DimRed/html/._DimRed-bs009.html +++ b/doc/pub/DimRed/html/._DimRed-bs009.html @@ -46,14 +46,15 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('Principal Component Analysis', 2, None, '___sec2'), - ('PCA and scikit-learn', 2, None, '___sec3'), - ('More on the PCA', 2, None, '___sec4'), - ('Incremental PCA', 2, None, '___sec5'), - ('Randomized PCA', 2, None, '___sec6'), - ('Kernel PCA', 2, None, '___sec7'), - ('LLE', 2, None, '___sec8'), - ('Other techniques', 2, None, '___sec9')]} + ('Simple preprocessing examples', 2, None, '___sec2'), + ('Principal Component Analysis', 2, None, '___sec3'), + ('PCA and scikit-learn', 2, None, '___sec4'), + ('More on the PCA', 2, None, '___sec5'), + ('Incremental PCA', 2, None, '___sec6'), + ('Randomized PCA', 2, None, '___sec7'), + ('Kernel PCA', 2, None, '___sec8'), + ('LLE', 2, None, '___sec9'), + ('Other techniques', 2, None, '___sec10')]} end of tocinfo --> @@ -93,14 +94,15 @@ MathJax.Hub.Config({ @@ -116,14 +118,32 @@ MathJax.Hub.Config({ -

    LLE

    +

    Kernel PCA

    +
    +
    +

    -Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction -(NLDR) technique. It is a Manifold Learning technique that does not rely on projections like the previous -algorithms. In a nutshell, LLE works by first measuring how each training instance linearly relates to its -closest neighbors (c.n.), and then looking for a low-dimensional representation of the training set where -these local relationships are best preserved (more details shortly). +The kernel trick is a mathematical technique that implicitly maps instances into a +very high-dimensional space (called the feature space), enabling nonlinear classification and regression +with Support Vector Machines. Recall that a linear decision boundary in the high-dimensional feature +space corresponds to a complex nonlinear decision boundary in the original space. +It turns out that the same trick can be applied to PCA, making it possible to perform complex nonlinear +projections for dimensionality reduction. This is called Kernel PCA (kPCA). It is often good at +preserving clusters of instances after projection, or sometimes even unrolling datasets that lie close to a +twisted manifold. +For example, the following code uses Scikit-Learn’s KernelPCA class to perform kPCA with an +

    + + +

    from sklearn.decomposition import KernelPCA
    +rbf_pca = KernelPCA(n_components = 2, kernel="rbf", gamma=0.04)
    +X_reduced = rbf_pca.fit_transform(X)
    +
    +

    +

    +
    +

    @@ -141,6 +161,7 @@ these local relationships are best preserved (more details shortly).

  • 9
  • 10
  • 11
  • +
  • 12
  • »
  • diff --git a/doc/pub/DimRed/html/DimRed-bs.html b/doc/pub/DimRed/html/DimRed-bs.html index bbc53cf32..57a5f55b6 100644 --- a/doc/pub/DimRed/html/DimRed-bs.html +++ b/doc/pub/DimRed/html/DimRed-bs.html @@ -46,14 +46,15 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('Principal Component Analysis', 2, None, '___sec2'), - ('PCA and scikit-learn', 2, None, '___sec3'), - ('More on the PCA', 2, None, '___sec4'), - ('Incremental PCA', 2, None, '___sec5'), - ('Randomized PCA', 2, None, '___sec6'), - ('Kernel PCA', 2, None, '___sec7'), - ('LLE', 2, None, '___sec8'), - ('Other techniques', 2, None, '___sec9')]} + ('Simple preprocessing examples', 2, None, '___sec2'), + ('Principal Component Analysis', 2, None, '___sec3'), + ('PCA and scikit-learn', 2, None, '___sec4'), + ('More on the PCA', 2, None, '___sec5'), + ('Incremental PCA', 2, None, '___sec6'), + ('Randomized PCA', 2, None, '___sec7'), + ('Kernel PCA', 2, None, '___sec8'), + ('LLE', 2, None, '___sec9'), + ('Other techniques', 2, None, '___sec10')]} end of tocinfo --> @@ -93,14 +94,15 @@ MathJax.Hub.Config({ @@ -135,7 +137,7 @@ MathJax.Hub.Config({
    [2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

    -

    Oct 10, 2019

    +

    Oct 12, 2019


    @@ -159,7 +161,7 @@ MathJax.Hub.Config({

  • 9
  • 10
  • ...
  • -
  • 11
  • +
  • 12
  • »
  • diff --git a/doc/pub/DimRed/html/DimRed-reveal.html b/doc/pub/DimRed/html/DimRed-reveal.html index fadbd8544..1cb7cc4d2 100644 --- a/doc/pub/DimRed/html/DimRed-reveal.html +++ b/doc/pub/DimRed/html/DimRed-reveal.html @@ -148,7 +148,7 @@ MathJax.Hub.Config({
    [2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

     
    -

    Oct 10, 2019

    +

    Oct 12, 2019


    @@ -184,15 +184,15 @@ reduction techniques: the principal component analysis PCA, Kernel PCA, and Loca

    Before we proceed however, we will discuss how to preprocess our -data. Till now and in connection with project 1 not met so many cases +data. Till now and in connection with our previous examples we have not met so many cases where we are too sensitive to the scaling of our data. Normally the data may need a rescaling and/or may be sensitive to extreme values. Scaling the data renders our inputs much more suitable for the -algorithms we want to emply. +algorithms we want to employ.

    Scikit-Learn has several functions which allow us to rescale the data, normally resulting in much better results in terms of various accuracy scores. The StandardScaler function in Scikit-Learn ensures that for each feature/predictor we study the mean value is zero and the variance is zero (every column in the design/feature matrix). -This scaling has the drawback that it does not ensure that we have a particular maximum or minumum in our data set. Another function included in Scikit-Learn is the MinMaxScaler which ensures that all features are exactly between \( 0 \) and \( 1 \). The Normalizer function scale each column of the design matrix so that +This scaling has the drawback that it does not ensure that we have a particular maximum or minumum in our data set. Another function included in Scikit-Learn is the MinMaxScaler which ensures that all features are exactly between \( 0 \) and \( 1 \). The Normalizer function scales each column of the design matrix by its Euclidean norm.

    @@ -200,7 +200,60 @@ This scaling has the drawback that it does not ensure that we have a particular
    -

    Principal Component Analysis

    +

    Simple preprocessing examples

    + +

    +We show here how we can use a simple regression case (our nuclear binding energies discussed earlier). +Rescaling our data with different + +

    + + +

    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
    +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)
    +
    +svm = SVC(C=100)
    +svm.fit(X_train, y_train)
    +print("Test set accuracy: {:.2f}".format(svm.score(X_test,y_test)))
    +
    +from sklearn.preprocessing import MinMaxScaler, StandardScaler
    +
    +scaler = MinMaxScaler()
    +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)))
    +
    +
    +svm.fit(X_train_scaled, y_train)
    +print("Test set accuracy scaled data: {:.2f}".format(svm.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)
    +
    +svm.fit(X_train_scaled, y_train)
    +print("Test set accuracy scaled data: {:.2f}".format(svm.score(X_test_scaled,y_test)))
    +
    +
    + + +
    +

    Principal Component Analysis

    @@ -237,7 +290,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 @@ -268,7 +321,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 @@ -297,7 +350,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 @@ -307,7 +360,7 @@ instances arrive).
    -

    Randomized PCA

    +

    Randomized PCA

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

    -

    Kernel PCA

    +

    Kernel PCA

    @@ -347,7 +400,7 @@ X_reduced = rbf_pca.fit_transform(X)

    -

    LLE

    +

    LLE

    Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction @@ -359,7 +412,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 79620e5a8..599608524 100644 --- a/doc/pub/DimRed/html/DimRed-solarized.html +++ b/doc/pub/DimRed/html/DimRed-solarized.html @@ -66,14 +66,15 @@ div { text-align: justify; text-justify: inter-word; } None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('Principal Component Analysis', 2, None, '___sec2'), - ('PCA and scikit-learn', 2, None, '___sec3'), - ('More on the PCA', 2, None, '___sec4'), - ('Incremental PCA', 2, None, '___sec5'), - ('Randomized PCA', 2, None, '___sec6'), - ('Kernel PCA', 2, None, '___sec7'), - ('LLE', 2, None, '___sec8'), - ('Other techniques', 2, None, '___sec9')]} + ('Simple preprocessing examples', 2, None, '___sec2'), + ('Principal Component Analysis', 2, None, '___sec3'), + ('PCA and scikit-learn', 2, None, '___sec4'), + ('More on the PCA', 2, None, '___sec5'), + ('Incremental PCA', 2, None, '___sec6'), + ('Randomized PCA', 2, None, '___sec7'), + ('Kernel PCA', 2, None, '___sec8'), + ('LLE', 2, None, '___sec9'), + ('Other techniques', 2, None, '___sec10')]} end of tocinfo --> @@ -115,7 +116,7 @@ MathJax.Hub.Config({

    [2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

    -

    Oct 10, 2019

    +

    Oct 12, 2019












    @@ -150,15 +151,15 @@ reduction techniques: the principal component analysis PCA, Kernel PCA, and Loca

    Before we proceed however, we will discuss how to preprocess our -data. Till now and in connection with project 1 not met so many cases +data. Till now and in connection with our previous examples we have not met so many cases where we are too sensitive to the scaling of our data. Normally the data may need a rescaling and/or may be sensitive to extreme values. Scaling the data renders our inputs much more suitable for the -algorithms we want to emply. +algorithms we want to employ.

    Scikit-Learn has several functions which allow us to rescale the data, normally resulting in much better results in terms of various accuracy scores. The StandardScaler function in Scikit-Learn ensures that for each feature/predictor we study the mean value is zero and the variance is zero (every column in the design/feature matrix). -This scaling has the drawback that it does not ensure that we have a particular maximum or minumum in our data set. Another function included in Scikit-Learn is the MinMaxScaler which ensures that all features are exactly between \( 0 \) and \( 1 \). The Normalizer function scale each column of the design matrix so that +This scaling has the drawback that it does not ensure that we have a particular maximum or minumum in our data set. Another function included in Scikit-Learn is the MinMaxScaler which ensures that all features are exactly between \( 0 \) and \( 1 \). The Normalizer function scales each column of the design matrix by its Euclidean norm.

    @@ -167,7 +168,59 @@ This scaling has the drawback that it does not ensure that we have a particular











    -

    Principal Component Analysis

    +

    Simple preprocessing examples

    + +

    +We show here how we can use a simple regression case (our nuclear binding energies discussed earlier). +Rescaling our data with different + +

    + + +

    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
    +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)
    +
    +svm = SVC(C=100)
    +svm.fit(X_train, y_train)
    +print("Test set accuracy: {:.2f}".format(svm.score(X_test,y_test)))
    +
    +from sklearn.preprocessing import MinMaxScaler, StandardScaler
    +
    +scaler = MinMaxScaler()
    +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)))
    +
    +
    +svm.fit(X_train_scaled, y_train)
    +print("Test set accuracy scaled data: {:.2f}".format(svm.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)
    +
    +svm.fit(X_train_scaled, y_train)
    +print("Test set accuracy scaled data: {:.2f}".format(svm.score(X_test_scaled,y_test)))
    +
    +

    +









    + +

    Principal Component Analysis

    @@ -203,7 +256,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 @@ -234,7 +287,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 @@ -262,7 +315,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 @@ -272,7 +325,7 @@ instances arrive).











    -

    Randomized PCA

    +

    Randomized PCA

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











    -

    Kernel PCA

    +

    Kernel PCA

    @@ -316,7 +369,7 @@ X_reduced = rbf_pca.fit_transform(X)











    -

    LLE

    +

    LLE

    Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction @@ -328,7 +381,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 426a673ac..98ec982c9 100644 --- a/doc/pub/DimRed/html/DimRed.html +++ b/doc/pub/DimRed/html/DimRed.html @@ -71,14 +71,15 @@ div { text-align: justify; text-justify: inter-word; } None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('Principal Component Analysis', 2, None, '___sec2'), - ('PCA and scikit-learn', 2, None, '___sec3'), - ('More on the PCA', 2, None, '___sec4'), - ('Incremental PCA', 2, None, '___sec5'), - ('Randomized PCA', 2, None, '___sec6'), - ('Kernel PCA', 2, None, '___sec7'), - ('LLE', 2, None, '___sec8'), - ('Other techniques', 2, None, '___sec9')]} + ('Simple preprocessing examples', 2, None, '___sec2'), + ('Principal Component Analysis', 2, None, '___sec3'), + ('PCA and scikit-learn', 2, None, '___sec4'), + ('More on the PCA', 2, None, '___sec5'), + ('Incremental PCA', 2, None, '___sec6'), + ('Randomized PCA', 2, None, '___sec7'), + ('Kernel PCA', 2, None, '___sec8'), + ('LLE', 2, None, '___sec9'), + ('Other techniques', 2, None, '___sec10')]} end of tocinfo --> @@ -120,7 +121,7 @@ MathJax.Hub.Config({

    [2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

    -

    Oct 10, 2019

    +

    Oct 12, 2019












    @@ -155,15 +156,15 @@ reduction techniques: the principal component analysis PCA, Kernel PCA, and Loca

    Before we proceed however, we will discuss how to preprocess our -data. Till now and in connection with project 1 not met so many cases +data. Till now and in connection with our previous examples we have not met so many cases where we are too sensitive to the scaling of our data. Normally the data may need a rescaling and/or may be sensitive to extreme values. Scaling the data renders our inputs much more suitable for the -algorithms we want to emply. +algorithms we want to employ.

    Scikit-Learn has several functions which allow us to rescale the data, normally resulting in much better results in terms of various accuracy scores. The StandardScaler function in Scikit-Learn ensures that for each feature/predictor we study the mean value is zero and the variance is zero (every column in the design/feature matrix). -This scaling has the drawback that it does not ensure that we have a particular maximum or minumum in our data set. Another function included in Scikit-Learn is the MinMaxScaler which ensures that all features are exactly between \( 0 \) and \( 1 \). The Normalizer function scale each column of the design matrix so that +This scaling has the drawback that it does not ensure that we have a particular maximum or minumum in our data set. Another function included in Scikit-Learn is the MinMaxScaler which ensures that all features are exactly between \( 0 \) and \( 1 \). The Normalizer function scales each column of the design matrix by its Euclidean norm.

    @@ -172,7 +173,59 @@ This scaling has the drawback that it does not ensure that we have a particular











    -

    Principal Component Analysis

    +

    Simple preprocessing examples

    + +

    +We show here how we can use a simple regression case (our nuclear binding energies discussed earlier). +Rescaling our data with different + +

    + + +

    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
    +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)
    +
    +svm = SVC(C=100)
    +svm.fit(X_train, y_train)
    +print("Test set accuracy: {:.2f}".format(svm.score(X_test,y_test)))
    +
    +from sklearn.preprocessing import MinMaxScaler, StandardScaler
    +
    +scaler = MinMaxScaler()
    +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)))
    +
    +
    +svm.fit(X_train_scaled, y_train)
    +print("Test set accuracy scaled data: {:.2f}".format(svm.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)
    +
    +svm.fit(X_train_scaled, y_train)
    +print("Test set accuracy scaled data: {:.2f}".format(svm.score(X_test_scaled,y_test)))
    +
    +

    +









    + +

    Principal Component Analysis

    @@ -208,7 +261,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 @@ -239,7 +292,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 @@ -267,7 +320,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 @@ -277,7 +330,7 @@ instances arrive).











    -

    Randomized PCA

    +

    Randomized PCA

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











    -

    Kernel PCA

    +

    Kernel PCA

    @@ -321,7 +374,7 @@ X_reduced = rbf_pcaLLE +

    LLE

    Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction @@ -333,7 +386,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 2a1cdc58d..b9620502d 100644 --- a/doc/pub/DimRed/ipynb/DimRed.ipynb +++ b/doc/pub/DimRed/ipynb/DimRed.ipynb @@ -10,7 +10,7 @@ " \n", "**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n", "\n", - "Date: **Oct 10, 2019**\n", + "Date: **Oct 12, 2019**\n", "\n", "Copyright 1999-2019, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n", "\n", @@ -34,19 +34,79 @@ "## Preprocessing our data\n", "\n", "Before we proceed however, we will discuss how to preprocess our\n", - "data. Till now and in connection with project 1 not met so many cases\n", + "data. Till now and in connection with our previous examples we have not met so many cases\n", "where we are too sensitive to the scaling of our data. Normally the\n", "data may need a rescaling and/or may be sensitive to extreme\n", "values. Scaling the data renders our inputs much more suitable for the\n", - "algorithms we want to emply.\n", + "algorithms we want to employ.\n", "\n", "**Scikit-Learn** has several functions which allow us to rescale the data, normally resulting in much better results in terms of various accuracy scores. The **StandardScaler** function in **Scikit-Learn** ensures that for each feature/predictor we study the mean value is zero and the variance is zero (every column in the design/feature matrix).\n", - "This scaling has the drawback that it does not ensure that we have a particular maximum or minumum in our data set. Another function included in **Scikit-Learn** is the **MinMaxScaler** which ensures that all features are exactly between $0$ and $1$. The **Normalizer** function scale each column of the design matrix so that\n", + "This scaling has the drawback that it does not ensure that we have a particular maximum or minumum in our data set. Another function included in **Scikit-Learn** is the **MinMaxScaler** which ensures that all features are exactly between $0$ and $1$. The **Normalizer** function scales each column of the design matrix by its Euclidean norm.\n", "\n", "\n", "\n", "\n", + "## Simple preprocessing examples\n", "\n", + "We show here how we can use a simple regression case (our nuclear binding energies discussed earlier).\n", + "Rescaling our data with different" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "%matplotlib inline\n", + "\n", + "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.svm import SVC\n", + "cancer = load_breast_cancer()\n", + "\n", + "X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)\n", + "print(X_train.shape)\n", + "print(X_test.shape)\n", + "\n", + "svm = SVC(C=100)\n", + "svm.fit(X_train, y_train)\n", + "print(\"Test set accuracy: {:.2f}\".format(svm.score(X_test,y_test)))\n", + "\n", + "from sklearn.preprocessing import MinMaxScaler, StandardScaler\n", + "\n", + "scaler = MinMaxScaler()\n", + "scaler.fit(X_train)\n", + "X_train_scaled = scaler.transform(X_train)\n", + "X_test_scaled = scaler.transform(X_test)\n", + "\n", + "print(\"Feature min values before scaling:\\n {}\".format(X_train.min(axis=0)))\n", + "print(\"Feature max values before scaling:\\n {}\".format(X_train.max(axis=0)))\n", + "\n", + "print(\"Feature min values before scaling:\\n {}\".format(X_train_scaled.min(axis=0)))\n", + "print(\"Feature max values before scaling:\\n {}\".format(X_train_scaled.max(axis=0)))\n", + "\n", + "\n", + "svm.fit(X_train_scaled, y_train)\n", + "print(\"Test set accuracy scaled data: {:.2f}\".format(svm.score(X_test_scaled,y_test)))\n", + "\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", + "\n", + "svm.fit(X_train_scaled, y_train)\n", + "print(\"Test set accuracy scaled data: {:.2f}\".format(svm.score(X_test_scaled,y_test)))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "## Principal Component Analysis\n", "Principal Component Analysis (PCA) is by far the most popular dimensionality reduction algorithm.\n", "First it identifies the hyperplane that lies closest to the data, and then it projects the data onto it.\n", @@ -57,7 +117,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -84,7 +144,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -108,7 +168,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -130,7 +190,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -159,7 +219,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -182,7 +242,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": { "collapsed": false }, @@ -228,7 +288,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "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 0fe79a5ff..c7ace3921 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 5a901e9eb..aafa544a7 100644 Binary files a/doc/pub/DimRed/pdf/DimRed-minted.pdf and b/doc/pub/DimRed/pdf/DimRed-minted.pdf differ