class LogLGBM(LGBMRegressor): def __init__(self, target=None, **kwargs): super().__init__(**kwargs) if target == "Oil_norm": self.target_scaler = PowerTransformer(method='box-cox', standardize=False) elif target == 'Gas_norm': self.target_scaler = FunctionTransformer(func=np.log1p, inverse_func=np.expm1) elif target == 'Water_norm': self.target_scaler = FunctionTransformer(func=np.log1p, inverse_func=np.expm1) def fit(self, X, Y, **kwargs): # y_train = np.log1p(Y) self.target_scaler.fit(Y.values.reshape(-1, 1) + 1) y_train = pd.Series( self.target_scaler.transform(Y.values.reshape(-1, 1) + 1).reshape( -1, )) super(LogLGBM, self).fit(X, y_train, **kwargs) return self def predict(self, X): preds = super(LogLGBM, self).predict(X).reshape(-1, 1) preds = self.target_scaler.inverse_transform(preds) - 1 return preds[:, 0]
class DataTransfomer: """A class to transform data based on user-defined function to get predicted outcomes. This class calls FunctionTransformer of scikit-learn internally (https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.FunctionTransformer.html).""" def __init__(self, func=None, kw_args=None): self.func = func self.kw_args = kw_args def feed_data_params(self, data_interface): if self.kw_args is not None: self.kw_args['data_interface'] = data_interface else: self.kw_args = {'data_interface': data_interface} def initialize_transform_func(self): if self.func == 'ohe-min-max': self.data_transformer = FunctionTransformer(func=ohe_min_max_transformation, kw_args=self.kw_args, validate=False) elif self.func is None: # identity transformation # add more ready-to-use transformers (such as label-encoding) in elif loops. self.data_transformer = FunctionTransformer(func=self.func, kw_args=None, validate=False) else: # add more ready-to-use transformers (such as label-encoding) in elif loops. self.data_transformer = FunctionTransformer(func=self.func, kw_args=self.kw_args, validate=False) def transform(self, data): return self.data_transformer.transform(data) # should return a numpy array def inverse_transform(self, data): return self.data_transformer.inverse_transform(data) # should return a numpy array
def test_inverse_transform(): X = np.array([1, 4, 9, 16]).reshape((2, 2)) # Test that inverse_transform works correctly F = FunctionTransformer(func=np.sqrt, inverse_func=np.around, inv_kw_args=dict(decimals=3)) testing.assert_array_equal(F.inverse_transform(F.transform(X)), np.around(np.sqrt(X), decimals=3))
def test_inverse_transform(): X = np.array([1, 4, 9, 16]).reshape((2, 2)) # Test that inverse_transform works correctly F = FunctionTransformer( func=np.sqrt, inverse_func=np.around, inv_kw_args=dict(decimals=3)) testing.assert_array_equal( F.inverse_transform(F.transform(X)), np.around(np.sqrt(X), decimals=3))
def test_check_inverse(): X_dense = np.array([1, 4, 9, 16], dtype=np.float64).reshape((2, 2)) X_list = [X_dense, sparse.csr_matrix(X_dense), sparse.csc_matrix(X_dense)] for X in X_list: if sparse.issparse(X): accept_sparse = True else: accept_sparse = False trans = FunctionTransformer( func=np.sqrt, inverse_func=np.around, accept_sparse=accept_sparse, check_inverse=True, validate=True, ) warning_message = ( "The provided functions are not strictly" " inverse of each other. If you are sure you" " want to proceed regardless, set" " 'check_inverse=False'." ) with pytest.warns(UserWarning, match=warning_message): trans.fit(X) trans = FunctionTransformer( func=np.expm1, inverse_func=np.log1p, accept_sparse=accept_sparse, check_inverse=True, validate=True, ) with warnings.catch_warnings(): warnings.simplefilter("error", UserWarning) Xt = trans.fit_transform(X) assert_allclose_dense_sparse(X, trans.inverse_transform(Xt)) # check that we don't check inverse when one of the func or inverse is not # provided. trans = FunctionTransformer( func=np.expm1, inverse_func=None, check_inverse=True, validate=True ) with warnings.catch_warnings(): warnings.simplefilter("error", UserWarning) trans.fit(X_dense) trans = FunctionTransformer( func=None, inverse_func=np.expm1, check_inverse=True, validate=True ) with warnings.catch_warnings(): warnings.simplefilter("error", UserWarning) trans.fit(X_dense)
def test_function_transformer_validate_inverse(): """Test that function transformer does not reset estimator in `inverse_transform`.""" def add_constant_feature(X): X_one = np.ones((X.shape[0], 1)) return np.concatenate((X, X_one), axis=1) def inverse_add_constant(X): return X[:, :-1] X = np.array([[1, 2], [3, 4], [3, 4]]) trans = FunctionTransformer( func=add_constant_feature, inverse_func=inverse_add_constant, validate=True, ) X_trans = trans.fit_transform(X) assert trans.n_features_in_ == X.shape[1] trans.inverse_transform(X_trans) assert trans.n_features_in_ == X.shape[1]
def test_check_inverse(): X_dense = np.array([1, 4, 9, 16], dtype=np.float64).reshape((2, 2)) X_list = [X_dense, sparse.csr_matrix(X_dense), sparse.csc_matrix(X_dense)] for X in X_list: if sparse.issparse(X): accept_sparse = True else: accept_sparse = False trans = FunctionTransformer(func=np.sqrt, inverse_func=np.around, accept_sparse=accept_sparse, check_inverse=True, validate=True) assert_warns_message(UserWarning, "The provided functions are not strictly" " inverse of each other. If you are sure you" " want to proceed regardless, set" " 'check_inverse=False'.", trans.fit, X) trans = FunctionTransformer(func=np.expm1, inverse_func=np.log1p, accept_sparse=accept_sparse, check_inverse=True, validate=True) Xt = assert_no_warnings(trans.fit_transform, X) assert_allclose_dense_sparse(X, trans.inverse_transform(Xt)) # check that we don't check inverse when one of the func or inverse is not # provided. trans = FunctionTransformer(func=np.expm1, inverse_func=None, check_inverse=True, validate=True) assert_no_warnings(trans.fit, X_dense) trans = FunctionTransformer(func=None, inverse_func=np.expm1, check_inverse=True, validate=True) assert_no_warnings(trans.fit, X_dense)
def test_check_inverse(): X_dense = np.array([1, 4, 9, 16], dtype=np.float64).reshape((2, 2)) X_list = [X_dense, sparse.csr_matrix(X_dense), sparse.csc_matrix(X_dense)] for X in X_list: if sparse.issparse(X): accept_sparse = True else: accept_sparse = False trans = FunctionTransformer(func=np.sqrt, inverse_func=np.around, accept_sparse=accept_sparse, check_inverse=True) assert_warns_message( UserWarning, "The provided functions are not strictly" " inverse of each other. If you are sure you" " want to proceed regardless, set" " 'check_inverse=False'.", trans.fit, X) trans = FunctionTransformer(func=np.expm1, inverse_func=np.log1p, accept_sparse=accept_sparse, check_inverse=True) Xt = assert_no_warnings(trans.fit_transform, X) assert_allclose_dense_sparse(X, trans.inverse_transform(Xt)) # check that we don't check inverse when one of the func or inverse is not # provided. trans = FunctionTransformer(func=np.expm1, inverse_func=None, check_inverse=True) assert_no_warnings(trans.fit, X_dense) trans = FunctionTransformer(func=None, inverse_func=np.expm1, check_inverse=True) assert_no_warnings(trans.fit, X_dense)
class TransformedTargetRegressor(RegressorMixin, BaseEstimator): """Meta-estimator to regress on a transformed target. Useful for applying a non-linear transformation to the target ``y`` in regression problems. This transformation can be given as a Transformer such as the QuantileTransformer or as a function and its inverse such as ``log`` and ``exp``. The computation during ``fit`` is:: regressor.fit(X, func(y)) or:: regressor.fit(X, transformer.transform(y)) The computation during ``predict`` is:: inverse_func(regressor.predict(X)) or:: transformer.inverse_transform(regressor.predict(X)) Read more in the :ref:`User Guide <transformed_target_regressor>`. .. versionadded:: 0.20 Parameters ---------- regressor : object, default=None Regressor object such as derived from ``RegressorMixin``. This regressor will automatically be cloned each time prior to fitting. If regressor is ``None``, ``LinearRegression()`` is created and used. transformer : object, default=None Estimator object such as derived from ``TransformerMixin``. Cannot be set at the same time as ``func`` and ``inverse_func``. If ``transformer`` is ``None`` as well as ``func`` and ``inverse_func``, the transformer will be an identity transformer. Note that the transformer will be cloned during fitting. Also, the transformer is restricting ``y`` to be a numpy array. func : function, default=None Function to apply to ``y`` before passing to ``fit``. Cannot be set at the same time as ``transformer``. The function needs to return a 2-dimensional array. If ``func`` is ``None``, the function used will be the identity function. inverse_func : function, default=None Function to apply to the prediction of the regressor. Cannot be set at the same time as ``transformer`` as well. The function needs to return a 2-dimensional array. The inverse function is used to return predictions to the same space of the original training labels. check_inverse : bool, default=True Whether to check that ``transform`` followed by ``inverse_transform`` or ``func`` followed by ``inverse_func`` leads to the original targets. Attributes ---------- regressor_ : object Fitted regressor. transformer_ : object Transformer used in ``fit`` and ``predict``. Examples -------- >>> import numpy as np >>> from sklearn.linear_model import LinearRegression >>> from sklearn.compose import TransformedTargetRegressor >>> tt = TransformedTargetRegressor(regressor=LinearRegression(), ... func=np.log, inverse_func=np.exp) >>> X = np.arange(4).reshape(-1, 1) >>> y = np.exp(2 * X).ravel() >>> tt.fit(X, y) TransformedTargetRegressor(...) >>> tt.score(X, y) 1.0 >>> tt.regressor_.coef_ array([2.]) Notes ----- Internally, the target ``y`` is always converted into a 2-dimensional array to be used by scikit-learn transformers. At the time of prediction, the output will be reshaped to a have the same number of dimensions as ``y``. See :ref:`examples/compose/plot_transformed_target.py <sphx_glr_auto_examples_compose_plot_transformed_target.py>`. """ @_deprecate_positional_args def __init__(self, regressor=None, *, transformer=None, func=None, inverse_func=None, check_inverse=True): self.regressor = regressor self.transformer = transformer self.func = func self.inverse_func = inverse_func self.check_inverse = check_inverse def _fit_transformer(self, y): """Check transformer and fit transformer. Create the default transformer, fit it and make additional inverse check on a subset (optional). """ if (self.transformer is not None and (self.func is not None or self.inverse_func is not None)): raise ValueError("'transformer' and functions 'func'/" "'inverse_func' cannot both be set.") elif self.transformer is not None: self.transformer_ = clone(self.transformer) else: if self.func is not None and self.inverse_func is None: raise ValueError("When 'func' is provided, 'inverse_func' must" " also be provided") self.transformer_ = FunctionTransformer( func=self.func, inverse_func=self.inverse_func, validate=True, check_inverse=self.check_inverse) # XXX: sample_weight is not currently passed to the # transformer. However, if transformer starts using sample_weight, the # code should be modified accordingly. At the time to consider the # sample_prop feature, it is also a good use case to be considered. self.transformer_.fit(y) if self.check_inverse: idx_selected = slice(None, None, max(1, y.shape[0] // 10)) y_sel = _safe_indexing(y, idx_selected) y_sel_t = self.transformer_.transform(y_sel) if not np.allclose(y_sel, self.transformer_.inverse_transform(y_sel_t)): warnings.warn("The provided functions or transformer are" " not strictly inverse of each other. If" " you are sure you want to proceed regardless" ", set 'check_inverse=False'", UserWarning) def fit(self, X, y, **fit_params): """Fit the model according to the given training data. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features) Training vector, where n_samples is the number of samples and n_features is the number of features. y : array-like of shape (n_samples,) Target values. **fit_params : dict Parameters passed to the ``fit`` method of the underlying regressor. Returns ------- self : object """ y = check_array(y, accept_sparse=False, force_all_finite=True, allow_nd=True, ensure_2d=False, dtype='numeric') # store the number of dimension of the target to predict an array of # similar shape at predict self._training_dim = y.ndim # transformers are designed to modify X which is 2d dimensional, we # need to modify y accordingly. if y.ndim == 1: y_2d = y.reshape(-1, 1) else: y_2d = y self._fit_transformer(y_2d) # transform y and convert back to 1d array if needed y_trans = self.transformer_.transform(y_2d) # FIXME: a FunctionTransformer can return a 1D array even when validate # is set to True. Therefore, we need to check the number of dimension # first. if y_trans.ndim == 2 and y_trans.shape[1] == 1: y_trans = y_trans.squeeze(axis=1) if self.regressor is None: from ..linear_model import LinearRegression self.regressor_ = LinearRegression() else: self.regressor_ = clone(self.regressor) self.regressor_.fit(X, y_trans, **fit_params) return self def predict(self, X): """Predict using the base regressor, applying inverse. The regressor is used to predict and the ``inverse_func`` or ``inverse_transform`` is applied before returning the prediction. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features) Samples. Returns ------- y_hat : ndarray of shape (n_samples,) Predicted values. """ check_is_fitted(self) pred = self.regressor_.predict(X) if pred.ndim == 1: pred_trans = self.transformer_.inverse_transform( pred.reshape(-1, 1)) else: pred_trans = self.transformer_.inverse_transform(pred) if (self._training_dim == 1 and pred_trans.ndim == 2 and pred_trans.shape[1] == 1): pred_trans = pred_trans.squeeze(axis=1) return pred_trans def _more_tags(self): return {'poor_score': True, 'no_validation': True} @property def n_features_in_(self): # For consistency with other estimators we raise a AttributeError so # that hasattr() returns False the estimator isn't fitted. try: check_is_fitted(self) except NotFittedError as nfe: raise AttributeError( "{} object has no n_features_in_ attribute." .format(self.__class__.__name__) ) from nfe return self.regressor_.n_features_in_
y_train = Y[:split] X_test = data_t1[split:, :] y_test = Y[split:] #fit SVR clf = SVR(C=1000, epsilon=0.01, gamma=0.01) clf.fit(X_train, y_train) #reshaped_X_test_norm = X_test_norm[:,0].reshape(-1,1) #predict pred_test = clf.predict(X_test) #denormalisation predicted test y #if scaled, use scalar, if transformed, use transformer inv_pred_test = np.column_stack([pred_test, X_test[:, 1:]]) inv_pred_test = transformer.inverse_transform(inv_pred_test) inv_pred_test = inv_pred_test[:, 0] #denormalisation actual test y #if scaled, use scalar, if transformed, use transformer inv_y_test = np.column_stack([y_test, X_test[:, 1:]]) inv_y_test = transformer.inverse_transform(inv_y_test) inv_y_test = inv_y_test[:, 0] #scores err = mean_absolute_error(inv_y_test, inv_pred_test) print('Test MAE: %.3f' % err) with open('B SVR simple zsc poly.txt', 'w') as f: print('Test MAE: %.3f' % error, file=f) #r2_score
def predict_missing_price(preprocessed_data, one_hot=False): test_index = preprocessed_data.price != preprocessed_data.price feature_columns = [ i for i in preprocessed_data.columns if i not in ['class_id', 'price'] ] y_column = ['price'] testX = preprocessed_data.loc[test_index, feature_columns].values trainX = preprocessed_data.loc[(1 - test_index).astype(bool), feature_columns].values trainY = preprocessed_data.loc[(1 - test_index).astype(bool), y_column].values # plt.hist(trainY) # plt.show() # 销量数据使用log1p处理后更接近正态分布,比sqrt处理要好 # trs = FunctionTransformer(func=np.sqrt, inverse_func=np.square) trs = FunctionTransformer(func=np.log1p, inverse_func=np.expm1) scaler = MinMaxScaler() trainX = scaler.fit_transform(trainX) trainY = trs.fit_transform(np.reshape(trainY, (-1, 1))) # plt.hist(trainY) # plt.show() print(trainX.shape, trainY.shape) clf = xgb.XGBRegressor(seed=12) if one_hot: # ONE HOT with norm PARAMS sqare grid = [ { 'booster': ['gbtree'], 'learning_rate': [0.1], # 'min_child_weight':[], 'max_depth': [2], 'gamma': [1], 'subsample': [0.3], 'colsample_bytree': [0.3], 'reg_alpha': [1.0], 'reg_lambda': [0.85], 'scale_pos_weight': [1] }, ] else: # no one hot PARAMS sqrt # grid = [{ # 'booster': ['gbtree'], # 'learning_rate': [0.1], # # 'min_child_weight':[], # 'max_depth': [2], # 'gamma': [0.7], # 'subsample': [0.1], # 'colsample_bytree': [0.3], # 'reg_alpha': [0.5], # 'reg_lambda': [0.3], # 'scale_pos_weight': [1] # }, # ] # no one hot PARAMS log1p grid = [ { 'booster': ['gbtree'], 'learning_rate': [0.25], # 'min_child_weight':[], 'max_depth': [2], 'gamma': [0.09], 'subsample': [0.1], 'colsample_bytree': [0.95], 'reg_alpha': [0.5], 'reg_lambda': [0.25], 'scale_pos_weight': [1] }, ] gridCV = GridSearchCV(estimator=clf, param_grid=grid, scoring=make_scorer(_scorer, greater_is_better=False), iid=False, n_jobs=-1, cv=6, verbose=1) gridCV.fit(trainX, trainY) print("best params:", gridCV.best_params_) print('best score:', gridCV.best_score_) testX = scaler.transform(testX) predY = np.reshape(gridCV.predict(testX), (-1, 1)) preprocessed_data.loc[test_index, y_column] = trs.inverse_transform(predY) return preprocessed_data
class NNPredictorNumerical: def __init__(self, numerical_features, data, **kwargs): self.numerical_features = numerical_features self.scaler = StandardScaler() self.target_scaler = FunctionTransformer(func=np.log1p,inverse_func=np.expm1()) self.model = None self.data = data self.inputs = [] self.build_full_network(**kwargs) def build_full_network(self, optimizer=SGD(lr=0.001)): # Create the categorical embeddings first: input_num = Input(shape=(len(self.numerical_features),)) dense_num = Dense(256, activation="relu")(input_num) m = Dropout(rate=0.2)(dense_num) dense_num = Dense(128, activation="relu")(m) m = Dropout(rate=0.2)(dense_num) dense_num = Dense(64, activation="relu")(m) m = Dense(16, activation="relu")(dense_num) m = Dropout(rate=0.2)(m) m = Dense(8, activation="relu")(m) m = Dropout(rate=0.2)(m) m = Dense(4, activation="relu")(m) output = Dense(1, activation="linear")(m) model = Model(input_num, output) model.compile(loss="mae", optimizer=optimizer) self.model = model def fit(self, x, y, **kwargs): y = y.reshape(-1, 1) self.target_scaler.fit(y) y = self.target_scaler.transform(y) self.model.fit(x, y, **kwargs) def predict(self, x, **kwargs): y = self.model.predict(x, **kwargs) y = self.target_scaler.inverse_transform(y) return y def preprocess_data(self, X_train, X_val, X_test): input_list_train = [] input_list_val = [] input_list_test = [] for c in self.numerical_features: mu = np.nanmean(X_train[c]) X_train[c] = X_train[c].fillna(mu) X_test[c] = X_test[c].fillna(mu) X_val[c] = X_val[c].fillna(mu) # Fit scaler self.scaler.fit(X_train[self.numerical_features]) X_train[self.numerical_features] = self.scaler.transform( X_train[self.numerical_features] ) X_test[self.numerical_features] = self.scaler.transform( X_test[self.numerical_features] ) X_val[self.numerical_features] = self.scaler.transform( X_val[self.numerical_features] ) input_list_train.append(X_train[self.numerical_features].values) input_list_val.append(X_val[self.numerical_features].values) input_list_test.append(X_test[self.numerical_features].values) return input_list_train, input_list_val, input_list_test
class AdvancedTransformedTargetRegressor(TransformedTargetRegressor): """Expand :class:`sklearn.compose.TransformedTargetRegressor`.""" @property def coef_(self): """numpy.ndarray: Model coefficients.""" return self.regressor_.coef_ @property def feature_importances_(self): """numpy.ndarray: Feature importances.""" return self.regressor_.feature_importances_ def fit(self, x_data, y_data, **fit_kwargs): """Expand :meth:`fit` to accept kwargs.""" (y_2d, regressor_kwargs) = self.fit_transformer_only(y_data, **fit_kwargs) # Transform y and convert back to 1d array if necessary y_trans = self.transformer_.transform(y_2d) if y_trans.ndim == 2 and y_trans.shape[1] == 1: y_trans = y_trans.squeeze(axis=1) # Perform linear regression if regressor is not given if self.regressor is None: self.regressor_ = LinearRegression() else: self.regressor_ = clone(self.regressor) # Fit regressor with kwargs self.regressor_.fit(x_data, y_trans, **regressor_kwargs) return self def fit_transformer_only(self, y_data, **fit_kwargs): """Fit only ``transformer`` step.""" y_data = check_array(y_data, accept_sparse=False, force_all_finite=True, ensure_2d=False, dtype='numeric') self._training_dim = y_data.ndim # Process kwargs (_, regressor_kwargs) = self._get_fit_params(fit_kwargs) # Transformers are designed to modify X which is 2D, modify y_data # FIXME: Transformer does NOT use transformer_kwargs if y_data.ndim == 1: y_2d = y_data.reshape(-1, 1) else: y_2d = y_data self._fit_transformer(y_2d) return (y_2d, regressor_kwargs) def predict(self, x_data, always_return_1d=True, **predict_kwargs): """Expand :meth:`predict()` to accept kwargs.""" check_is_fitted(self) if not hasattr(self, 'regressor_'): raise NotFittedError( f"Regressor of {self.__class__} is not fitted yet, call fit() " f"first") # Kwargs for returning variance or covariance if ('return_std' in predict_kwargs and 'return_std' in getfullargspec( self.regressor_.predict).args): raise NotImplementedError( f"Using keyword argument 'return_std' for final regressor " f"{self.regressor_.__class__} is not supported yet, only " f"'return_var' is allowed. Expand the regressor to accept " f"'return_var' instead (see 'esmvaltool/diag_scripts/mlr" f"/models/gpr_sklearn.py' for an example)") mlr.check_predict_kwargs(predict_kwargs) return_var = predict_kwargs.get('return_var', False) return_cov = predict_kwargs.get('return_cov', False) # Prediction prediction = self.regressor_.predict(x_data, **predict_kwargs) if return_var or return_cov: pred = prediction[0] else: pred = prediction if pred.ndim == 1: pred_trans = self.transformer_.inverse_transform( pred.reshape(-1, 1)) else: pred_trans = self.transformer_.inverse_transform(pred) if self._to_be_squeezed(pred_trans, always_return_1d=always_return_1d): pred_trans = pred_trans.squeeze(axis=1) if not (return_var or return_cov): return pred_trans # Return scaled variance or covariance if desired err = prediction[1] if not hasattr(self.transformer_, 'scale_'): raise NotImplementedError( f"Transforming of additional prediction output (e.g. by " f"'return_var' or 'return_cov') is not supported for " f"transformer {self.transformer_.__class__} yet, the " f"necessary attribute 'scale_' is missing") scale = self.transformer_.scale_ if scale is not None: err *= scale**2 if self._to_be_squeezed(err, always_return_1d=always_return_1d): err = err.squeeze(axis=1) return (pred_trans, err) def _get_fit_params(self, fit_kwargs): """Separate ``transformer`` and ``regressor`` kwargs.""" steps = [ ('transformer', self.transformer), ('regressor', self.regressor), ] fit_params = _get_fit_parameters(fit_kwargs, steps, self.__class__) fit_params.setdefault('transformer', {}) fit_params.setdefault('regressor', {}) # FIXME if fit_params['transformer']: raise NotImplementedError( f"Fit parameters {fit_params['transformer']} for transformer " f"{self.transformer.__class__} of {self.__class__} are not " f"supported at the moment") return (fit_params['transformer'], fit_params['regressor']) def _fit_transformer(self, y_data): """Check transformer and fit transformer.""" if (self.transformer is not None and (self.func is not None or self.inverse_func is not None)): raise ValueError("'transformer' and functions 'func'/" "'inverse_func' cannot both be set.") if self.transformer is not None: self.transformer_ = clone(self.transformer) else: if self.func is not None and self.inverse_func is None: raise ValueError( "When 'func' is provided, 'inverse_func' must also be " "provided") self.transformer_ = FunctionTransformer( func=self.func, inverse_func=self.inverse_func, validate=True, check_inverse=self.check_inverse) self.transformer_.fit(y_data) if self.check_inverse: idx_selected = slice(None, None, max(1, y_data.shape[0] // 10)) y_sel = _safe_indexing(y_data, idx_selected) y_sel_t = self.transformer_.transform(y_sel) if not np.allclose(y_sel, self.transformer_.inverse_transform(y_sel_t)): warnings.warn( "The provided functions or transformer are " "not strictly inverse of each other. If " "you are sure you want to proceed regardless, " "set 'check_inverse=False'", UserWarning) def _to_be_squeezed(self, array, always_return_1d=True): """Check if ``array`` should be squeezed or not.""" squeeze = array.ndim == 2 and array.shape[1] == 1 if not always_return_1d: squeeze = squeeze and self._training_dim == 1 return squeeze
X_test = X[split:] #reshape data for fitting X_train_reshaped = X_train.reshape(-1, 1) X_test_reshaped = X_test.reshape(-1, 1) #fit SVR model, rbf kernel clf = SVR(C=5.0, epsilon=0.001, gamma=0.01, kernel='rbf') clf.fit(X_train_reshaped, y_train) y_pred = clf.predict(X_test_reshaped) #check performance MAE y_pred = y_pred.reshape(-1, 1) y_test = y_test.reshape(-1, 1) #retransform data for plots and results inv_y_pred = transformer.inverse_transform(y_pred) inv_y_test = transformer.inverse_transform(y_test) #MAE error = mean_absolute_error(inv_y_test, inv_y_pred) print('Test MAE: %.3f' % error) with open('LNtr_RBF Kernel_Tac.txt', 'w') as f: print('Test MAE: %.3f' % error, file=f) #r2_score r2score = r2_score(inv_y_test, inv_y_pred) print('Test r2_score: %.3f' % r2score) with open('LNtr__RBF Kernel_Tac.txt', 'a') as f: print('Test r2_score: %.3f' % r2score, file=f) #plots