本文整理汇总了Python中sklearn.linear_model.ridge.Ridge.predict方法的典型用法代码示例。如果您正苦于以下问题:Python Ridge.predict方法的具体用法?Python Ridge.predict怎么用?Python Ridge.predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.linear_model.ridge.Ridge
的用法示例。
在下文中一共展示了Ridge.predict方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_dtype_match
# 需要导入模块: from sklearn.linear_model.ridge import Ridge [as 别名]
# 或者: from sklearn.linear_model.ridge.Ridge import predict [as 别名]
def test_dtype_match():
rng = np.random.RandomState(0)
alpha = 1.0
n_samples, n_features = 6, 5
X_64 = rng.randn(n_samples, n_features)
y_64 = rng.randn(n_samples)
X_32 = X_64.astype(np.float32)
y_32 = y_64.astype(np.float32)
solvers = ["svd", "sparse_cg", "cholesky", "lsqr"]
for solver in solvers:
# Check type consistency 32bits
ridge_32 = Ridge(alpha=alpha, solver=solver)
ridge_32.fit(X_32, y_32)
coef_32 = ridge_32.coef_
# Check type consistency 64 bits
ridge_64 = Ridge(alpha=alpha, solver=solver)
ridge_64.fit(X_64, y_64)
coef_64 = ridge_64.coef_
# Do the actual checks at once for easier debug
assert coef_32.dtype == X_32.dtype
assert coef_64.dtype == X_64.dtype
assert ridge_32.predict(X_32).dtype == X_32.dtype
assert ridge_64.predict(X_64).dtype == X_64.dtype
assert_almost_equal(ridge_32.coef_, ridge_64.coef_, decimal=5)
示例2: test_dtype_match_cholesky
# 需要导入模块: from sklearn.linear_model.ridge import Ridge [as 别名]
# 或者: from sklearn.linear_model.ridge.Ridge import predict [as 别名]
def test_dtype_match_cholesky():
# Test different alphas in cholesky solver to ensure full coverage.
# This test is separated from test_dtype_match for clarity.
rng = np.random.RandomState(0)
alpha = (1.0, 0.5)
n_samples, n_features, n_target = 6, 7, 2
X_64 = rng.randn(n_samples, n_features)
y_64 = rng.randn(n_samples, n_target)
X_32 = X_64.astype(np.float32)
y_32 = y_64.astype(np.float32)
# Check type consistency 32bits
ridge_32 = Ridge(alpha=alpha, solver='cholesky')
ridge_32.fit(X_32, y_32)
coef_32 = ridge_32.coef_
# Check type consistency 64 bits
ridge_64 = Ridge(alpha=alpha, solver='cholesky')
ridge_64.fit(X_64, y_64)
coef_64 = ridge_64.coef_
# Do all the checks at once, like this is easier to debug
assert coef_32.dtype == X_32.dtype
assert coef_64.dtype == X_64.dtype
assert ridge_32.predict(X_32).dtype == X_32.dtype
assert ridge_64.predict(X_64).dtype == X_64.dtype
assert_almost_equal(ridge_32.coef_, ridge_64.coef_, decimal=5)
示例3: test_dtype_match
# 需要导入模块: from sklearn.linear_model.ridge import Ridge [as 别名]
# 或者: from sklearn.linear_model.ridge.Ridge import predict [as 别名]
def test_dtype_match(solver):
rng = np.random.RandomState(0)
alpha = 1.0
n_samples, n_features = 6, 5
X_64 = rng.randn(n_samples, n_features)
y_64 = rng.randn(n_samples)
X_32 = X_64.astype(np.float32)
y_32 = y_64.astype(np.float32)
# Check type consistency 32bits
ridge_32 = Ridge(alpha=alpha, solver=solver, max_iter=500, tol=1e-10,)
ridge_32.fit(X_32, y_32)
coef_32 = ridge_32.coef_
# Check type consistency 64 bits
ridge_64 = Ridge(alpha=alpha, solver=solver, max_iter=500, tol=1e-10,)
ridge_64.fit(X_64, y_64)
coef_64 = ridge_64.coef_
# Do the actual checks at once for easier debug
assert coef_32.dtype == X_32.dtype
assert coef_64.dtype == X_64.dtype
assert ridge_32.predict(X_32).dtype == X_32.dtype
assert ridge_64.predict(X_64).dtype == X_64.dtype
assert_allclose(ridge_32.coef_, ridge_64.coef_, rtol=1e-4)
示例4: _test_multi_ridge_diabetes
# 需要导入模块: from sklearn.linear_model.ridge import Ridge [as 别名]
# 或者: from sklearn.linear_model.ridge.Ridge import predict [as 别名]
def _test_multi_ridge_diabetes(filter_):
# simulate several responses
Y = np.vstack((y_diabetes, y_diabetes)).T
n_features = X_diabetes.shape[1]
ridge = Ridge(fit_intercept=False)
ridge.fit(filter_(X_diabetes), Y)
assert_equal(ridge.coef_.shape, (2, n_features))
Y_pred = ridge.predict(filter_(X_diabetes))
ridge.fit(filter_(X_diabetes), y_diabetes)
y_pred = ridge.predict(filter_(X_diabetes))
assert_array_almost_equal(np.vstack((y_pred, y_pred)).T, Y_pred, decimal=3)
示例5: _test_ridge_loo
# 需要导入模块: from sklearn.linear_model.ridge import Ridge [as 别名]
# 或者: from sklearn.linear_model.ridge.Ridge import predict [as 别名]
def _test_ridge_loo(filter_):
# test that can work with both dense or sparse matrices
n_samples = X_diabetes.shape[0]
ret = []
ridge_gcv = _RidgeGCV(fit_intercept=False)
ridge = Ridge(fit_intercept=False)
# generalized cross-validation (efficient leave-one-out)
K, v, Q = ridge_gcv._pre_compute(X_diabetes, y_diabetes)
errors, c = ridge_gcv._errors(v, Q, y_diabetes, 1.0)
values, c = ridge_gcv._values(K, v, Q, y_diabetes, 1.0)
# brute-force leave-one-out: remove one example at a time
errors2 = []
values2 = []
for i in range(n_samples):
sel = np.arange(n_samples) != i
X_new = X_diabetes[sel]
y_new = y_diabetes[sel]
ridge.fit(X_new, y_new)
value = ridge.predict([X_diabetes[i]])[0]
error = (y_diabetes[i] - value) ** 2
errors2.append(error)
values2.append(value)
# check that efficient and brute-force LOO give same results
assert_almost_equal(errors, errors2)
assert_almost_equal(values, values2)
# check best alpha
ridge_gcv.fit(filter_(X_diabetes), y_diabetes)
best_alpha = ridge_gcv.best_alpha
ret.append(best_alpha)
# check that we get same best alpha with custom loss_func
ridge_gcv2 = _RidgeGCV(fit_intercept=False, loss_func=mean_squared_error)
ridge_gcv2.fit(filter_(X_diabetes), y_diabetes)
assert_equal(ridge_gcv2.best_alpha, best_alpha)
# check that we get same best alpha with sample weights
ridge_gcv.fit(filter_(X_diabetes), y_diabetes,
sample_weight=np.ones(n_samples))
assert_equal(ridge_gcv.best_alpha, best_alpha)
# simulate several responses
Y = np.vstack((y_diabetes, y_diabetes)).T
ridge_gcv.fit(filter_(X_diabetes), Y)
Y_pred = ridge_gcv.predict(filter_(X_diabetes))
ridge_gcv.fit(filter_(X_diabetes), y_diabetes)
y_pred = ridge_gcv.predict(filter_(X_diabetes))
assert_array_almost_equal(np.vstack((y_pred, y_pred)).T,
Y_pred, decimal=5)
return ret
示例6: test_fit_simple_backupsklearn
# 需要导入模块: from sklearn.linear_model.ridge import Ridge [as 别名]
# 或者: from sklearn.linear_model.ridge.Ridge import predict [as 别名]
def test_fit_simple_backupsklearn():
df = pd.read_csv("./open_data/simple.txt", delim_whitespace=True)
X = np.array(df.iloc[:, :df.shape[1] - 1], dtype='float32', order='C')
y = np.array(df.iloc[:, df.shape[1] - 1], dtype='float32', order='C')
Solver = h2o4gpu.Ridge
enet = Solver(glm_stop_early=False)
print("h2o4gpu fit()")
enet.fit(X, y)
print("h2o4gpu predict()")
print(enet.predict(X))
print("h2o4gpu score()")
print(enet.score(X,y))
enet_wrapper = Solver(normalize=True, random_state=1234)
print("h2o4gpu scikit wrapper fit()")
enet_wrapper.fit(X, y)
print("h2o4gpu scikit wrapper predict()")
print(enet_wrapper.predict(X))
print("h2o4gpu scikit wrapper score()")
print(enet_wrapper.score(X, y))
from sklearn.linear_model.ridge import Ridge
enet_sk = Ridge(normalize=True, random_state=1234)
print("Scikit fit()")
enet_sk.fit(X, y)
print("Scikit predict()")
print(enet_sk.predict(X))
print("Scikit score()")
print(enet_sk.score(X, y))
enet_sk_coef = csr_matrix(enet_sk.coef_, dtype=np.float32).toarray()
print(enet_sk.coef_)
print(enet_sk_coef)
print(enet_wrapper.coef_)
print(enet_sk.intercept_)
print(enet_wrapper.intercept_)
print(enet_sk.n_iter_)
print(enet_wrapper.n_iter_)
print("Coeffs, intercept, and n_iters should match")
assert np.allclose(enet_wrapper.coef_, enet_sk_coef)
assert np.allclose(enet_wrapper.intercept_, enet_sk.intercept_)
示例7: test_toy_ridge_object
# 需要导入模块: from sklearn.linear_model.ridge import Ridge [as 别名]
# 或者: from sklearn.linear_model.ridge.Ridge import predict [as 别名]
def test_toy_ridge_object():
# Test BayesianRegression ridge classifier
# TODO: test also n_samples > n_features
X = np.array([[1], [2]])
Y = np.array([1, 2])
clf = Ridge(alpha=0.0)
clf.fit(X, Y)
X_test = [[1], [2], [3], [4]]
assert_almost_equal(clf.predict(X_test), [1., 2, 3, 4])
assert_equal(len(clf.coef_.shape), 1)
assert_equal(type(clf.intercept_), np.float64)
Y = np.vstack((Y, Y)).T
clf.fit(X, Y)
X_test = [[1], [2], [3], [4]]
assert_equal(len(clf.coef_.shape), 2)
assert_equal(type(clf.intercept_), np.ndarray)
示例8: eval_aggr_shifts
# 需要导入模块: from sklearn.linear_model.ridge import Ridge [as 别名]
# 或者: from sklearn.linear_model.ridge.Ridge import predict [as 别名]
def eval_aggr_shifts(X, y, ignore_rows):
eps = 1e-6
pred = []
real = []
for inst_n in ignore_rows:
X = np.concatenate((X[:inst_n], X[inst_n+1:]))
y = np.concatenate((y[:inst_n], y[inst_n+1:]))
n = X.shape[0]
for inst_n in range(n):
x_i = X[inst_n]
y_i = y[inst_n]
X_train = np.concatenate((X[:inst_n], X[inst_n+1:]))
y_train = np.concatenate((y[:inst_n], y[inst_n+1:]))
y_train = np.array([max(eps, min(1 - eps, val)) for val in y_train])
y_train = np.log(y_train / (1 - y_train))
model = Ridge(alpha=.2, fit_intercept=True, normalize=True)
#model = Lasso(alpha=.001, fit_intercept=True, normalize=True)
model.fit(X_train, y_train)
y_hat = model.predict(x_i.reshape(1, -1))[0]
y_i1 = max(eps, min(1 - eps, y_i))
y_i1 = np.log(y_i1 / (1 - y_i1))
print('inst: ' + str(inst_n) + ', prediction: ' + str(y_hat) + ', err: ' + str(y_hat - y_i1))
pred.append(1 / (1 + exp(-y_hat)))
real.append(y_i)
model = Ridge(alpha=.2, fit_intercept=True, normalize=True)
model.fit(X, y)
return pred, real, model.coef_
示例9: _test_ridge_loo
# 需要导入模块: from sklearn.linear_model.ridge import Ridge [as 别名]
# 或者: from sklearn.linear_model.ridge.Ridge import predict [as 别名]
def _test_ridge_loo(filter_):
# test that can work with both dense or sparse matrices
n_samples = X_diabetes.shape[0]
ret = []
ridge_gcv = _RidgeGCV(fit_intercept=False)
ridge = Ridge(alpha=1.0, fit_intercept=False)
# generalized cross-validation (efficient leave-one-out)
decomp = ridge_gcv._pre_compute(X_diabetes, y_diabetes)
errors, c = ridge_gcv._errors(1.0, y_diabetes, *decomp)
values, c = ridge_gcv._values(1.0, y_diabetes, *decomp)
# brute-force leave-one-out: remove one example at a time
errors2 = []
values2 = []
for i in range(n_samples):
sel = np.arange(n_samples) != i
X_new = X_diabetes[sel]
y_new = y_diabetes[sel]
ridge.fit(X_new, y_new)
value = ridge.predict([X_diabetes[i]])[0]
error = (y_diabetes[i] - value) ** 2
errors2.append(error)
values2.append(value)
# check that efficient and brute-force LOO give same results
assert_almost_equal(errors, errors2)
assert_almost_equal(values, values2)
# generalized cross-validation (efficient leave-one-out,
# SVD variation)
decomp = ridge_gcv._pre_compute_svd(X_diabetes, y_diabetes)
errors3, c = ridge_gcv._errors_svd(ridge.alpha, y_diabetes, *decomp)
values3, c = ridge_gcv._values_svd(ridge.alpha, y_diabetes, *decomp)
# check that efficient and SVD efficient LOO give same results
assert_almost_equal(errors, errors3)
assert_almost_equal(values, values3)
# check best alpha
ridge_gcv.fit(filter_(X_diabetes), y_diabetes)
alpha_ = ridge_gcv.alpha_
ret.append(alpha_)
# check that we get same best alpha with custom loss_func
f = ignore_warnings
scoring = make_scorer(mean_squared_error, greater_is_better=False)
ridge_gcv2 = RidgeCV(fit_intercept=False, scoring=scoring)
f(ridge_gcv2.fit)(filter_(X_diabetes), y_diabetes)
assert_equal(ridge_gcv2.alpha_, alpha_)
# check that we get same best alpha with custom score_func
func = lambda x, y: -mean_squared_error(x, y)
scoring = make_scorer(func)
ridge_gcv3 = RidgeCV(fit_intercept=False, scoring=scoring)
f(ridge_gcv3.fit)(filter_(X_diabetes), y_diabetes)
assert_equal(ridge_gcv3.alpha_, alpha_)
# check that we get same best alpha with a scorer
scorer = get_scorer('mean_squared_error')
ridge_gcv4 = RidgeCV(fit_intercept=False, scoring=scorer)
ridge_gcv4.fit(filter_(X_diabetes), y_diabetes)
assert_equal(ridge_gcv4.alpha_, alpha_)
# check that we get same best alpha with sample weights
ridge_gcv.fit(filter_(X_diabetes), y_diabetes,
sample_weight=np.ones(n_samples))
assert_equal(ridge_gcv.alpha_, alpha_)
# simulate several responses
Y = np.vstack((y_diabetes, y_diabetes)).T
ridge_gcv.fit(filter_(X_diabetes), Y)
Y_pred = ridge_gcv.predict(filter_(X_diabetes))
ridge_gcv.fit(filter_(X_diabetes), y_diabetes)
y_pred = ridge_gcv.predict(filter_(X_diabetes))
assert_array_almost_equal(np.vstack((y_pred, y_pred)).T,
Y_pred, decimal=5)
return ret
示例10: extract_target
# 需要导入模块: from sklearn.linear_model.ridge import Ridge [as 别名]
# 或者: from sklearn.linear_model.ridge.Ridge import predict [as 别名]
train1 = extract_target(train_dataset)
test0 = extract_predictor(test_dataset, False)
results = []
for cnt in range(1000):
projected0 = []
projected1 = []
for i in xrange(len(train0)):
if random.random() < 0.4:
continue
projected0.append(train0[i])
projected1.append(train1[i])
print "now fitting the model", cnt, "with len", len(projected0)
model = Ridge()
model.fit(projected0, projected1)
predictions=model.predict(test0)
results.append(list(predictions))
final_result = []
for ind in xrange(len(results[0])):
cand = []
for i in xrange(len(results)):
cand.append(results[i][ind])
final_result.append(sum(sorted(cand)[100:-100])*1.0/(len(cand)-200))
#predictions=model.predict(valid_dataset)
#Evaluate the quality of the prediction
#print sklearn.metrics.mean_absolute_error(predictions,valid_target)
print "saving the predicted result into the file"
示例11: open
# 需要导入模块: from sklearn.linear_model.ridge import Ridge [as 别名]
# 或者: from sklearn.linear_model.ridge.Ridge import predict [as 别名]
model2_train1 += virtual_test1
print "now saving the result"
ff = open('virtual_train_data.json', 'w')
ff.write(json.dumps([model2_train0, model2_train1]))
ff.close()
if sys.argv[1] == "second":
ff = open('virtual_train_data.json', 'r')
model2_train0, model2_train1 = json.loads(ff.read())
ff.close()
print "opened train0 and train1 with each length", len(model2_train0), len(model2_train1)
print model2_train0[0]
print model2_train1[0]
ff = open('intermediate_result.json', 'r')
model2_test0, _ = json.loads(ff.read())
print model2_test0[0]
model2 = Ridge()
print "start fitting 2nd model"
model2.fit(model2_train0, model2_train1)
print "start predicting"
predictions=model2.predict(model2_test0)
print "saving the predicted result into the file"
f = open('result.csv', 'w')
f.write("ID;COTIS\n");
for ind, prd in enumerate(predictions):
f.write(my_ids[ind] + ';' + str(prd) + '\n')
f.close()
print "all tasks completed"
示例12: trainModel
# 需要导入模块: from sklearn.linear_model.ridge import Ridge [as 别名]
# 或者: from sklearn.linear_model.ridge.Ridge import predict [as 别名]
def trainModel(param,feat_folder,feat_name):
#read data from folder
print 'now we read data from folder:%s'%(feat_folder)
#start cv
print 'now we need to generate cross_validation'
accuracy_cv = []
for i in range(0,2):
print 'this is the run:%d cross-validation'%(i+1)
testIndex = loadCVIndex("%s/test.run%d.txt"%("../data/feat/combine",(i+1)))
#if we use xgboost to train model ,we need to use svmlib format
if param['task'] in ['regression']:
#with xgb we will dump the file with CV,and we will read data
train_data = xgb.DMatrix("%s/run%d/train.svm.txt"%(feat_folder,(i+1)))
valid_data = xgb.DMatrix("%s/run%d/test.svm.txt"%(feat_folder,(i+1)))
watchlist = [(train_data,'train'),(valid_data,'valid')]
bst = xgb.train(param,train_data,int(param['num_round']),watchlist)
pred = bst.predict(valid_data)
elif param['task'] in ['clf_skl_lr']:
train_data,train_label = load_svmlight_file("%s/run%d/train.svm.txt"%(feat_folder,(i+1)))
test_data,test_label = load_svmlight_file("%s/run%d/test.svm.txt"%(feat_folder,(i+1)))
train_data = train_data.tocsr()
test_data = test_data.tocsr()
clf = LogisticRegression()
clf.fit(train_data,train_label)
pred = clf.predict(test_data)
elif param['task'] == "reg_skl_rf":
## regression with sklearn random forest regressor
train_data,train_label = load_svmlight_file("%s/run%d/train.svm.txt"%(feat_folder,(i+1)))
test_data,test_label = load_svmlight_file("%s/run%d/test.svm.txt"%(feat_folder,(i+1)))
rf = RandomForestRegressor(n_estimators=param['n_estimators'],
max_features=param['max_features'],
n_jobs=param['n_jobs'],
random_state=param['random_state'])
rf.fit(train_data, test_label)
pred = rf.predict(test_data)
elif param['task'] == "reg_skl_etr":
## regression with sklearn extra trees regressor
train_data,train_label = load_svmlight_file("%s/run%d/train.svm.txt"%(feat_folder,(i+1)))
test_data,test_label = load_svmlight_file("%s/run%d/test.svm.txt"%(feat_folder,(i+1)))
etr = ExtraTreesRegressor(n_estimators=param['n_estimators'],
max_features=param['max_features'],
n_jobs=param['n_jobs'],
random_state=param['random_state'])
etr.fit(train_data,test_label)
pred = etr.predict(test_data)
elif param['task'] in ['reg_skl_gbm'] :
train_data,train_label = load_svmlight_file("%s/run%d/train.svm.txt"%(feat_folder,(i+1)))
test_data,test_label = load_svmlight_file("%s/run%d/test.svm.txt"%(feat_folder,(i+1)))
gbm = GradientBoostingClassifier(n_estimators=int(param['n_estimators']),
learning_rate=param['learning_rate'],
max_features=param['max_features'],
max_depth=param['max_depth'],
subsample=param['subsample'],
random_state=param['random_state'])
feat_names.remove('cid')
gbm.fit(train_data,train_label)
pred = gbm.predict(test_data)
elif param['task'] in ['reg_skl_ridge']:
train_data,train_label = load_svmlight_file("%s/run%d/train.svm.txt"%(feat_folder,(i+1)))
test_data,test_label = load_svmlight_file("%s/run%d/test.svm.txt"%(feat_folder,(i+1)))
train_data = train_data.tocsr()
test_data = test_data.tocsr()
ridge = Ridge(alpha=param["alpha"], normalize=True)
ridge.fit(train_data,train_label)
predraw = ridge.predict(test_data)
print predraw
predrank = predraw.argsort().argsort()
trainIndex = loadCVIndex("%s/train.run%d.txt"%("../data/feat/combine",(i+1)))
cdf = creatCDF(train, trainIndex)
pred = getScore(predrank,cdf)
print pred
"""
elif param['task'] in ['regression']:
elif param['task'] in ['reg_skl_gbm'] :
gbm = GradientBoostingClassifier(n_estimators=int(param['n_estimators']),
learning_rate=param['learning_rate'],
max_features=param['max_features'],
max_depth=param['max_depth'],
subsample=param['subsample'],
random_state=param['random_state'])
feat_names.remove('cid')
gbm.fit(train_data[feat_names],train_data['cid'])
pred = gbm.predict(valid_data[feat_names])
elif param['task'] in ['reg_skl_ridge']:
feat_names.remove('cid')
ridge = Ridge(alpha=param["alpha"], normalize=True)
ridge.fit(train_data[feat_names],train_data['cid'])
pred = ridge.predict(valid_data[feat_names])
#.........这里部分代码省略.........