def test_ridge_regression_model(datatype, algorithm, nrows, column_info): if algorithm == "svd" and nrows > 46340: pytest.skip("svd solver is not supported for the data that has more" "than 46340 rows or columns if you are using CUDA version" "10.x") ncols, n_info = column_info X_train, X_test, y_train, y_test = make_regression_dataset( datatype, nrows, ncols, n_info ) # Initialization of cuML's ridge regression model curidge = cuRidge(fit_intercept=False, normalize=False, solver=algorithm) # fit and predict cuml ridge regression model curidge.fit(X_train, y_train) curidge_predict = curidge.predict(X_test) if nrows < 500000: # sklearn ridge regression model initialization, fit and predict skridge = skRidge(fit_intercept=False, normalize=False) skridge.fit(X_train, y_train) skridge_predict = skridge.predict(X_test) assert array_equal(skridge_predict, curidge_predict, 1e-1, with_sign=True)
def test_ridge(datatype, X_type, y_type, algorithm): X = np.array([[2.0, 5.0], [6.0, 9.0], [2.0, 2.0], [2.0, 3.0]], dtype=datatype) y = np.dot(X, np.array([5.0, 10.0]).astype(datatype)) pred_data = np.array([[3.0, 5.0], [2.0, 5.0]]).astype(datatype) skridge = skRidge(fit_intercept=False, normalize=False) skridge.fit(X, y) curidge = cuRidge(fit_intercept=False, normalize=False, solver=algorithm) if X_type == 'dataframe': gdf = cudf.DataFrame() gdf['0'] = np.asarray([2, 6, 2, 2], dtype=datatype) gdf['1'] = np.asarray([5, 9, 2, 3], dtype=datatype) curidge.fit(gdf, y) elif X_type == 'ndarray': curidge.fit(X, y) sk_predict = skridge.predict(pred_data) cu_predict = curidge.predict(pred_data).to_array() assert array_equal(sk_predict, cu_predict, 1e-3, with_sign=True)
def test_weighted_ridge(datatype, algorithm, fit_intercept, normalize, distribution): nrows, ncols, n_info = 1000, 20, 10 max_weight = 10 noise = 20 X_train, X_test, y_train, y_test = make_regression_dataset( datatype, nrows, ncols, n_info, noise=noise ) # set weight per sample to be from 1 to max_weight if distribution == "uniform": wt = np.random.randint(1, high=max_weight, size=len(X_train)) elif distribution == "exponential": wt = np.random.exponential(size=len(X_train)) else: wt = np.random.lognormal(size=len(X_train)) # Initialization of cuML's linear regression model curidge = cuRidge(fit_intercept=fit_intercept, normalize=normalize, solver=algorithm) # fit and predict cuml linear regression model curidge.fit(X_train, y_train, sample_weight=wt) curidge_predict = curidge.predict(X_test) # sklearn linear regression model initialization, fit and predict skridge = skRidge(fit_intercept=fit_intercept, normalize=normalize) skridge.fit(X_train, y_train, sample_weight=wt) skridge_predict = skridge.predict(X_test) assert array_equal(skridge_predict, curidge_predict, 1e-1, with_sign=True)
def test_ridge_regression_model(datatype, algorithm, nrows, column_info): ncols, n_info = column_info X_train, X_test, y_train, y_test = make_regression_dataset( datatype, nrows, ncols, n_info ) # Initialization of cuML's ridge regression model curidge = cuRidge(fit_intercept=False, normalize=False, solver=algorithm) # fit and predict cuml ridge regression model curidge.fit(X_train, y_train) curidge_predict = curidge.predict(X_test) if nrows < 500000: # sklearn ridge regression model initialization, fit and predict skridge = skRidge(fit_intercept=False, normalize=False) skridge.fit(X_train, y_train) skridge_predict = skridge.predict(X_test) assert array_equal(skridge_predict, curidge_predict, 1e-1, with_sign=True)
def test_ridge_regression_model_default(datatype): X_train, X_test, y_train, y_test = small_regression_dataset(datatype) curidge = cuRidge() # fit and predict cuml ridge regression model curidge.fit(X_train, y_train) curidge_predict = curidge.predict(X_test) # sklearn ridge regression model initialization, fit and predict skridge = skRidge() skridge.fit(X_train, y_train) skridge_predict = skridge.predict(X_test) assert array_equal(skridge_predict, curidge_predict, 1e-1, with_sign=True)
def test_linear_models(datatype, X_type, y_type, algorithm, nrows, ncols, n_info): train_rows = np.int32(nrows * 0.8) X, y = make_regression(n_samples=(nrows), n_features=ncols, n_informative=n_info, random_state=0) X_test = np.asarray(X[train_rows:, 0:]).astype(datatype) X_train = np.asarray(X[0:train_rows, :]).astype(datatype) y_train = np.asarray(y[0:train_rows, ]).astype(datatype) # Initialization of cuML's linear and ridge regression models cuols = cuLinearRegression(fit_intercept=True, normalize=False, algorithm=algorithm) curidge = cuRidge(fit_intercept=False, normalize=False, solver=algorithm) if X_type == 'dataframe': y_train = pd.DataFrame({'labels': y_train[0:, ]}) X_train = pd.DataFrame( {'fea%d' % i: X_train[0:, i] for i in range(X_train.shape[1])}) X_test = pd.DataFrame( {'fea%d' % i: X_test[0:, i] for i in range(X_test.shape[1])}) X_cudf = cudf.DataFrame.from_pandas(X_train) X_cudf_test = cudf.DataFrame.from_pandas(X_test) y_cudf = y_train.values y_cudf = y_cudf[:, 0] y_cudf = cudf.Series(y_cudf) # fit and predict cuml linear regression model cuols.fit(X_cudf, y_cudf) cuols_predict = cuols.predict(X_cudf_test).to_array() # fit and predict cuml ridge regression model curidge.fit(X_cudf, y_cudf) curidge_predict = curidge.predict(X_cudf_test).to_array() elif X_type == 'ndarray': # fit and predict cuml linear regression model cuols.fit(X_train, y_train) cuols_predict = cuols.predict(X_test).to_array() # fit and predict cuml ridge regression model curidge.fit(X_train, y_train) curidge_predict = curidge.predict(X_test).to_array() if nrows < 500000: # sklearn linear and ridge regression model initialization and fit skols = skLinearRegression(fit_intercept=True, normalize=False) skols.fit(X_train, y_train) skridge = skRidge(fit_intercept=False, normalize=False) skridge.fit(X_train, y_train) skols_predict = skols.predict(X_test) skridge_predict = skridge.predict(X_test) assert array_equal(skols_predict, cuols_predict, 1e-1, with_sign=True) assert array_equal(skridge_predict, curidge_predict, 1e-1, with_sign=True)
# U R_i2n R_n2n | # Initialize, fit and apply NodeToOutput y_pred = Ridge().fit(R_n2n, y).predict(R_n2n) print(y_pred.shape) # Predicting the Mackey-Glass equation # Load the dataset X, y = mackey_glass(n_timesteps=5000) # Define Train/Test lengths trainLen = 1900 X_train, y_train = X[:trainLen], y[:trainLen] X_test, y_test = X[trainLen:], y[trainLen:] # Initialize and train an ELMRegressor and an ESNRegressor esn = ESNRegressor().fit(X=X_train.reshape(-1, 1), y=y_train) elm = ELMRegressor(regressor=skRidge()).fit(X=X_train.reshape(-1, 1), y=y_train) print("Fitted models") # Build Reservoir Computing Networks with PyRCN U, y = make_blobs(n_samples=100, n_features=10) # Vanilla ELM for regression tasks with input_scaling # _ _ _ _ _ _ _ _ _ _ _ _ _ _ # | | | | # ----|Input-to-Node |-----|Node-to-Output |------ # u[n]| _ _ _ _ _ _ _|r'[n]| _ _ _ _ _ _ _ |y[n] # y_pred # vanilla_elm = ELMRegressor(input_scaling=0.9) vanilla_elm.fit(U, y)