本文整理汇总了Python中scipy.special.expit方法的典型用法代码示例。如果您正苦于以下问题:Python special.expit方法的具体用法?Python special.expit怎么用?Python special.expit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.special
的用法示例。
在下文中一共展示了special.expit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Ey
# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import expit [as 别名]
def Ey(self, f):
r"""
Expected value of the Bernoulli likelihood.
Parameters
----------
f: ndarray
latent function from the GLM prior (:math:`\mathbf{f} =
\boldsymbol\Phi \mathbf{w}`)
Returns
-------
Ey: ndarray
expected value of y, :math:`\mathbb{E}[\mathbf{y}|\mathbf{f}]`.
"""
return expit(f)
示例2: df
# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import expit [as 别名]
def df(self, y, f):
r"""
Derivative of Bernoulli log likelihood w.r.t.\ f.
Parameters
----------
y: ndarray
array of 0, 1 valued integers of targets
f: ndarray
latent function from the GLM prior (:math:`\mathbf{f} =
\boldsymbol\Phi \mathbf{w}`)
Returns
-------
df: ndarray
the derivative :math:`\partial \log p(y|f) / \partial f`
"""
y, f = np.broadcast_arrays(y, f)
return y - expit(f)
示例3: cdf
# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import expit [as 别名]
def cdf(self, y, f):
r"""
Cumulative density function of the likelihood.
Parameters
----------
y: ndarray
query quantiles, i.e.\ :math:`P(Y \leq y)`.
f: ndarray
latent function from the GLM prior (:math:`\mathbf{f} =
\boldsymbol\Phi \mathbf{w}`)
Returns
-------
cdf: ndarray
Cumulative density function evaluated at y.
"""
return bernoulli.cdf(y, expit(f))
示例4: loglike
# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import expit [as 别名]
def loglike(self, y, f, n):
r"""
Binomial log likelihood.
Parameters
----------
y: ndarray
array of 0, 1 valued integers of targets
f: ndarray
latent function from the GLM prior (:math:`\mathbf{f} =
\boldsymbol\Phi \mathbf{w}`)
n: ndarray
the total number of observations
Returns
-------
logp: ndarray
the log likelihood of each y given each f under this
likelihood.
"""
ll = binom.logpmf(y, n=n, p=expit(f))
return ll
示例5: transform
# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import expit [as 别名]
def transform(self, X, lenscale=None):
r"""
Apply the sigmoid basis function to X.
Parameters
----------
X: ndarray
(N, d) array of observations where N is the number of samples, and
d is the dimensionality of X.
lenscale: float
the length scale (scalar) of the RBFs to apply to X. If not input,
this uses the value of the initial length scale.
Returns
-------
ndarray:
of shape (N, D) where D is number of centres.
"""
N, d = X.shape
lenscale = self._check_dim(d, lenscale)
return expit(cdist(X / lenscale, self.C / lenscale, 'euclidean'))
示例6: logistic_regression_cost_gradient
# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import expit [as 别名]
def logistic_regression_cost_gradient(parameters, input, output):
"""
Cost and gradient for logistic regression
:param parameters: weight vector
:param input: feature vector
:param output: binary label (0 or 1)
:return: cost and gradient for the input and output
"""
prediction = expit(np.dot(input, parameters))
if output:
inside_log = prediction
else:
inside_log = 1.0 - prediction
if inside_log != 0.0:
cost = -np.log(inside_log)
else:
cost = np.finfo(float).min
gradient = (prediction - output) * input
return cost, gradient
示例7: assertLogisticRegression
# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import expit [as 别名]
def assertLogisticRegression(self, sampler):
data_size = 3
input_size = 5
inputs = np.random.uniform(-10.0, 10.0, size=(data_size, input_size))
outputs = np.random.randint(0, 2, size=data_size)
initial_parameters = np.random.normal(scale=1e-5, size=input_size)
# Create cost and gradient function for gradient descent and check its gradient
cost_gradient = bind_cost_gradient(logistic_regression_cost_gradient,
inputs, outputs, sampler=sampler)
result = gradient_check(cost_gradient, initial_parameters)
self.assertEqual([], result)
# Train logistic regression and see if it predicts correct labels
final_parameters, cost_history = gradient_descent(cost_gradient, initial_parameters, 100)
predictions = expit(np.dot(inputs, final_parameters)) > 0.5
# Binary classification of 3 data points with 5 dimension is always linearly separable
for output, prediction in zip(outputs, predictions):
self.assertEqual(output, prediction)
示例8: predict
# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import expit [as 别名]
def predict(model, docs, dtype='real'):
normalized = docs/docs.sum(axis=-1)[:,np.newaxis]
normalized_bow = torch.tensor(normalized, dtype=torch.float)
num_documents = docs.shape[0]
treatment_ones = torch.ones(num_documents)
treatment_zeros = torch.zeros(num_documents)
model.eval()
with torch.no_grad():
doc_representation,_ = model.get_theta(normalized_bow)
propensity_score = model.predict_treatment(doc_representation).squeeze().detach().numpy()
propensity_score = expit(propensity_score)
expected_outcome_treat = model.predict_outcome_st_treat(doc_representation, treatment_ones).squeeze().detach().numpy()
expected_outcome_no_treat = model.predict_outcome_st_no_treat(doc_representation, treatment_zeros).squeeze().detach().numpy()
if dtype == 'binary':
expected_outcome_treat = expit(expected_outcome_treat)
expected_outcome_no_treat = expit(expected_outcome_no_treat)
return propensity_score, expected_outcome_treat, expected_outcome_no_treat
示例9: _perturbed_model
# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import expit [as 别名]
def _perturbed_model(q_t0, q_t1, g, t, q, eps):
# helper function for psi_tmle
h1 = t / q - ((1 - t) * g) / (q * (1 - g))
full_q = (1.0 - t) * q_t0 + t * q_t1
perturbed_q = full_q - eps * h1
def q1(t_cf, epsilon):
h_cf = t_cf * (1.0 / g) - (1.0 - t_cf) / (1.0 - g)
full_q = (1.0 - t_cf) * q_t0 + t_cf * q_t1 # predictions from unperturbed model
return full_q - epsilon * h_cf
psi_init = np.mean(t * (q1(np.ones_like(t), eps) - q1(np.zeros_like(t), eps))) / q
h2 = (q_t1 - q_t0 - psi_init) / q
perturbed_g = expit(logit(g) - eps * h2)
return perturbed_q, perturbed_g
示例10: test_probability_exponential
# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import expit [as 别名]
def test_probability_exponential():
# Predict probabilities.
clf = GradientBoostingClassifier(loss='exponential',
n_estimators=100, random_state=1)
assert_raises(ValueError, clf.predict_proba, T)
clf.fit(X, y)
assert_array_equal(clf.predict(T), true_result)
# check if probabilities are in [0, 1].
y_proba = clf.predict_proba(T)
assert np.all(y_proba >= 0.0)
assert np.all(y_proba <= 1.0)
score = clf.decision_function(T).ravel()
assert_array_almost_equal(y_proba[:, 1], expit(2 * score))
# derive predictions from probabilities
y_pred = clf.classes_.take(y_proba.argmax(axis=1), axis=0)
assert_array_equal(y_pred, true_result)
示例11: predict_proba
# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import expit [as 别名]
def predict_proba(self, X):
"""Estimate probability.
Parameters
----------
X : array-like, shape (n_samples, n_features)
Input data.
Returns
-------
C : array, shape (n_samples, n_classes)
Estimated probabilities.
"""
check_is_fitted(self, 'classes_')
decision = self.decision_function(X)
if self.classes_.size == 2:
proba = expit(decision)
return np.vstack([1-proba, proba]).T
else:
return softmax(decision)
示例12: to_prob
# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import expit [as 别名]
def to_prob(probabilities: np.ndarray):
"""
If the probabilities array is not a distrubution will softmax it.
Args:
probabilities (array): [batch_size, num_classes, ...]
Returns:
Same as probabilities.
"""
not_bounded = np.min(probabilities) < 0 or np.max(probabilities) > 1.0
multiclass = probabilities.shape[1] > 1
sum_to_one = np.allclose(probabilities.sum(1), 1)
if not_bounded or (multiclass and not sum_to_one):
if multiclass:
probabilities = softmax(probabilities, 1)
else:
probabilities = expit(probabilities)
return probabilities
示例13: _mu
# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import expit [as 别名]
def _mu(distr, z, eta, fit_intercept):
"""The non-linearity (inverse link)."""
if distr in ['softplus', 'gamma']:
mu = np.log1p(np.exp(z))
elif distr == 'poisson':
mu = z.copy()
beta0 = (1 - eta) * np.exp(eta) if fit_intercept else 0.
mu[z > eta] = z[z > eta] * np.exp(eta) + beta0
mu[z <= eta] = np.exp(z[z <= eta])
elif distr == 'gaussian':
mu = z
elif distr == 'binomial':
mu = expit(z)
elif distr == 'probit':
mu = norm.cdf(z)
return mu
示例14: update_dag_logits
# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import expit [as 别名]
def update_dag_logits(self, gradient_dicts, weight_decay, max_grad=0.1):
"""
Updates the probabilities of each path being selected using the given gradients.
"""
dag_probs = tuple(expit(logit) for logit in self.dags_logits)
current_average_dag_probs = tuple(np.mean(prob) for prob in dag_probs)
for i, key in enumerate(self.all_connections):
for grad_dict, current_average_dag_prob, dag_logits in zip(gradient_dicts, current_average_dag_probs,
self.dags_logits):
if key in grad_dict:
grad = grad_dict[key] - weight_decay * (
current_average_dag_prob - self.target_ave_prob) # *expit(dag_logits[i])
deriv = sigmoid_derivitive(dag_logits[i])
logit_grad = grad * deriv
dag_logits[i] += np.clip(logit_grad, -max_grad, max_grad)
示例15: count
# 需要导入模块: from scipy import special [as 别名]
# 或者: from scipy.special import expit [as 别名]
def count(self, x):
if self.quasi:
return np.round(expit(x).sum())
else:
return x.sum()