本文整理汇总了Python中pyearth.Earth.predict方法的典型用法代码示例。如果您正苦于以下问题:Python Earth.predict方法的具体用法?Python Earth.predict怎么用?Python Earth.predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyearth.Earth
的用法示例。
在下文中一共展示了Earth.predict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: marsmodelorr
# 需要导入模块: from pyearth import Earth [as 别名]
# 或者: from pyearth.Earth import predict [as 别名]
def marsmodelorr(self, use_smY=True, slope_trunc=0.00001, savgol_window=151, savgol_order=3, ex_order=51):
Xf, Yf = self.Xf_, self.Yf_
X, Y = self.X_, self.Y_
fom = {}
# smooth the data
smY = savgol(Y, savgol_window, savgol_order)
# perform mars
model = MARS()
if use_smY:
model.fit(X, smY)
else:
model.fit(X, Y)
Y_h = model.predict(X)
'''
calculate dydx based on mars model to get knots and intercepts as this is
complicated to extract from hinge functions
'''
diff1 = np.diff(Y_h) / np.diff(X)
tdiff1 = diff1 - np.nanmin(diff1)
tdiff1 = tdiff1 / np.nanmax(tdiff1)
#calculate slopes of linear segments
ID = [i for i in range(1, len(tdiff1)) if np.abs(tdiff1[i] - tdiff1[i - 1]) > slope_trunc]
ID.insert(0, 0)
ID.append(np.argmax(X)) # this might cause an error
slopes = [np.nanmean(diff1[ID[i - 1]:ID[i]]) for i in range(1, len(ID) - 1)]
a = [Y_h[ID[i]] - slopes[i] * X[ID[i]] for i in range(len(ID) - 2)]
IDM, IDm = np.argmax(slopes), np.argmin(np.abs(slopes))
# intercept of highest slope and zero as well as highest slope and lowest slope
fom['zinter'] = -a[IDM] / slopes[IDM]
fom['lminter'] = (a[IDM] - a[IDm]) / (slopes[IDm] - slopes[IDM])
fom['max_slope'] = slopes[IDM]
fom['curr_lminter_model'] = fom['lminter'] * slopes[IDM] + a[IDM]
fom['curr_lminter_data'] = np.mean(Y[np.where(np.abs(X - fom['lminter']) < 0.5)[0]])
# calculate how the CV curves kight look like without the 'ORR part'
srYs = smY - model.predict(X)
srYf = savgol(Yf - model.predict(Xf), savgol_window, savgol_order)
# calculate their derivative
dsrYf = savgol(np.diff(srYf) / np.diff(Xf), savgol_window, savgol_order)
# find the extrema in the derivatives for extraction of redox pots
redID_f = argrelextrema(srYf, np.less, order=ex_order)
oxID_f = argrelextrema(srYf, np.greater, order=ex_order)
# calc some more foms like position of redox waves
fom['redpot_f'], fom['redpot_f_var'] = np.nanmean(Xf[redID_f]), np.nanstd(Xf[redID_f])
fom['oxpot_f'], fom['oxpot_f_var'] = np.nanmean(Xf[oxID_f]), np.nanstd(Xf[oxID_f])
fom['X'], fom['Xf'] = X, Xf
fom['srYs'], fom['srYf'], fom['smY'] = srYs, srYf, smY
fom['Y'], fom['Yf'], fom['Y_h'] = Y, Yf, Y_h
fom['noise_lvl'] = np.sum((Y_h - Y) ** 2, axis=0)
self.fom = fom
示例2: test_export_python_string
# 需要导入模块: from pyearth import Earth [as 别名]
# 或者: from pyearth.Earth import predict [as 别名]
def test_export_python_string():
for smooth in (True, False):
model = Earth(penalty=1, smooth=smooth, max_degree=2).fit(X, y)
export_model = export_python_string(model, 'my_test_model')
six.exec_(export_model, globals())
for exp_pred, model_pred in zip(model.predict(X), my_test_model(X)):
assert_almost_equal(exp_pred, model_pred)
示例3: test_copy_compatibility
# 需要导入模块: from pyearth import Earth [as 别名]
# 或者: from pyearth.Earth import predict [as 别名]
def test_copy_compatibility():
model = Earth(**default_params).fit(X, y)
model_copy = copy.copy(model)
assert_true(model_copy == model)
assert_true(
numpy.all(model.predict(X) == model_copy.predict(X)))
assert_true(model.basis_[0] is model.basis_[1]._get_root())
assert_true(model_copy.basis_[0] is model_copy.basis_[1]._get_root())
示例4: run_pyearth
# 需要导入模块: from pyearth import Earth [as 别名]
# 或者: from pyearth.Earth import predict [as 别名]
def run_pyearth(X, y, **kwargs):
'''Run with pyearth. Return prediction value, training time, and number of forward pass iterations.'''
model = Earth(**kwargs)
t0 = time.time()
model.fit(X, y)
t1 = time.time()
y_pred = model.predict(X)
forward_iterations = len(model.forward_trace()) - 1
return y_pred, t1 - t0, forward_iterations
示例5: test_nb_terms
# 需要导入模块: from pyearth import Earth [as 别名]
# 或者: from pyearth.Earth import predict [as 别名]
def test_nb_terms():
for max_terms in (1, 3, 12, 13):
model = Earth(max_terms=max_terms)
model.fit(X, y)
assert_true(len(model.basis_) <= max_terms)
assert_true(len(model.coef_) <= len(model.basis_))
assert_true(len(model.coef_) >= 1)
if max_terms == 1:
assert_list_almost_equal_value(model.predict(X), y.mean())
示例6: test_export_sympy
# 需要导入模块: from pyearth import Earth [as 别名]
# 或者: from pyearth.Earth import predict [as 别名]
def test_export_sympy():
import pandas as pd
from sympy.utilities.lambdify import lambdify
from sympy.printing.lambdarepr import NumPyPrinter
class PyEarthNumpyPrinter(NumPyPrinter):
def _print_Max(self, expr):
return 'maximum(' + ','.join(self._print(i) for i in expr.args) + ')'
def _print_NaNProtect(self, expr):
return 'where(isnan(' + ','.join(self._print(a) for a in expr.args) + '), 0, ' \
+ ','.join(self._print(a) for a in expr.args) + ')'
def _print_Missing(self, expr):
return 'isnan(' + ','.join(self._print(a) for a in expr.args) + ').astype(float)'
for smooth, n_cols, allow_missing in product((True, False), (1, 2), (True, False)):
X_df = pd.DataFrame(X.copy(), columns=['x_%d' % i for i in range(X.shape[1])])
y_df = pd.DataFrame(Y[:, :n_cols])
if allow_missing:
# Randomly remove some values so that the fitted model contains MissingnessBasisFunctions
X_df['x_1'][numpy.random.binomial(n=1, p=.1, size=X_df.shape[0]).astype(bool)] = numpy.nan
model = Earth(allow_missing=allow_missing, smooth=smooth, max_degree=2).fit(X_df, y_df)
expressions = export_sympy(model) if n_cols > 1 else [export_sympy(model)]
module_dict = {'select': numpy.select, 'less_equal': numpy.less_equal, 'isnan': numpy.isnan,
'greater_equal':numpy.greater_equal, 'logical_and': numpy.logical_and, 'less': numpy.less,
'logical_not':numpy.logical_not, "greater": numpy.greater, 'maximum':numpy.maximum,
'Missing': lambda x: numpy.isnan(x).astype(float),
'NaNProtect': lambda x: numpy.where(numpy.isnan(x), 0, x), 'nan': numpy.nan,
'float': float, 'where': numpy.where
}
for i, expression in enumerate(expressions):
# The lambdified functions for smoothed basis functions only work with modules='numpy' and
# for regular basis functions with modules={'Max':numpy.maximum}. This is a confusing situation
func = lambdify(X_df.columns, expression, printer=PyEarthNumpyPrinter, modules=module_dict)
y_pred_sympy = func(*[X_df.loc[:,var] for var in X_df.columns])
y_pred = model.predict(X_df)[:,i] if n_cols > 1 else model.predict(X_df)
assert_array_almost_equal(y_pred, y_pred_sympy)
示例7: runModel
# 需要导入模块: from pyearth import Earth [as 别名]
# 或者: from pyearth.Earth import predict [as 别名]
def runModel(i,featureCombo):
mae = np.array([])
logging.warning('try alpha = %s' % i)
for ktrain,ktest in kf:
x = trainCleaned.iloc[ktrain,]
y = trainCleaned.iloc[ktest,]
model = Earth()
model.fit(x[featureCombo],x['Expected'])
pred = model.predict(y[featureCombo])
mae = np.append(mae,(getMAE(pred,y['Expected'])))
logging.warning('average 10-fold MAE for alpha %s feature %s' % (i,featureCombo))
logging.warning(mae.mean())
示例8: test_output_weight
# 需要导入模块: from pyearth import Earth [as 别名]
# 或者: from pyearth.Earth import predict [as 别名]
def test_output_weight():
x = numpy.random.uniform(-1, 1, size=(1000, 1))
y = (numpy.dot(x, numpy.random.normal(0, 1, size=(1, 10)))) ** 5 + 1
y = (y - y.mean(axis=0)) / y.std(axis=0)
group = numpy.array([1] * 5 + [0] * 5)
output_weight = numpy.array([1] * 5 + [2] * 5, dtype=float)
model = Earth().fit(x, y, output_weight=output_weight)
# Check that the model fits at least better
# the more heavily weighted group
mse = ((model.predict(x) - y)**2).mean(axis=0)
group1_mean = mse[group].mean()
group2_mean = mse[numpy.logical_not(group)].mean()
assert_true(group1_mean > group2_mean or
round(abs(group1_mean - group2_mean), 7) == 0)
示例9: getTrain
# 需要导入模块: from pyearth import Earth [as 别名]
# 或者: from pyearth.Earth import predict [as 别名]
def getTrain(trainData, testData):
size_s = len(trainData)
size_t = len(testData)
lenY = len(testData[0])
X = numpy.zeros((size_s,lenY-1))
Y = numpy.zeros((size_s,1))
z = 0
for d in trainData:
for j in range(lenY-1):
X[z][j] = d[j]
Y[z][0] = float(d[lenY-1])
z += 1
z = 0
dX = numpy.zeros((size_t,lenY-1))
for d in testData:
for j in range(lenY-1):
dX[z][j] = d[j]
z += 1
model = Earth()
model.fit(X,Y)
y_hat = model.predict(dX)
corrent = 0
for i in range(size_t):
x1 = testData[i][lenY-1]
x2 = y_hat[i]
if x1 * x2 >= 0:
corrent += 1
return corrent
示例10: MARSInterpolant
# 需要导入模块: from pyearth import Earth [as 别名]
# 或者: from pyearth.Earth import predict [as 别名]
class MARSInterpolant(Surrogate):
"""Compute and evaluate a MARS interpolant
MARS builds a model of the form
.. math::
\\hat{f}(x) = \\sum_{i=1}^{k} c_i B_i(x).
The model is a weighted sum of basis functions :math:`B_i(x)`. Each basis
function :math:`B_i(x)` takes one of the following three forms:
1. a constant 1.
2. a hinge function of the form :math:`\\max(0, x - const)` or \
:math:`\\max(0, const - x)`. MARS automatically selects variables \
and values of those variables for knots of the hinge functions.
3. a product of two or more hinge functions. These basis functions c \
an model interaction between two or more variables.
:param dim: Number of dimensions
:type dim: int
:ivar dim: Number of dimensions
:ivar num_pts: Number of points in surrogate model
:ivar X: Point incorporated in surrogate model (num_pts x dim)
:ivar fX: Function values in surrogate model (num_pts x 1)
:ivar updated: True if model is up-to-date (no refit needed)
:ivar model: Earth object
"""
def __init__(self, dim):
self.num_pts = 0
self.X = np.empty([0, dim])
self.fX = np.empty([0, 1])
self.dim = dim
self.updated = False
try:
from pyearth import Earth
self.model = Earth()
except ImportError as err:
print("Failed to import pyearth")
raise err
def _fit(self):
"""Compute new coefficients if the MARS interpolant is not updated."""
with warnings.catch_warnings():
warnings.simplefilter("ignore") # Surpress deprecation warnings
if self.updated is False:
self.model.fit(self.X, self.fX)
self.updated = True
def predict(self, xx):
"""Evaluate the MARS interpolant at the points xx
:param xx: Prediction points, must be of size num_pts x dim or (dim, )
:type xx: numpy.ndarray
:return: Prediction of size num_pts x 1
:rtype: numpy.ndarray
"""
self._fit()
xx = np.atleast_2d(xx)
return np.expand_dims(self.model.predict(xx), axis=1)
def predict_deriv(self, xx):
"""Evaluate the derivative of the MARS interpolant at points xx
:param xx: Prediction points, must be of size num_pts x dim or (dim, )
:type xx: numpy.array
:return: Derivative of the RBF interpolant at xx
:rtype: numpy.array
"""
self._fit()
xx = np.expand_dims(xx, axis=0)
dfx = self.model.predict_deriv(xx, variables=None)
return dfx[0]
示例11: enumerate
# 需要导入模块: from pyearth import Earth [as 别名]
# 或者: from pyearth.Earth import predict [as 别名]
k = 1
fig = plt.figure()
for i, alpha in enumerate(alphas):
# Fit an Earth model
model = Earth(max_degree=5,
minspan_alpha=.05,
endspan_alpha=.05,
max_terms=10,
check_every=1,
thresh=0.)
output_weight = np.array([alpha, 1 - alpha])
model.fit(X, y_mix, output_weight=output_weight)
print(model.summary())
# Plot the model
y_hat = model.predict(X)
mse = ((y_hat - y_mix) ** 2).mean(axis=0)
ax = plt.subplot(n_plots, 2, k)
ax.set_ylabel("Run {0}".format(i + 1), rotation=0, labelpad=20)
plt.plot(X[:, 6], y_mix[:, 0], 'r.')
plt.plot(X[:, 6], model.predict(X)[:, 0], 'b.')
plt.title("MSE: {0:.3f}, Weight : {1:.1f}".format(mse[0], alpha))
plt.subplot(n_plots, 2, k + 1)
plt.plot(X[:, 5], y_mix[:, 1], 'r.')
plt.plot(X[:, 5], model.predict(X)[:, 1], 'b.')
plt.title("MSE: {0:.3f}, Weight : {1:.1f}".format(mse[1], 1 - alpha))
k += 2
plt.tight_layout()
plt.show()
示例12: MARSInterpolant
# 需要导入模块: from pyearth import Earth [as 别名]
# 或者: from pyearth.Earth import predict [as 别名]
class MARSInterpolant(Earth):
"""Compute and evaluate a MARS interpolant
:ivar nump: Current number of points
:ivar maxp: Initial maximum number of points (can grow)
:ivar x: Interpolation points
:ivar fx: Function evaluations of interpolation points
:ivar dim: Number of dimensions
:ivar model: MARS interpolaion model
"""
def __init__(self, maxp=100):
self.nump = 0
self.maxp = maxp
self.x = None # pylint: disable=invalid-name
self.fx = None
self.dim = None
self.model = Earth()
self.updated = False
def reset(self):
"""Reset the interpolation."""
self.nump = 0
self.x = None
self.fx = None
self.updated = False
def _alloc(self, dim):
"""Allocate storage for x, fx, rhs, and A.
:param dim: Number of dimensions
"""
maxp = self.maxp
self.dim = dim
self.x = np.zeros((maxp, dim))
self.fx = np.zeros((maxp, 1))
def _realloc(self, dim, extra=1):
"""Expand allocation to accommodate more points (if needed)
:param dim: Number of dimensions
:param extra: Number of additional points to accommodate
"""
if self.nump == 0:
self._alloc(dim)
elif self.nump+extra > self.maxp:
self.maxp = max(self.maxp*2, self.maxp+extra)
self.x.resize((self.maxp, dim))
self.fx.resize((self.maxp, 1))
def get_x(self):
"""Get the list of data points
:return: List of data points
"""
return self.x[:self.nump, :]
def get_fx(self):
"""Get the list of function values for the data points.
:return: List of function values
"""
return self.fx[:self.nump, :]
def add_point(self, xx, fx):
"""Add a new function evaluation
:param xx: Point to add
:param fx: The function value of the point to add
"""
dim = len(xx)
self._realloc(dim)
self.x[self.nump, :] = xx
self.fx[self.nump, :] = fx
self.nump += 1
self.updated = False
def eval(self, xx, d=None):
"""Evaluate the MARS interpolant at the point xx
:param xx: Point where to evaluate
:return: Value of the MARS interpolant at x
"""
if self.updated is False:
self.model.fit(self.x, self.fx)
self.updated = True
xx = np.expand_dims(xx, axis=0)
fx = self.model.predict(xx)
return fx[0]
def evals(self, xx, d=None):
"""Evaluate the MARS interpolant at the points xx
:param xx: Points where to evaluate
:return: Values of the MARS interpolant at x
"""
if self.updated is False:
self.model.fit(self.x, self.fx)
self.updated = True
#.........这里部分代码省略.........
示例13: test_export_python_function
# 需要导入模块: from pyearth import Earth [as 别名]
# 或者: from pyearth.Earth import predict [as 别名]
def test_export_python_function():
for smooth in (True, False):
model = Earth(penalty=1, smooth=smooth, max_degree=2).fit(X, y)
export_model = export_python_function(model)
for exp_pred, model_pred in zip(model.predict(X), export_model(X)):
assert_almost_equal(exp_pred, model_pred)
示例14: join
# 需要导入模块: from pyearth import Earth [as 别名]
# 或者: from pyearth.Earth import predict [as 别名]
folder_path = join(DATA_DIR, 'models', folder_name)
os.makedirs(folder_path)
# Dump the hyperparameter dictionary
with open(join(folder_path, 'hyperparameters.pkl'), 'wb') as f:
pickle.dump(hp, f, -1)
training_times = []
for a in xrange(0, y.shape[1]):
start = time.time()
y_train = y_train_mat[:, a:(a + 1)].ravel()
model = Earth(**hp)
model.fit(X_train_scaled, y_train)
end = time.time()
print 'Fast MARS t-7, a{0} took {1} to train'.format(a, end - start)
training_times.append(end - start)
with open(join(DATA_DIR,
'models/{0}/MARS_a{1}.pkl'.format(folder_name, a)),
'wb') as f:
pickle.dump(model, f, -1)
start = time.time()
y_pred_mat[:, a] = model.predict(X_test)
end = time.time()
print 'Fast MARS t-7, a{0} took {1} to predict'.format(a, end - start)
sys.stdout.flush()
RMSE = mean_squared_error(y_test_mat, y_pred_mat) ** 0.5
print RMSE
with open(join(folder_path, 'stats.txt'), 'wb') as f:
f.write('{0}\n{1}\n'.format(str(RMSE), sum(training_times)))
示例15: earth
# 需要导入模块: from pyearth import Earth [as 别名]
# 或者: from pyearth.Earth import predict [as 别名]
def earth(x, y):
model = Earth(max_terms=30, endspan=2, thresh=0.00001)
model.fit(np.array(x), np.array(y))
return model.predict(x)