def main(readcsv=read_csv, method='defaultDense'): infile = "./data/batch/linear_regression_train.csv" testfile = "./data/batch/linear_regression_test.csv" # Configure a Lasso regression training object train_algo = d4p.lasso_regression_training(interceptFlag=True) # Read data. Let's have 10 independent, and 1 dependent variable (for each observation) indep_data = readcsv(infile, range(10)) dep_data = readcsv(infile, range(10, 11)) # Now train/compute, the result provides the model for prediction train_result = train_algo.compute(indep_data, dep_data) # Now let's do some prediction predict_algo = d4p.lasso_regression_prediction() # read test data (with same #features) pdata = readcsv(testfile, range(10)) ptdata = readcsv(testfile, range(10, 11)) # now predict using the model from the training above predict_result = predict_algo.compute(pdata, train_result.model) # The prediction result provides prediction assert predict_result.prediction.shape == (pdata.shape[0], dep_data.shape[1]) assert np.square(predict_result.prediction - ptdata).mean() < 2.4 return (predict_result, ptdata)
def main(readcsv=read_csv, method='defaultDense'): infile = "./data/batch/linear_regression_train.csv" testfile = "./data/batch/linear_regression_test.csv" # Configure a Lasso regression training object train_algo = d4p.lasso_regression_training(interceptFlag=True) # Read data. Let's have 10 independent, and 2 dependent variables (for each observation) indep_data = readcsv(infile, range(10)) dep_data = readcsv(infile, range(10, 12)) # Now train/compute, the result provides the model for prediction train_result = train_algo.compute(indep_data, dep_data) # Now let's do some prediction predict_algo = d4p.lasso_regression_prediction() # read test data (with same #features) pdata = readcsv(testfile, range(10)) ptdata = readcsv(testfile, range(10, 12)) # now predict using the model from the training above predict_result = predict_algo.compute(pdata, train_result.model) # The prediction result provides prediction assert predict_result.prediction.shape == (pdata.shape[0], dep_data.shape[1]) # the example is used in tests with the scipy.sparse matrix # we use this trick until subtracting a sparse matrix is not supported if hasattr(ptdata, 'toarray'): ptdata = ptdata.toarray() # this assertion is outdated, will be fixed in next release # assert np.square(predict_result.prediction - np.asarray(ptdata)).mean() < 2.2 return (predict_result, ptdata)
def _daal4py_predict_lasso(self, X): X = make2d(X) _fptype = getFPType(self.coef_) lasso_palg = daal4py.lasso_regression_prediction(fptype=_fptype, method='defaultDense') lasso_res = lasso_palg.compute(X, self.daal_model_) res = lasso_res.prediction if res.shape[1] == 1 and self.coef_.ndim == 1: res = np.ravel(res) return res
def _daal4py_predict_lasso(self, X): X = make2d(X) _fptype = getFPType(self.coef_) lasso_palg = daal4py.lasso_regression_prediction(fptype=_fptype, method='defaultDense') if self.n_features_in_ != X.shape[1]: raise ValueError((f'X has {X.shape[1]} features, ' f'but Lasso is expecting ' f'{self.n_features_in_} features as input')) lasso_res = lasso_palg.compute(X, self.daal_model_) res = lasso_res.prediction if res.shape[1] == 1 and self.coef_.ndim == 1: res = np.ravel(res) return res