from gplearn.genetic import SymbolicTransformer
from sklearn.utils import check_random_state
from sklearn.datasets import load_boston
import numpy as np
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
rng = check_random_state(0)
boston = load_boston()
perm = rng.permutation(boston.target.size)
boston.data = boston.data[perm]
boston.target = boston.target[perm]
from sklearn.linear_model import Ridge
est = Ridge()
est.fit(boston.data[:300, :], boston.target[:300])
print(est.score(boston.data[300:, :], boston.target[300:]))
function_set = ['add', 'sub', 'mul', 'div', 'sqrt', 'log',
'abs', 'neg', 'inv', 'max', 'min']
gp = SymbolicTransformer(generations=20, population_size=2000,
hall_of_fame=100, n_components=10,
function_set=function_set,
parsimony_coefficient=0.0005,
max_samples=0.9, verbose=1,
random_state=0)
gp.fit(boston.data[:300, :], boston.target[:300])
gp_features = gp.transform(boston.data)
new_boston = np.hstack((boston.data, gp_features))
est = Ridge()
est.fit(new_boston[:300, :], boston.target[:300])
print(est.score(new_boston[300:, :], boston.target[300:]))
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
gplearn demo
于 2022-01-29 19:41:48 首次发布