diff --git a/doc/pub/DimRed/html/._DimRed-bs000.html b/doc/pub/DimRed/html/._DimRed-bs000.html index 57a5f55b6..84af171c4 100644 --- a/doc/pub/DimRed/html/._DimRed-bs000.html +++ b/doc/pub/DimRed/html/._DimRed-bs000.html @@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('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')]} + ('Simple preprocessing examples, Franke function and regression', + 2, + None, + '___sec2'), + ('Simple preprocessing examples, breast cancer data and ' + 'classification', + 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')]} end of tocinfo --> @@ -94,15 +102,16 @@ MathJax.Hub.Config({ @@ -137,7 +146,7 @@ MathJax.Hub.Config({
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

-

Oct 12, 2019

+

Oct 14, 2019


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

  • 9
  • 10
  • ...
  • -
  • 12
  • +
  • 13
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs001.html b/doc/pub/DimRed/html/._DimRed-bs001.html index 02f65d120..8027c7441 100644 --- a/doc/pub/DimRed/html/._DimRed-bs001.html +++ b/doc/pub/DimRed/html/._DimRed-bs001.html @@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('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')]} + ('Simple preprocessing examples, Franke function and regression', + 2, + None, + '___sec2'), + ('Simple preprocessing examples, breast cancer data and ' + 'classification', + 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')]} end of tocinfo --> @@ -94,15 +102,16 @@ MathJax.Hub.Config({ @@ -156,7 +165,7 @@ reduction techniques: the principal component analysis PCA, Kernel PCA, and Loca
  • 10
  • 11
  • ...
  • -
  • 12
  • +
  • 13
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs002.html b/doc/pub/DimRed/html/._DimRed-bs002.html index ca0c8c84d..9b0341c36 100644 --- a/doc/pub/DimRed/html/._DimRed-bs002.html +++ b/doc/pub/DimRed/html/._DimRed-bs002.html @@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('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')]} + ('Simple preprocessing examples, Franke function and regression', + 2, + None, + '___sec2'), + ('Simple preprocessing examples, breast cancer data and ' + 'classification', + 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')]} end of tocinfo --> @@ -94,15 +102,16 @@ MathJax.Hub.Config({ @@ -157,6 +166,8 @@ This scaling has the drawback that it does not ensure that we have a particular
  • 10
  • 11
  • 12
  • +
  • ...
  • +
  • 13
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs003.html b/doc/pub/DimRed/html/._DimRed-bs003.html index a64e6b72c..b57689b38 100644 --- a/doc/pub/DimRed/html/._DimRed-bs003.html +++ b/doc/pub/DimRed/html/._DimRed-bs003.html @@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('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')]} + ('Simple preprocessing examples, Franke function and regression', + 2, + None, + '___sec2'), + ('Simple preprocessing examples, breast cancer data and ' + 'classification', + 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')]} end of tocinfo --> @@ -94,15 +102,16 @@ MathJax.Hub.Config({ @@ -118,33 +127,90 @@ MathJax.Hub.Config({ -

    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 +

    Simple preprocessing examples, Franke function and regression

    -

    import matplotlib.pyplot as plt
    +
    # Common imports
    +import os
     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()
    +import pandas as pd
    +import matplotlib.pyplot as plt
    +import sklearn.linear_model as skl
    +from sklearn.metrics import mean_squared_error
    +from sklearn.model_selection import  train_test_split
    +from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer
    +from sklearn.svm import SVR
     
    -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)
    +# Where to save the figures and data files
    +PROJECT_ROOT_DIR = "Results"
    +FIGURE_ID = "Results/FigureFiles"
    +DATA_ID = "DataFiles/"
     
    -svm = SVC(C=100)
    +if not os.path.exists(PROJECT_ROOT_DIR):
    +    os.mkdir(PROJECT_ROOT_DIR)
    +
    +if not os.path.exists(FIGURE_ID):
    +    os.makedirs(FIGURE_ID)
    +
    +if not os.path.exists(DATA_ID):
    +    os.makedirs(DATA_ID)
    +
    +def image_path(fig_id):
    +    return os.path.join(FIGURE_ID, fig_id)
    +
    +def data_path(dat_id):
    +    return os.path.join(DATA_ID, dat_id)
    +
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
    +
    +
    +def FrankeFunction(x,y):
    +	term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
    +	term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
    +	term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
    +	term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
    +	return term1 + term2 + term3 + term4
    +
    +
    +def create_X(x, y, n ):
    +	if len(x.shape) > 1:
    +		x = np.ravel(x)
    +		y = np.ravel(y)
    +
    +	N = len(x)
    +	l = int((n+1)*(n+2)/2)		# Number of elements in beta
    +	X = np.ones((N,l))
    +
    +	for i in range(1,n+1):
    +		q = int((i)*(i+1)/2)
    +		for k in range(i+1):
    +			X[:,q+k] = (x**(i-k))*(y**k)
    +
    +	return X
    +
    +
    +# Making meshgrid of datapoints and compute Franke's function
    +n = 5
    +N = 1000
    +x = np.sort(np.random.uniform(0, 1, N))
    +y = np.sort(np.random.uniform(0, 1, N))
    +z = FrankeFunction(x, y)
    +X = create_X(x, y, n=n)    
    +# split in training and test data
    +X_train, X_test, y_train, y_test = train_test_split(X,z,test_size=0.2)
    +
    +
    +svm = SVR(gamma='auto',C=10.0)
     svm.fit(X_train, y_train)
    -print("Test set accuracy: {:.2f}".format(svm.score(X_test,y_test)))
     
    -from sklearn.preprocessing import MinMaxScaler, StandardScaler
    +# The mean squared error and R2 score
    +print("MSE before scaling: {:.2f}".format(mean_squared_error(svm.predict(X_test), y_test)))
    +print("R2 score before scaling {:.2f}".format(svm.score(X_test,y_test)))
     
    -scaler = MinMaxScaler()
    +scaler = StandardScaler()
     scaler.fit(X_train)
     X_train_scaled = scaler.transform(X_train)
     X_test_scaled = scaler.transform(X_test)
    @@ -152,20 +218,14 @@ X_test_scaled = scalerprint("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)))
    -
    +print("Feature min values after scaling:\n {}".format(X_train_scaled.min(axis=0)))
    +print("Feature max values after scaling:\n {}".format(X_train_scaled.max(axis=0)))
     
    +svm = SVR(gamma='auto',C=10.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)))
    +print("MSE after  scaling: {:.2f}".format(mean_squared_error(svm.predict(X_test_scaled), y_test)))
    +print("R2 score for  scaled data: {:.2f}".format(svm.score(X_test_scaled,y_test)))
     

    @@ -184,6 +244,7 @@ svm.fit(X_train_scaled, y_train)

  • 10
  • 11
  • 12
  • +
  • 13
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs004.html b/doc/pub/DimRed/html/._DimRed-bs004.html index 9bd20ef9e..e7bf415b7 100644 --- a/doc/pub/DimRed/html/._DimRed-bs004.html +++ b/doc/pub/DimRed/html/._DimRed-bs004.html @@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('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')]} + ('Simple preprocessing examples, Franke function and regression', + 2, + None, + '___sec2'), + ('Simple preprocessing examples, breast cancer data and ' + 'classification', + 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')]} end of tocinfo --> @@ -94,15 +102,16 @@ MathJax.Hub.Config({ @@ -118,38 +127,53 @@ 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, breast cancer data and classification

    -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 on the breast cancer data using support vector machine as algorithm for classification +

    -

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

    @@ -168,6 +192,7 @@ X2D = X_centered10

  • 11
  • 12
  • +
  • 13
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs005.html b/doc/pub/DimRed/html/._DimRed-bs005.html index fea5bd2b7..1ca4766d5 100644 --- a/doc/pub/DimRed/html/._DimRed-bs005.html +++ b/doc/pub/DimRed/html/._DimRed-bs005.html @@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('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')]} + ('Simple preprocessing examples, Franke function and regression', + 2, + None, + '___sec2'), + ('Simple preprocessing examples, breast cancer data and ' + 'classification', + 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')]} end of tocinfo --> @@ -94,15 +102,16 @@ MathJax.Hub.Config({ @@ -116,36 +125,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. -

    @@ -163,6 +177,7 @@ More material to come here.

  • 10
  • 11
  • 12
  • +
  • 13
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs006.html b/doc/pub/DimRed/html/._DimRed-bs006.html index 93c298869..76dff32c4 100644 --- a/doc/pub/DimRed/html/._DimRed-bs006.html +++ b/doc/pub/DimRed/html/._DimRed-bs006.html @@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('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')]} + ('Simple preprocessing examples, Franke function and regression', + 2, + None, + '___sec2'), + ('Simple preprocessing examples, breast cancer data and ' + 'classification', + 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')]} end of tocinfo --> @@ -94,15 +102,16 @@ MathJax.Hub.Config({ @@ -116,33 +125,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. +

    @@ -160,6 +172,7 @@ X_reduced = pca

  • 10
  • 11
  • 12
  • +
  • 13
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs007.html b/doc/pub/DimRed/html/._DimRed-bs007.html index 3d7461c0c..b93647590 100644 --- a/doc/pub/DimRed/html/._DimRed-bs007.html +++ b/doc/pub/DimRed/html/._DimRed-bs007.html @@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('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')]} + ('Simple preprocessing examples, Franke function and regression', + 2, + None, + '___sec2'), + ('Simple preprocessing examples, breast cancer data and ' + 'classification', + 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')]} end of tocinfo --> @@ -94,15 +102,16 @@ MathJax.Hub.Config({ @@ -118,13 +127,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)
    +

    @@ -142,6 +169,7 @@ instances arrive).

  • 10
  • 11
  • 12
  • +
  • 13
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs008.html b/doc/pub/DimRed/html/._DimRed-bs008.html index c26ea557e..9c9289160 100644 --- a/doc/pub/DimRed/html/._DimRed-bs008.html +++ b/doc/pub/DimRed/html/._DimRed-bs008.html @@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('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')]} + ('Simple preprocessing examples, Franke function and regression', + 2, + None, + '___sec2'), + ('Simple preprocessing examples, breast cancer data and ' + 'classification', + 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')]} end of tocinfo --> @@ -94,15 +102,16 @@ MathJax.Hub.Config({ @@ -118,18 +127,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).

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

  • 10
  • 11
  • 12
  • +
  • 13
  • »
  • diff --git a/doc/pub/DimRed/html/._DimRed-bs009.html b/doc/pub/DimRed/html/._DimRed-bs009.html index f6dd448e3..dd7fef739 100644 --- a/doc/pub/DimRed/html/._DimRed-bs009.html +++ b/doc/pub/DimRed/html/._DimRed-bs009.html @@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('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')]} + ('Simple preprocessing examples, Franke function and regression', + 2, + None, + '___sec2'), + ('Simple preprocessing examples, breast cancer data and ' + 'classification', + 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')]} end of tocinfo --> @@ -94,15 +102,16 @@ MathJax.Hub.Config({ @@ -118,28 +127,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)
    -

    @@ -162,6 +157,7 @@ X_reduced = rbf_pca10
  • 11
  • 12
  • +
  • 13
  • »
  • diff --git a/doc/pub/DimRed/html/DimRed-bs.html b/doc/pub/DimRed/html/DimRed-bs.html index 57a5f55b6..84af171c4 100644 --- a/doc/pub/DimRed/html/DimRed-bs.html +++ b/doc/pub/DimRed/html/DimRed-bs.html @@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('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')]} + ('Simple preprocessing examples, Franke function and regression', + 2, + None, + '___sec2'), + ('Simple preprocessing examples, breast cancer data and ' + 'classification', + 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')]} end of tocinfo --> @@ -94,15 +102,16 @@ MathJax.Hub.Config({ @@ -137,7 +146,7 @@ MathJax.Hub.Config({
    [2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

    -

    Oct 12, 2019

    +

    Oct 14, 2019


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

  • 9
  • 10
  • ...
  • -
  • 12
  • +
  • 13
  • »
  • diff --git a/doc/pub/DimRed/html/DimRed-reveal.html b/doc/pub/DimRed/html/DimRed-reveal.html index 1cb7cc4d2..ba5ca09ac 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 12, 2019

    +

    Oct 14, 2019


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

    -

    Simple preprocessing examples

    +

    Simple preprocessing examples, Franke function and regression

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

    # Common imports
    +import os
    +import numpy as np
    +import pandas as pd
    +import matplotlib.pyplot as plt
    +import sklearn.linear_model as skl
    +from sklearn.metrics import mean_squared_error
    +from sklearn.model_selection import  train_test_split
    +from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer
    +from sklearn.svm import SVR
    +
    +# Where to save the figures and data files
    +PROJECT_ROOT_DIR = "Results"
    +FIGURE_ID = "Results/FigureFiles"
    +DATA_ID = "DataFiles/"
    +
    +if not os.path.exists(PROJECT_ROOT_DIR):
    +    os.mkdir(PROJECT_ROOT_DIR)
    +
    +if not os.path.exists(FIGURE_ID):
    +    os.makedirs(FIGURE_ID)
    +
    +if not os.path.exists(DATA_ID):
    +    os.makedirs(DATA_ID)
    +
    +def image_path(fig_id):
    +    return os.path.join(FIGURE_ID, fig_id)
    +
    +def data_path(dat_id):
    +    return os.path.join(DATA_ID, dat_id)
    +
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
    +
    +
    +def FrankeFunction(x,y):
    +	term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
    +	term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
    +	term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
    +	term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
    +	return term1 + term2 + term3 + term4
    +
    +
    +def create_X(x, y, n ):
    +	if len(x.shape) > 1:
    +		x = np.ravel(x)
    +		y = np.ravel(y)
    +
    +	N = len(x)
    +	l = int((n+1)*(n+2)/2)		# Number of elements in beta
    +	X = np.ones((N,l))
    +
    +	for i in range(1,n+1):
    +		q = int((i)*(i+1)/2)
    +		for k in range(i+1):
    +			X[:,q+k] = (x**(i-k))*(y**k)
    +
    +	return X
    +
    +
    +# Making meshgrid of datapoints and compute Franke's function
    +n = 5
    +N = 1000
    +x = np.sort(np.random.uniform(0, 1, N))
    +y = np.sort(np.random.uniform(0, 1, N))
    +z = FrankeFunction(x, y)
    +X = create_X(x, y, n=n)    
    +# split in training and test data
    +X_train, X_test, y_train, y_test = train_test_split(X,z,test_size=0.2)
    +
    +
    +svm = SVR(gamma='auto',C=10.0)
    +svm.fit(X_train, y_train)
    +
    +# The mean squared error and R2 score
    +print("MSE before scaling: {:.2f}".format(mean_squared_error(svm.predict(X_test), y_test)))
    +print("R2 score before scaling {:.2f}".format(svm.score(X_test,y_test)))
    +
    +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 after scaling:\n {}".format(X_train_scaled.min(axis=0)))
    +print("Feature max values after scaling:\n {}".format(X_train_scaled.max(axis=0)))
    +
    +svm = SVR(gamma='auto',C=10.0)
    +svm.fit(X_train_scaled, y_train)
    +
    +print("MSE after  scaling: {:.2f}".format(mean_squared_error(svm.predict(X_test_scaled), y_test)))
    +print("R2 score for  scaled data: {:.2f}".format(svm.score(X_test_scaled,y_test)))
    +
    +
    + + +
    +

    Simple preprocessing examples, breast cancer data and classification

    + +

    +We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification

    @@ -253,7 +356,7 @@ svm.fit(X_train_scaled, y_train)

    -

    Principal Component Analysis

    +

    Principal Component Analysis

    @@ -290,7 +393,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 @@ -321,7 +424,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 @@ -350,7 +453,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 @@ -360,7 +463,7 @@ instances arrive).
    -

    Randomized PCA

    +

    Randomized PCA

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

    -

    Kernel PCA

    +

    Kernel PCA

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

    -

    LLE

    +

    LLE

    Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction @@ -412,7 +515,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 599608524..829228a99 100644 --- a/doc/pub/DimRed/html/DimRed-solarized.html +++ b/doc/pub/DimRed/html/DimRed-solarized.html @@ -66,15 +66,23 @@ div { text-align: justify; text-justify: inter-word; } None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('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')]} + ('Simple preprocessing examples, Franke function and regression', + 2, + None, + '___sec2'), + ('Simple preprocessing examples, breast cancer data and ' + 'classification', + 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')]} end of tocinfo --> @@ -116,7 +124,7 @@ MathJax.Hub.Config({

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

    -

    Oct 12, 2019

    +

    Oct 14, 2019












    @@ -168,11 +176,113 @@ This scaling has the drawback that it does not ensure that we have a particular











    -

    Simple preprocessing examples

    +

    Simple preprocessing examples, Franke function and regression

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

    # Common imports
    +import os
    +import numpy as np
    +import pandas as pd
    +import matplotlib.pyplot as plt
    +import sklearn.linear_model as skl
    +from sklearn.metrics import mean_squared_error
    +from sklearn.model_selection import  train_test_split
    +from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer
    +from sklearn.svm import SVR
    +
    +# Where to save the figures and data files
    +PROJECT_ROOT_DIR = "Results"
    +FIGURE_ID = "Results/FigureFiles"
    +DATA_ID = "DataFiles/"
    +
    +if not os.path.exists(PROJECT_ROOT_DIR):
    +    os.mkdir(PROJECT_ROOT_DIR)
    +
    +if not os.path.exists(FIGURE_ID):
    +    os.makedirs(FIGURE_ID)
    +
    +if not os.path.exists(DATA_ID):
    +    os.makedirs(DATA_ID)
    +
    +def image_path(fig_id):
    +    return os.path.join(FIGURE_ID, fig_id)
    +
    +def data_path(dat_id):
    +    return os.path.join(DATA_ID, dat_id)
    +
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
    +
    +
    +def FrankeFunction(x,y):
    +	term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
    +	term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
    +	term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
    +	term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
    +	return term1 + term2 + term3 + term4
    +
    +
    +def create_X(x, y, n ):
    +	if len(x.shape) > 1:
    +		x = np.ravel(x)
    +		y = np.ravel(y)
    +
    +	N = len(x)
    +	l = int((n+1)*(n+2)/2)		# Number of elements in beta
    +	X = np.ones((N,l))
    +
    +	for i in range(1,n+1):
    +		q = int((i)*(i+1)/2)
    +		for k in range(i+1):
    +			X[:,q+k] = (x**(i-k))*(y**k)
    +
    +	return X
    +
    +
    +# Making meshgrid of datapoints and compute Franke's function
    +n = 5
    +N = 1000
    +x = np.sort(np.random.uniform(0, 1, N))
    +y = np.sort(np.random.uniform(0, 1, N))
    +z = FrankeFunction(x, y)
    +X = create_X(x, y, n=n)    
    +# split in training and test data
    +X_train, X_test, y_train, y_test = train_test_split(X,z,test_size=0.2)
    +
    +
    +svm = SVR(gamma='auto',C=10.0)
    +svm.fit(X_train, y_train)
    +
    +# The mean squared error and R2 score
    +print("MSE before scaling: {:.2f}".format(mean_squared_error(svm.predict(X_test), y_test)))
    +print("R2 score before scaling {:.2f}".format(svm.score(X_test,y_test)))
    +
    +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 after scaling:\n {}".format(X_train_scaled.min(axis=0)))
    +print("Feature max values after scaling:\n {}".format(X_train_scaled.max(axis=0)))
    +
    +svm = SVR(gamma='auto',C=10.0)
    +svm.fit(X_train_scaled, y_train)
    +
    +print("MSE after  scaling: {:.2f}".format(mean_squared_error(svm.predict(X_test_scaled), y_test)))
    +print("R2 score for  scaled data: {:.2f}".format(svm.score(X_test_scaled,y_test)))
    +
    +

    +









    + +

    Simple preprocessing examples, breast cancer data and classification

    + +

    +We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification

    @@ -220,7 +330,7 @@ svm.fit(X_train_scaled, y_train)











    -

    Principal Component Analysis

    +

    Principal Component Analysis

    @@ -256,7 +366,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 @@ -287,7 +397,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 @@ -315,7 +425,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 @@ -325,7 +435,7 @@ instances arrive).











    -

    Randomized PCA

    +

    Randomized PCA

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











    -

    Kernel PCA

    +

    Kernel PCA

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











    -

    LLE

    +

    LLE

    Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction @@ -381,7 +491,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 98ec982c9..421408fe5 100644 --- a/doc/pub/DimRed/html/DimRed.html +++ b/doc/pub/DimRed/html/DimRed.html @@ -71,15 +71,23 @@ div { text-align: justify; text-justify: inter-word; } None, '___sec0'), ('Preprocessing our data', 2, None, '___sec1'), - ('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')]} + ('Simple preprocessing examples, Franke function and regression', + 2, + None, + '___sec2'), + ('Simple preprocessing examples, breast cancer data and ' + 'classification', + 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')]} end of tocinfo --> @@ -121,7 +129,7 @@ MathJax.Hub.Config({

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

    -

    Oct 12, 2019

    +

    Oct 14, 2019












    @@ -173,11 +181,113 @@ This scaling has the drawback that it does not ensure that we have a particular











    -

    Simple preprocessing examples

    +

    Simple preprocessing examples, Franke function and regression

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

    # Common imports
    +import os
    +import numpy as np
    +import pandas as pd
    +import matplotlib.pyplot as plt
    +import sklearn.linear_model as skl
    +from sklearn.metrics import mean_squared_error
    +from sklearn.model_selection import  train_test_split
    +from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer
    +from sklearn.svm import SVR
    +
    +# Where to save the figures and data files
    +PROJECT_ROOT_DIR = "Results"
    +FIGURE_ID = "Results/FigureFiles"
    +DATA_ID = "DataFiles/"
    +
    +if not os.path.exists(PROJECT_ROOT_DIR):
    +    os.mkdir(PROJECT_ROOT_DIR)
    +
    +if not os.path.exists(FIGURE_ID):
    +    os.makedirs(FIGURE_ID)
    +
    +if not os.path.exists(DATA_ID):
    +    os.makedirs(DATA_ID)
    +
    +def image_path(fig_id):
    +    return os.path.join(FIGURE_ID, fig_id)
    +
    +def data_path(dat_id):
    +    return os.path.join(DATA_ID, dat_id)
    +
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
    +
    +
    +def FrankeFunction(x,y):
    +	term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
    +	term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
    +	term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
    +	term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
    +	return term1 + term2 + term3 + term4
    +
    +
    +def create_X(x, y, n ):
    +	if len(x.shape) > 1:
    +		x = np.ravel(x)
    +		y = np.ravel(y)
    +
    +	N = len(x)
    +	l = int((n+1)*(n+2)/2)		# Number of elements in beta
    +	X = np.ones((N,l))
    +
    +	for i in range(1,n+1):
    +		q = int((i)*(i+1)/2)
    +		for k in range(i+1):
    +			X[:,q+k] = (x**(i-k))*(y**k)
    +
    +	return X
    +
    +
    +# Making meshgrid of datapoints and compute Franke's function
    +n = 5
    +N = 1000
    +x = np.sort(np.random.uniform(0, 1, N))
    +y = np.sort(np.random.uniform(0, 1, N))
    +z = FrankeFunction(x, y)
    +X = create_X(x, y, n=n)    
    +# split in training and test data
    +X_train, X_test, y_train, y_test = train_test_split(X,z,test_size=0.2)
    +
    +
    +svm = SVR(gamma='auto',C=10.0)
    +svm.fit(X_train, y_train)
    +
    +# The mean squared error and R2 score
    +print("MSE before scaling: {:.2f}".format(mean_squared_error(svm.predict(X_test), y_test)))
    +print("R2 score before scaling {:.2f}".format(svm.score(X_test,y_test)))
    +
    +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 after scaling:\n {}".format(X_train_scaled.min(axis=0)))
    +print("Feature max values after scaling:\n {}".format(X_train_scaled.max(axis=0)))
    +
    +svm = SVR(gamma='auto',C=10.0)
    +svm.fit(X_train_scaled, y_train)
    +
    +print("MSE after  scaling: {:.2f}".format(mean_squared_error(svm.predict(X_test_scaled), y_test)))
    +print("R2 score for  scaled data: {:.2f}".format(svm.score(X_test_scaled,y_test)))
    +
    +

    +









    + +

    Simple preprocessing examples, breast cancer data and classification

    + +

    +We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification

    @@ -225,7 +335,7 @@ svm.fit(X_train_scaled, y_train)











    -

    Principal Component Analysis

    +

    Principal Component Analysis

    @@ -261,7 +371,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 @@ -292,7 +402,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 @@ -320,7 +430,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 @@ -330,7 +440,7 @@ instances arrive).











    -

    Randomized PCA

    +

    Randomized PCA

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











    -

    Kernel PCA

    +

    Kernel PCA

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

    LLE

    Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction @@ -386,7 +496,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 b9620502d..370f5479a 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 12, 2019**\n", + "Date: **Oct 14, 2019**\n", "\n", "Copyright 1999-2019, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n", "\n", @@ -45,11 +45,7 @@ "\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" + "## Simple preprocessing examples, Franke function and regression" ] }, { @@ -62,6 +58,119 @@ "source": [ "%matplotlib inline\n", "\n", + "# Common imports\n", + "import os\n", + "import numpy as np\n", + "import pandas as pd\n", + "import matplotlib.pyplot as plt\n", + "import sklearn.linear_model as skl\n", + "from sklearn.metrics import mean_squared_error\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer\n", + "from sklearn.svm import SVR\n", + "\n", + "# Where to save the figures and data files\n", + "PROJECT_ROOT_DIR = \"Results\"\n", + "FIGURE_ID = \"Results/FigureFiles\"\n", + "DATA_ID = \"DataFiles/\"\n", + "\n", + "if not os.path.exists(PROJECT_ROOT_DIR):\n", + " os.mkdir(PROJECT_ROOT_DIR)\n", + "\n", + "if not os.path.exists(FIGURE_ID):\n", + " os.makedirs(FIGURE_ID)\n", + "\n", + "if not os.path.exists(DATA_ID):\n", + " os.makedirs(DATA_ID)\n", + "\n", + "def image_path(fig_id):\n", + " return os.path.join(FIGURE_ID, fig_id)\n", + "\n", + "def data_path(dat_id):\n", + " return os.path.join(DATA_ID, dat_id)\n", + "\n", + "def save_fig(fig_id):\n", + " plt.savefig(image_path(fig_id) + \".png\", format='png')\n", + "\n", + "\n", + "def FrankeFunction(x,y):\n", + "\tterm1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))\n", + "\tterm2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))\n", + "\tterm3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))\n", + "\tterm4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)\n", + "\treturn term1 + term2 + term3 + term4\n", + "\n", + "\n", + "def create_X(x, y, n ):\n", + "\tif len(x.shape) > 1:\n", + "\t\tx = np.ravel(x)\n", + "\t\ty = np.ravel(y)\n", + "\n", + "\tN = len(x)\n", + "\tl = int((n+1)*(n+2)/2)\t\t# Number of elements in beta\n", + "\tX = np.ones((N,l))\n", + "\n", + "\tfor i in range(1,n+1):\n", + "\t\tq = int((i)*(i+1)/2)\n", + "\t\tfor k in range(i+1):\n", + "\t\t\tX[:,q+k] = (x**(i-k))*(y**k)\n", + "\n", + "\treturn X\n", + "\n", + "\n", + "# Making meshgrid of datapoints and compute Franke's function\n", + "n = 5\n", + "N = 1000\n", + "x = np.sort(np.random.uniform(0, 1, N))\n", + "y = np.sort(np.random.uniform(0, 1, N))\n", + "z = FrankeFunction(x, y)\n", + "X = create_X(x, y, n=n) \n", + "# split in training and test data\n", + "X_train, X_test, y_train, y_test = train_test_split(X,z,test_size=0.2)\n", + "\n", + "\n", + "svm = SVR(gamma='auto',C=10.0)\n", + "svm.fit(X_train, y_train)\n", + "\n", + "# The mean squared error and R2 score\n", + "print(\"MSE before scaling: {:.2f}\".format(mean_squared_error(svm.predict(X_test), y_test)))\n", + "print(\"R2 score before scaling {:.2f}\".format(svm.score(X_test,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", + "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 after scaling:\\n {}\".format(X_train_scaled.min(axis=0)))\n", + "print(\"Feature max values after scaling:\\n {}\".format(X_train_scaled.max(axis=0)))\n", + "\n", + "svm = SVR(gamma='auto',C=10.0)\n", + "svm.fit(X_train_scaled, y_train)\n", + "\n", + "print(\"MSE after scaling: {:.2f}\".format(mean_squared_error(svm.predict(X_test_scaled), y_test)))\n", + "print(\"R2 score for scaled data: {:.2f}\".format(svm.score(X_test_scaled,y_test)))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Simple preprocessing examples, breast cancer data and classification\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" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "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", @@ -117,7 +226,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -144,7 +253,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -168,7 +277,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -190,7 +299,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -219,7 +328,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": { "collapsed": false }, @@ -242,7 +351,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": { "collapsed": false }, @@ -288,7 +397,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "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 c7ace3921..25c4809a5 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 aafa544a7..6a752715d 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 6394aab14..052f91f72 100644 --- a/doc/src/DimRed/DimRed.do.txt +++ b/doc/src/DimRed/DimRed.do.txt @@ -35,12 +35,114 @@ This scaling has the drawback that it does not ensure that we have a particular !eblock +!split +===== Simple preprocessing examples, Franke function and regression ===== + +!bc pycod +# Common imports +import os +import numpy as np +import pandas as pd +import matplotlib.pyplot as plt +import sklearn.linear_model as skl +from sklearn.metrics import mean_squared_error +from sklearn.model_selection import train_test_split +from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer +from sklearn.svm import SVR + +# Where to save the figures and data files +PROJECT_ROOT_DIR = "Results" +FIGURE_ID = "Results/FigureFiles" +DATA_ID = "DataFiles/" + +if not os.path.exists(PROJECT_ROOT_DIR): + os.mkdir(PROJECT_ROOT_DIR) + +if not os.path.exists(FIGURE_ID): + os.makedirs(FIGURE_ID) + +if not os.path.exists(DATA_ID): + os.makedirs(DATA_ID) + +def image_path(fig_id): + return os.path.join(FIGURE_ID, fig_id) + +def data_path(dat_id): + return os.path.join(DATA_ID, dat_id) + +def save_fig(fig_id): + plt.savefig(image_path(fig_id) + ".png", format='png') + + +def FrankeFunction(x,y): + term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2)) + term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1)) + term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2)) + term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2) + return term1 + term2 + term3 + term4 + + +def create_X(x, y, n ): + if len(x.shape) > 1: + x = np.ravel(x) + y = np.ravel(y) + + N = len(x) + l = int((n+1)*(n+2)/2) # Number of elements in beta + X = np.ones((N,l)) + + for i in range(1,n+1): + q = int((i)*(i+1)/2) + for k in range(i+1): + X[:,q+k] = (x**(i-k))*(y**k) + + return X + + +# Making meshgrid of datapoints and compute Franke's function +n = 5 +N = 1000 +x = np.sort(np.random.uniform(0, 1, N)) +y = np.sort(np.random.uniform(0, 1, N)) +z = FrankeFunction(x, y) +X = create_X(x, y, n=n) +# split in training and test data +X_train, X_test, y_train, y_test = train_test_split(X,z,test_size=0.2) + + +svm = SVR(gamma='auto',C=10.0) +svm.fit(X_train, y_train) + +# The mean squared error and R2 score +print("MSE before scaling: {:.2f}".format(mean_squared_error(svm.predict(X_test), y_test))) +print("R2 score before scaling {:.2f}".format(svm.score(X_test,y_test))) + +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 after scaling:\n {}".format(X_train_scaled.min(axis=0))) +print("Feature max values after scaling:\n {}".format(X_train_scaled.max(axis=0))) + +svm = SVR(gamma='auto',C=10.0) +svm.fit(X_train_scaled, y_train) + +print("MSE after scaling: {:.2f}".format(mean_squared_error(svm.predict(X_test_scaled), y_test))) +print("R2 score for scaled data: {:.2f}".format(svm.score(X_test_scaled,y_test))) + +!ec + + !split -===== Simple preprocessing examples ===== +===== Simple preprocessing examples, breast cancer data and classification ===== + +We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification -We show here how we can use a simple regression case (our nuclear binding energies discussed earlier). -Rescaling our data with different !bc pycod import matplotlib.pyplot as plt diff --git a/doc/src/Regression/franke.py b/doc/src/Regression/franke.py index 0cd5b2475..fb5afbd38 100644 --- a/doc/src/Regression/franke.py +++ b/doc/src/Regression/franke.py @@ -7,6 +7,7 @@ import matplotlib.pyplot as plt import sklearn.linear_model as skl from sklearn.metrics import mean_squared_error from sklearn.model_selection import train_test_split +from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer from sklearn.svm import SVR # Where to save the figures and data files @@ -41,7 +42,7 @@ def FrankeFunction(x,y): return term1 + term2 + term3 + term4 -def create_X(x, y, n = 5): +def create_X(x, y, n ): if len(x.shape) > 1: x = np.ravel(x) y = np.ravel(y) @@ -71,14 +72,11 @@ X_train, X_test, y_train, y_test = train_test_split(X,z,test_size=0.2) svm = SVR(gamma='auto',C=10.0) svm.fit(X_train, y_train) -# The mean squared error -print("Test set accuracy: {:.2f}".format(svm.score(X_test,y_test))) +# The mean squared error and R2 score +print("MSE before scaling: {:.2f}".format(mean_squared_error(svm.predict(X_test), y_test))) +print("R2 score before scaling {:.2f}".format(svm.score(X_test,y_test))) - - -from sklearn.preprocessing import MinMaxScaler, StandardScaler - scaler = StandardScaler() scaler.fit(X_train) X_train_scaled = scaler.transform(X_train) @@ -95,7 +93,7 @@ print("Feature max values after scaling:\n {}".format(X_train_scaled.max(axis=0) svm = SVR(gamma='auto',C=10.0) svm.fit(X_train_scaled, y_train) - +print("MSE after scaling: {:.2f}".format(mean_squared_error(svm.predict(X_test_scaled), y_test))) print("Test set accuracy scaled data: {:.2f}".format(svm.score(X_test_scaled,y_test)))