本文整理汇总了Python中sklearn.utils.validation.assert_all_finite函数的典型用法代码示例。如果您正苦于以下问题:Python assert_all_finite函数的具体用法?Python assert_all_finite怎么用?Python assert_all_finite使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了assert_all_finite函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _scrub_x
def _scrub_x(self, X, missing, **kwargs):
'''
Sanitize input predictors and extract column names if appropriate.
'''
# Check for sparseness
if sparse.issparse(X):
raise TypeError('A sparse matrix was passed, but dense data '
'is required. Use X.toarray() to convert to dense.')
# Figure out missingness
if missing is None:
# Infer missingness
missing = np.isnan(X)
# Convert to internally used data type
missing = np.asarray(missing, dtype=BOOL, order='F')
assert_all_finite(missing)
if missing.ndim == 1:
missing = missing[:, np.newaxis]
X = np.asarray(X, dtype=np.float64, order='F')
if not self.allow_missing:
try:
assert_all_finite(X)
except ValueError:
raise ValueError("Input contains NaN, infinity or a value that's too large. Did you mean to set allow_missing=True?")
if X.ndim == 1:
X = X[:, np.newaxis]
# Ensure correct number of columns
if hasattr(self, 'basis_') and self.basis_ is not None:
if X.shape[1] != self.basis_.num_variables:
raise ValueError('Wrong number of columns in X')
return X, missing
开发者ID:Panadaren,项目名称:py-earth,代码行数:34,代码来源:earth.py
示例2: predict_proba
def predict_proba(self, X):
""" Predict label probabilities with the fitted estimator
on predictor(s) X.
Returns
-------
proba : array of shape = [n_samples]
The predicted label probabilities of the input samples.
"""
proba = []
X_subs = self._get_subdata(X)
for i in range(self.n_classes_):
e = self.estimators_[i]
X_i = X_subs[i]
pred = e.predict(X_i).reshape(-1, 1)
proba.append(pred)
proba = np.hstack(proba)
normalizer = proba.sum(axis=1)[:, np.newaxis]
normalizer[normalizer == 0.0] = 1.0
proba /= normalizer
assert_all_finite(proba)
return proba
开发者ID:swarbrickjones,项目名称:RandomActsOfPizzaKaggle,代码行数:27,代码来源:stacking.py
示例3: _make_meta
def _make_meta(self, X):
rows = []
for e in self.estimators_:
proba = e.predict_proba(X)
assert_all_finite(proba)
rows.append(proba)
return np.hstack(rows)
开发者ID:AlexInTown,项目名称:otto,代码行数:7,代码来源:stacking.py
示例4: predict
def predict(self, X):
"""
Perform regression on an array of test vectors X.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Returns
-------
p : array, shape = [n_samples]
Predicted target values for X
"""
try:
assert_all_finite(self.coef_)
pred = safe_sparse_dot(X, self.coef_.T)
except ValueError:
n_samples = X.shape[0]
n_vectors = self.coef_.shape[0]
pred = np.zeros((n_samples, n_vectors))
if not self.outputs_2d_:
pred = pred.ravel()
return pred
开发者ID:shockley,项目名称:lightning,代码行数:25,代码来源:sgd.py
示例5: test_suppress_validation
def test_suppress_validation():
X = np.array([0, np.inf])
assert_raises(ValueError, assert_all_finite, X)
sklearn.set_config(assume_finite=True)
assert_all_finite(X)
sklearn.set_config(assume_finite=False)
assert_raises(ValueError, assert_all_finite, X)
开发者ID:daniel-perry,项目名称:scikit-learn,代码行数:7,代码来源:test_validation.py
示例6: _fit_diag
def _fit_diag(self, pairs, y):
"""Learn diagonal metric using MMC.
Parameters
----------
X : (n x d) data matrix
each row corresponds to a single instance
constraints : 4-tuple of arrays
(a,b,c,d) indices into X, with (a,b) specifying similar and (c,d)
dissimilar pairs
"""
num_dim = pairs.shape[2]
pos_pairs, neg_pairs = pairs[y == 1], pairs[y == -1]
s_sum = np.sum((pos_pairs[:, 0, :] - pos_pairs[:, 1, :]) ** 2, axis=0)
it = 0
error = 1.0
eps = 1e-6
reduction = 2.0
w = np.diag(self.A_).copy()
while error > self.convergence_threshold and it < self.max_iter:
fD0, fD_1st_d, fD_2nd_d = self._D_constraint(neg_pairs, w)
obj_initial = np.dot(s_sum, w) + self.diagonal_c * fD0
fS_1st_d = s_sum # first derivative of the similarity constraints
gradient = fS_1st_d - self.diagonal_c * fD_1st_d # gradient of the objective
hessian = -self.diagonal_c * fD_2nd_d + eps * np.eye(num_dim) # Hessian of the objective
step = np.dot(np.linalg.inv(hessian), gradient)
# Newton-Rapshon update
# search over optimal lambda
lambd = 1 # initial step-size
w_tmp = np.maximum(0, w - lambd * step)
obj = (np.dot(s_sum, w_tmp) + self.diagonal_c *
self._D_objective(neg_pairs, w_tmp))
assert_all_finite(obj)
obj_previous = obj + 1 # just to get the while-loop started
inner_it = 0
while obj < obj_previous:
obj_previous = obj
w_previous = w_tmp.copy()
lambd /= reduction
w_tmp = np.maximum(0, w - lambd * step)
obj = (np.dot(s_sum, w_tmp) + self.diagonal_c *
self._D_objective(neg_pairs, w_tmp))
inner_it += 1
assert_all_finite(obj)
w[:] = w_previous
error = np.abs((obj_previous - obj_initial) / obj_previous)
if self.verbose:
print('mmc iter: %d, conv = %f' % (it, error))
it += 1
self.A_ = np.diag(w)
self.transformer_ = transformer_from_metric(self.A_)
return self
开发者ID:all-umass,项目名称:metric-learn,代码行数:60,代码来源:mmc.py
示例7: _svd
def _svd(self, array, n_components, n_discard):
"""Returns first `n_components` left and right singular
vectors u and v, discarding the first `n_discard`.
"""
if self.svd_method == "randomized":
kwargs = {}
if self.n_svd_vecs is not None:
kwargs["n_oversamples"] = self.n_svd_vecs
u, _, vt = randomized_svd(array, n_components, random_state=self.random_state, **kwargs)
elif self.svd_method == "arpack":
u, _, vt = svds(array, k=n_components, ncv=self.n_svd_vecs)
if np.any(np.isnan(vt)):
# some eigenvalues of A * A.T are negative, causing
# sqrt() to be np.nan. This causes some vectors in vt
# to be np.nan.
_, v = eigsh(safe_sparse_dot(array.T, array), ncv=self.n_svd_vecs)
vt = v.T
if np.any(np.isnan(u)):
_, u = eigsh(safe_sparse_dot(array, array.T), ncv=self.n_svd_vecs)
assert_all_finite(u)
assert_all_finite(vt)
u = u[:, n_discard:]
vt = vt[n_discard:]
return u, vt.T
开发者ID:VirgileFritsch,项目名称:scikit-learn,代码行数:27,代码来源:spectral.py
示例8: test_gibbs_smoke
def test_gibbs_smoke():
"""Check if we don't get NaNs sampling the full digits dataset."""
rng = np.random.RandomState(42)
X = Xdigits.astype(np.float32)
rbm1 = BernoulliRBM(X.shape[1], n_hidden=42, batch_size=40,
n_iter=20, random_state=rng)
rbm1.fit(X)
X_sampled = rbm1.gibbs(X)
assert_all_finite(X_sampled)
开发者ID:stachon,项目名称:binet,代码行数:9,代码来源:test_rbm.py
示例9: test_gibbs_smoke
def test_gibbs_smoke():
""" just seek if we don't get NaNs sampling the full digits dataset """
rng = np.random.RandomState(42)
X = Xdigits
rbm1 = BernoulliRBM(n_components=42, batch_size=10,
n_iter=20, random_state=rng)
rbm1.fit(X)
X_sampled = rbm1.gibbs(X)
assert_all_finite(X_sampled)
开发者ID:Ashatz,项目名称:scikit-learn,代码行数:9,代码来源:test_rbm.py
示例10: test_gibbs_smoke
def test_gibbs_smoke():
"""Check if we don't get NaNs sampling the full digits dataset.
Also check that sampling again will yield different results."""
X = Xdigits
rbm1 = BernoulliRBM(n_components=42, batch_size=40, n_iter=20, random_state=42)
rbm1.fit(X)
X_sampled = rbm1.gibbs(X)
assert_all_finite(X_sampled)
X_sampled2 = rbm1.gibbs(X)
assert_true(np.all((X_sampled != X_sampled2).max(axis=1)))
开发者ID:amitmse,项目名称:scikit-learn,代码行数:10,代码来源:test_rbm.py
示例11: custom_svd
def custom_svd(array, n_components, n_discard,n_svd_vecs):
u, _, vt = svds(array, k=n_components, ncv=n_svd_vecs)
if np.any(np.isnan(vt)):
_, v = eigsh(safe_sparse_dot(array.T, array),ncv=n_svd_vecs)
vt = v.T
if np.any(np.isnan(u)):
_, u = eigsh(safe_sparse_dot(array, array.T),ncv=n_svd_vecs)
assert_all_finite(u)
assert_all_finite(vt)
u = u[:, n_discard:]
vt = vt[n_discard:]
return u, vt.T
开发者ID:rupam13081,项目名称:BDAProject2016,代码行数:12,代码来源:BiclusteringWithoutSpark.py
示例12: fit
def fit(self, X, y):
"""Fit model according to X and y.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples
and n_features is the number of features.
y : array-like, shape = [n_samples] or [n_samples, n_targets]
Target values.
Returns
-------
self : regressor
Returns self.
"""
rs = check_random_state(self.random_state)
ds = get_dataset(X)
n_samples = ds.get_n_samples()
n_features = ds.get_n_features()
self.outputs_2d_ = len(y.shape) == 2
if self.outputs_2d_:
Y = y
else:
Y = y.reshape(-1, 1)
Y = np.asfortranarray(Y)
n_vectors = Y.shape[1]
self.coef_ = np.zeros((n_vectors, n_features), dtype=np.float64)
self.intercept_ = np.zeros(n_vectors, dtype=np.float64)
loss = self._get_loss()
penalty = self._get_penalty()
for k in xrange(n_vectors):
_binary_sgd(self,
self.coef_, self.intercept_, k,
ds, Y[:, k], loss, penalty, self.alpha,
self._get_learning_rate(),
self.eta0, self.power_t,
self.fit_intercept,
self.intercept_decay,
int(self.max_iter * n_samples), self.shuffle, rs,
self.callback, self.n_calls, self.verbose)
try:
assert_all_finite(self.coef_)
except ValueError:
warnings.warn("coef_ contains infinite values")
return self
开发者ID:shockley,项目名称:lightning,代码行数:53,代码来源:sgd.py
示例13: predict
def predict(self, X):
try:
assert_all_finite(self.coef_)
pred = safe_sparse_dot(X, self.coef_.T)
except ValueError:
n_samples = X.shape[0]
n_vectors = self.coef_.shape[0]
pred = np.zeros((n_samples, n_vectors))
if not self.outputs_2d_:
pred = pred.ravel()
return pred
开发者ID:aurora1625,项目名称:lightning,代码行数:13,代码来源:sgd.py
示例14: test_cd_linear_trivial
def test_cd_linear_trivial():
# trivial example that failed due to gh#4
loss = Squared()
alpha = 1e-5
n_features = 100
x = np.zeros((1, n_features))
x[0, 1] = 1
y = np.ones(1)
cb = Callback(x, y, alpha)
w = _fit_linear(x, y, alpha, n_iter=20, loss=loss, callback=cb)
assert_all_finite(w)
assert_all_finite(cb.losses_)
开发者ID:Saikrishna41,项目名称:polylearn,代码行数:13,代码来源:test_cd_linear.py
示例15: fit
def fit(self, X, y):
rs = check_random_state(self.random_state)
reencode = self.multiclass
y, n_classes, n_vectors = self._set_label_transformers(y, reencode)
ds = get_dataset(X)
n_samples = ds.get_n_samples()
n_features = ds.get_n_features()
self.coef_ = np.zeros((n_vectors, n_features), dtype=np.float64)
self.intercept_ = np.zeros(n_vectors, dtype=np.float64)
loss = self._get_loss()
penalty = self._get_penalty()
if n_vectors == 1 or not self.multiclass:
Y = np.asfortranarray(self.label_binarizer_.fit_transform(y),
dtype=np.float64)
for i in xrange(n_vectors):
_binary_sgd(self,
self.coef_, self.intercept_, i,
ds, Y[:, i], loss, penalty,
self.alpha,
self._get_learning_rate(),
self.eta0, self.power_t,
self.fit_intercept,
self.intercept_decay,
int(self.max_iter * n_samples), self.shuffle, rs,
self.callback, self.n_calls, self.verbose)
elif self.multiclass:
_multiclass_sgd(self, self.coef_, self.intercept_,
ds, y.astype(np.int32), loss, penalty,
self.alpha, self._get_learning_rate(),
self.eta0, self.power_t, self.fit_intercept,
self.intercept_decay,
int(self.max_iter * n_samples),
self.shuffle, rs, self.callback, self.n_calls,
self.verbose)
else:
raise ValueError("Wrong value for multiclass.")
try:
assert_all_finite(self.coef_)
except ValueError:
warnings.warn("coef_ contains infinite values")
return self
开发者ID:aurora1625,项目名称:lightning,代码行数:50,代码来源:sgd.py
示例16: main
def main(name, num, useSpecial = False):
labels = []
with open("C:/MissingWord/corrScoring/"+name+"Labels.txt", "r") as f:
for line in f:
labels.append(float(line))
features = []
with open("C:/MissingWord/corrScoring/1000features.txt", "r") as f:
for line in f:
features.append([float(elem) for elem in line.split(",")])
specialFeatures = getSpecialFeatures(len(features))
if useSpecial:
for i in range(min(len(specialFeatures), len(features))):
features[i].extend(specialFeatures[i])
features = features[:num]
labels = labels[:num]
for i in range(len(features)):
if len(features[i]) != len(features[0]):
print(i)
try:
assert_all_finite(features[i])
except:
print(i)
cutoff = int(len(features) * 7 / 10)
trainFeatures = features[:cutoff]
testFeatures = features[cutoff:]
trainLabels = labels[:cutoff]
testLabels = labels[cutoff:]
#regr = svm.SVR(C=1)
regr = RandomForestRegressor(n_estimators = 300, n_jobs = 7)
#regr = linear_model.LinearRegression()
regr.fit(trainFeatures, trainLabels)
print("Train Residual sum of squares: %.2f"% np.mean((regr.predict(trainFeatures) - trainLabels) ** 2))
print("Test Residual sum of squares: %.2f"% np.mean((regr.predict(testFeatures) - testLabels) ** 2))
print('Variance score: %.2f' % regr.score(testFeatures, testLabels))
with open("C:/MissingWord/corrScoring/"+name+".regr", "wb") as f:
pickle.dump(regr, f)
开发者ID:seokhohong,项目名称:missing-word,代码行数:50,代码来源:testRegression.py
示例17: _scrub
def _scrub(self, X, y, sample_weight, **kwargs):
'''
Sanitize input data.
'''
# Check for sparseness
if sparse.issparse(y):
raise TypeError('A sparse matrix was passed, but dense data '
'is required. Use y.toarray() to convert to dense.')
if sparse.issparse(sample_weight):
raise TypeError('A sparse matrix was passed, but dense data '
'is required. Use sample_weight.toarray()'
'to convert to dense.')
# Check whether X is the output of patsy.dmatrices
if y is None and isinstance(X, tuple):
y, X = X
# Handle X separately
X = self._scrub_x(X, **kwargs)
# Convert y to internally used data type
y = np.asarray(y, dtype=np.float64)
assert_all_finite(y)
y = y.reshape(y.shape[0])
# Deal with sample_weight
if sample_weight is None:
sample_weight = np.ones(y.shape[0], dtype=y.dtype)
else:
sample_weight = np.asarray(sample_weight)
assert_all_finite(sample_weight)
sample_weight = sample_weight.reshape(sample_weight.shape[0])
# Make sure dimensions match
if y.shape[0] != X.shape[0]:
raise ValueError('X and y do not have compatible dimensions.')
if y.shape != sample_weight.shape:
raise ValueError(
'y and sample_weight do not have compatible dimensions.')
# Make sure everything is finite
assert_all_finite(X)
assert_all_finite(y)
assert_all_finite(sample_weight)
return X, y, sample_weight
开发者ID:aleon1138,项目名称:py-earth,代码行数:46,代码来源:earth.py
示例18: predict_proba
def predict_proba(self, X):
proba = []
X_subs = self._get_subdata(X)
for i in range(self.n_classes_):
e = self.estimators_[i]
X_i = X_subs[i]
pred = e.predict(X_i).reshape(-1, 1)
proba.append(pred)
proba = np.hstack(proba)
normalizer = proba.sum(axis=1)[:, np.newaxis]
normalizer[normalizer == 0.0] = 1.0
proba /= normalizer
assert_all_finite(proba)
return proba
开发者ID:AlexInTown,项目名称:otto,代码行数:19,代码来源:stacking.py
示例19: _scrub_x
def _scrub_x(self, X, **kwargs):
'''
Sanitize input predictors and extract column names if appropriate.
'''
# Check for sparseness
if sparse.issparse(X):
raise TypeError('A sparse matrix was passed, but dense data '
'is required. Use X.toarray() to convert to dense.')
# Convert to internally used data type
X = np.asarray(X, dtype=np.float64, order='F')
assert_all_finite(X)
if X.ndim == 1:
X = X[:, np.newaxis]
# Ensure correct number of columns
if hasattr(self, 'basis_') and self.basis_ is not None:
if X.shape[1] != self.basis_.num_variables:
raise ValueError('Wrong number of columns in X')
return X
开发者ID:Biodun,项目名称:py-earth,代码行数:21,代码来源:earth.py
示例20: _base_estimator_predict
def _base_estimator_predict(self, e, X):
"""Predict label values with the specified estimator on predictor(s) X.
Parameters
----------
e : int
The estimator object.
X : np.ndarray, shape=(n, m)
The feature data for which to compute the predicted outputs.
Returns
-------
pred : np.ndarray, shape=(len(X), 1)
The mean of the label probabilities predicted by the specified
estimator for each fold for each instance X.
"""
# Generate array for the base-level testing set, which is n x n_folds.
pred = e.predict(X)
assert_all_finite(pred)
return pred
开发者ID:EdwardBetts,项目名称:awesome-kagg-ml,代码行数:21,代码来源:Stack.py
注:本文中的sklearn.utils.validation.assert_all_finite函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论