dim red with new examples
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user