Args: data_features(Matrix): The input data matrix ``nxd``. output_samples(ColumnVector): The output for the given inputs, ``nx1``. n_alphas(int): The number of total regularization terms which will be tested by this solver. cross_validation_folds(int): The number of cross-validation folds used in this solver. """ super(_SkLearnLassoSolver, self).__init__(data_features, output_samples, n_alphas, cross_validation_folds) self._model = LassoCV(cv=cross_validation_folds, n_alphas=n_alphas, random_state=_rnd, normalize=False) def fit(self): """ The method which fits the requested model to the given data. """ self._model.fit(self._data_features, self._output_samples) self._fitted_coefficients = self._model.coef_ return self._fitted_coefficients _caratheodory_boosted_lasso_regression: Callable = caratheodory_booster(_SkLearnLassoSolver, perform_normalization=True) # A private dictionary used for creating the solvers factory :func:`get_method`. _lasso_regressions_methods: Dict[str, Callable] = { LassoRegressionMethods.SkLearnLassoRegression: _SkLearnLassoSolver, LassoRegressionMethods.BoostedLassoRegression: _caratheodory_boosted_lasso_regression } # A factory which creates the requested Lasso-Regression solvers. get_method: Callable = create_factory(_lasso_regressions_methods, are_methods=True)
data_features(Matrix): The input data matrix ``nxd``. output_samples(ColumnVector): The output for the given inputs, ``nx1``. n_alphas(int): The number of total regularization terms which will be tested by this solver. cross_validation_folds(int): The number of cross-validation folds used in this solver. """ super(_SkLearnElasticNetSolver, self).__init__(data_features, output_samples, n_alphas, cross_validation_folds) self._model = ElasticNetCV(cv=cross_validation_folds, n_alphas=n_alphas, random_state=_rnd, l1_ratio=elastic_net_factor, normalize=False) def fit(self) -> ColumnVector: """ The method which fits the requested model to the given data. """ self._model.fit(self._data_features, self._output_samples) self._fitted_coefficients = self._model.coef_ return self._fitted_coefficients _caratheodory_boosted_elastic_net_regression: Callable = caratheodory_booster(_SkLearnElasticNetSolver, perform_normalization=True) # A private dictionary used for creating the solvers factory 'get_method'. _elastic_net_regressions_methods: Dict[str, Callable] = { ElasticNetRegressionMethods.SkLearnElasticNetRegression: _SkLearnElasticNetSolver, ElasticNetRegressionMethods.BoostedElasticNetRegression: _caratheodory_boosted_elastic_net_regression } # A factory which creates the relevant Elastic-Net-Regression solver. get_method: Callable = create_factory(_elastic_net_regressions_methods, are_methods=True)
residuals: Vector = solver_obj.calc_residuals() coefficients_list.append(coefficients.tolist()) residuals_list.append(norm(residuals)) durations_list.append(duration) print( f'solver name={solver.__name__}, total alphas={current_alphas}, duration={duration}' ) except IOError: error_raised = True continue if not error_raised: data_log.append(LogFields.AlphasCount, list(alphas_range)) data_log.append(LogFields.Residuals, residuals_list) data_log.append(LogFields.Coefficients, coefficients_list) data_log.append(LogFields.DurationInSeconds, durations_list) all_logs[solver.__name__] = data_log return all_logs _experiment_type_to_method: Dict = { ExperimentType.RunTimeExperiment: _run_time_experiment, ExperimentType.NumberOfAlphasExperiment: _number_of_alphas_experiment } create_experiment: Callable = create_factory(_experiment_type_to_method, are_methods=True)
A Matrix of the features, besides the voltage, and a ColumnVector of the voltage feature. """ df = pd.read_csv(os.path.join(resources_path, "household_power_consumption.txt"), sep=';', na_values="?") df.dropna(axis=0, inplace=True) output_samples: ColumnVector = scale( pd.to_numeric(df["Voltage"]).to_numpy()) data_matrix: Matrix = np.ascontiguousarray( scale(df[["Global_active_power", "Global_reactive_power"]].astype("float64").to_numpy())) return data_matrix, output_samples # A private dictionary used to create the method "get_data" _database_type_to_function: Dict[str, Callable] = { DatabaseType.Synthetic: get_synthetic_data, DatabaseType.ThreeDRoadNetwork: _get_3d_road_network_data, DatabaseType.HouseSalesInKingCounty: _get_house_sales_in_king_county_data, DatabaseType.IndividualHouseholdElectricPowerConsumption: _get_household_electric_power_consumption_data } # The public method which fetches the data loading methods. get_data: Callable = create_factory(_database_type_to_function)
return MatInSVDForm(U, np.array(singular_values), V) def _get_example_5_data(data_size: int, singular_values: RowVector) -> Matrix: """ A method which creates a ``data_size x data_size`` matrix :math:`A` with the form: :math:`A=uv^{T}+\sigma I_n` where :math:`u=(1,0,...,0)` and :math:`v^{T}=\frac{1}{sqrt{n}}(1,1,...,1)` Args: data_size(int): The input data size n. singular_values(RowVector): A vector of singular values for the created matrix. Returns: A ``data_size x data_size`` Matrix in this form. """ return ExperimentNo5Form((data_size, data_size), singular_values[0]) # A private dictionary used to create the method "get_data" _data_type_to_function: Dict[str, Callable] = { ExperimentType.ExampleNo1: _get_first_3_examples_data, ExperimentType.ExampleNo2: _get_first_3_examples_data, ExperimentType.ExampleNo3: _get_first_3_examples_data, ExperimentType.ExampleNo4: _get_example_4_data, ExperimentType.ExampleNo5: _get_example_5_data } # The public method which fetches the data loading methods. get_data: Callable = create_factory(_data_type_to_function, are_methods=True)