本文整理汇总了Python中sklearn.linear_model.ElasticNet.predict方法的典型用法代码示例。如果您正苦于以下问题:Python ElasticNet.predict方法的具体用法?Python ElasticNet.predict怎么用?Python ElasticNet.predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.linear_model.ElasticNet
的用法示例。
在下文中一共展示了ElasticNet.predict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_ElasticNet
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import predict [as 别名]
def check_ElasticNet(X, y, pred, tol, reg_alpha, reg_lambda, weights):
enet = ElasticNet(alpha=reg_alpha + reg_lambda,
l1_ratio=reg_alpha / (reg_alpha + reg_lambda))
enet.fit(X, y)
enet_pred = enet.predict(X)
assert np.isclose(weights, enet.coef_, rtol=tol, atol=tol).all()
assert np.isclose(enet_pred, pred, rtol=tol, atol=tol).all()
示例2: elasticNet
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import predict [as 别名]
def elasticNet(X,y):
print("\n### ~~~~~~~~~~~~~~~~~~~~ ###")
print("Lasso Regression")
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
myDegree = 40
polynomialFeatures = PolynomialFeatures(degree=myDegree, include_bias=False)
Xp = polynomialFeatures.fit_transform(X)
myScaler = StandardScaler()
scaled_Xp = myScaler.fit_transform(Xp)
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
elasticNet = ElasticNet(alpha=1e-7,l1_ratio=0.5)
elasticNet.fit(scaled_Xp,y)
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
dummyX = np.arange(0,2,0.01)
dummyX = dummyX.reshape((dummyX.shape[0],1))
dummyXp = polynomialFeatures.fit_transform(dummyX)
scaled_dummyXp = myScaler.transform(dummyXp)
dummyY = elasticNet.predict(scaled_dummyXp)
outputFILE = 'plot-elasticNet.png'
fig, ax = plt.subplots()
fig.set_size_inches(h = 6.0, w = 10.0)
ax.axis([0,2,0,15])
ax.scatter(X,y,color="black",s=10.0)
ax.plot(dummyX, dummyY, color='red', linewidth=1.5)
plt.savefig(filename = outputFILE, bbox_inches='tight', pad_inches=0.2, dpi = 600)
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
return( None )
示例3: enet
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import predict [as 别名]
def enet(a):
print ("Doing elastic net")
clf3 = ElasticNet(alpha=a)
clf3.fit(base_X, base_Y)
print ("Score = %f" % clf3.score(base_X, base_Y))
clf3_pred = clf3.predict(X_test)
write_to_file("elastic.csv", clf3_pred)
示例4: report_ff_en
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import predict [as 别名]
def report_ff_en():
# Fastfood approximation of Gaussian kernel
para = FastfoodPara(n, d)
st = time()
PHI_train, _ = FastfoodForKernel(trainData, para, sgm)
elapsed_ff_kern_train = time() - st
st = time()
PHI_valid, _ = FastfoodForKernel(validationData, para, sgm)
elapsed_ff_kern_valid = time() - st
# Train elastic net on projected training data
en = ElasticNet()
st = time()
en.fit(PHI_train.T, trainLabels)
elapsed_en_fit = time() - st
# Predict labels for projected validation data
st = time()
y_pred = en.predict(PHI_valid.T)
elapsed_en_pred = time() - st
# Report performance
mse_proj = metrics.mean_squared_error(validationLabels, y_pred)
# print("For projected data, MSE = {:0.4g}.".format(mse_proj))
return mse_proj, elapsed_en_fit, elapsed_ff_kern_train
示例5: enet_granger_causality_test
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import predict [as 别名]
def enet_granger_causality_test(X_t, y_t, top_df, max_iter=10000000):
"""
Return the cv-parameters tested across the whole data
:param X_t:
:param y_t:
:param top_df:
:return: res_df, test_betas
"""
test_errs = np.zeros(len(top_df))
scores = np.zeros(len(top_df))
dfs = np.zeros(len(top_df))
test_coefs = np.zeros((len(top_df), X_t.shape[1]))
for i in range(len(top_df)):
alpha = top_df.iloc[i]["alpha"]
lambda_min = top_df.iloc[i]["lambda.min"]
enet = ElasticNet(l1_ratio=alpha, alpha=lambda_min, max_iter=max_iter)
enet.fit(X_t, y_t)
y_pred = enet.predict(X_t)
test_errs[i] = np.average((y_t - y_pred)**2)
scores[i] = enet.score(X_t, y_t)
test_coefs[i] = enet.coef_
dfs[i] = len(np.where(enet.coef_)[0])
top_df["test_err"] = test_errs
top_df["score"] = scores
top_df["df"] = dfs
return top_df, test_coefs
示例6: fit_enet
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import predict [as 别名]
def fit_enet(train_X, train_y, test_X):
"""
Use linear regression to predict. Elastic net is LR with L1 and L2
regularisation.
:param train_X:
:param train_y:
:param test_X:
:return:
"""
enet = ElasticNet()
enet.fit(train_X, train_y)
model = "ElasticNet int %.2f coefs %s" % (enet.intercept_, pprint(enet.coef_))
yhat_train = enet.predict(train_X)
yhat_test = enet.predict(test_X)
return model, yhat_train, yhat_test
示例7: ElasticNetRegression
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import predict [as 别名]
def ElasticNetRegression(input_dict):
# from sklearn.datasets import load_iris
# from sklearn import tree
# iris = load_iris()
# clf = tree.DecisionTreeClassifier()
# clf = clf.fit(iris.data, iris.target)
from sklearn.datasets import load_diabetes
dta = load_diabetes()
n_sample = dta.data
n_feature = dta.target
print "*******SAMPLES********"
print n_sample
print "******FEARTURES*******"
print n_feature
from sklearn.linear_model import ElasticNet
rgs = ElasticNet().fit(n_sample, n_feature)
print rgs
print rgs.predict(n_sample)
示例8: fit_model_12
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import predict [as 别名]
def fit_model_12(self,toWrite=False):
model = ElasticNet(alpha=1.0)
for data in self.cv_data:
X_train, X_test, Y_train, Y_test = data
model.fit(X_train,Y_train)
pred = model.predict(X_test)
print("Model 12 score %f" % (logloss(Y_test,pred),))
if toWrite:
f2 = open('model12/model.pkl','w')
pickle.dump(model,f2)
f2.close()
示例9: predict_linear
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import predict [as 别名]
def predict_linear(self, enet=True):
"""How well can we do on this SRFF with a linear regression
(with optional elastic-net regularisation)?"""
if enet:
clf = ElasticNet()
else:
clf = LinearRegression()
# we have to transpose X here because sklearn uses the
# opposite order (rows v columns). maybe this is a sign that
# I'm using the wrong order.
clf.fit(self.train_X.T, self.train_y)
yhat = clf.predict(self.test_X.T)
err = self.defn(self.test_y, yhat)
return clf.intercept_, clf.coef_, err
示例10: report_orig_en
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import predict [as 别名]
def report_orig_en():
# Train elastic net on original training data
en = ElasticNet()
st = time()
en.fit(trainData.T, trainLabels)
elapsed_en_fit = time() - st
# Predict labels for original validation data
st = time()
y_pred = en.predict(validationData.T)
elapsed_en_pred = time() - st
# Report performance
mse_orig = metrics.mean_squared_error(validationLabels, y_pred)
return mse_orig, elapsed_en_fit, 0.
示例11: create_ml_classifier
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import predict [as 别名]
def create_ml_classifier(df):
import operator
X = np.array(df.drop('base_ip_release',1))
y = np.array(df['base_ip_release'])
#clf = LinearRegression()
clf = ElasticNet(alpha=1,l1_ratio=0.5)
#clf = Ridge(alpha=2)
# train_X,test_X,train_y,test_y = cross_validation.train_test_split(X,y,train_size=0.9)
#
#
# sc = StandardScaler()
# sc.fit(train_X)
# X_train_std = sc.transform(train_X)
# X_test_std = sc.transform(test_X)
#
# clf.fit(X_train_std,train_y)
# print clf.predict(X_test_std)
# print accuracy_score(test_y,clf.predict(X_test_std))
c = np.zeros(len(X)/10)
kf = k(len(y),n_folds=10)
c = 0
min_dict = {}
get_error = []
for train,test in kf:
get_clif = clf.fit(X[train],y[train])
p = clf.predict(X[test])
#print p
e = (p - y[test])
#print e, len(e)
t = np.dot(e,e)
# print t
c += t
# print c
#print p, y[test]
min_dict[t] = get_clif
get_error.append(t)
#print min_dict
min_error = min(get_error)
print sorted(min_dict.items(),key=operator.itemgetter(0))
print min_dict[min_error]
print c
print np.sqrt(c/len(X))
return min_dict[min_error]
示例12: assert_regression_result
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import predict [as 别名]
def assert_regression_result(results, tol):
regression_results = [r for r in results if
r["param"]["objective"] == "reg:linear"]
for res in regression_results:
X = scale(res["dataset"].X,
with_mean=isinstance(res["dataset"].X, np.ndarray))
y = res["dataset"].y
reg_alpha = res["param"]["alpha"]
reg_lambda = res["param"]["lambda"]
pred = res["bst"].predict(xgb.DMatrix(X))
weights = xgb_get_weights(res["bst"])[1:]
enet = ElasticNet(alpha=reg_alpha + reg_lambda,
l1_ratio=reg_alpha / (reg_alpha + reg_lambda))
enet.fit(X, y)
enet_pred = enet.predict(X)
assert np.isclose(weights, enet.coef_, rtol=tol,
atol=tol).all(), (weights, enet.coef_)
assert np.isclose(enet_pred, pred, rtol=tol, atol=tol).all(), (
res["dataset"].name, enet_pred[:5], pred[:5])
示例13: Lasso
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import predict [as 别名]
def Lasso():
from sklearn.linear_model import Lasso
from sklearn.metrics import r2_score
alpha = 0.1
lasso = Lasso(alpha=alpha)
trainDat = shortData
trainLab = shortLabels
lassoPred = lasso.fit(trainDat,trainLab)
labPredict = lassoPred.predict(testDat)
r2val = r2_score(testLab,labPredict)
print(lasso)
print "r^2 for lasso testing is: ", r2val
from sklearn.linear_model import ElasticNet
enet = ElasticNet(alpha=alpha, l1_ratio = 0.7)
enetPred = enet.fit(trainDat, trainLab)
labPredict_enet = enet.predict(testDat)
r2val_enet = r2_score(testLab, labPredict_enet)
print enet
print "r^2 for enet testing is: ", r2val_enet
示例14: load_svmlight_file
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import predict [as 别名]
# It is made available under the MIT License
import numpy as np
from sklearn.datasets import load_svmlight_file
from sklearn.cross_validation import KFold
from sklearn.linear_model import ElasticNet
from sklearn.metrics import mean_squared_error, r2_score
data, target = load_svmlight_file('data/E2006.train')
# Edit the lines below if you want to switch method:
# met = LinearRegression(fit_intercept=True)
met = ElasticNet(fit_intercept=True, alpha=.1)
kf = KFold(len(target), n_folds=5)
pred = np.zeros_like(target)
for train, test in kf:
met.fit(data[train], target[train])
pred[test] = met.predict(data[test])
print('[EN 0.1] RMSE on testing (5 fold), {:.2}'.format(np.sqrt(mean_squared_error(target, pred))))
print('[EN 0.1] R2 on testing (5 fold), {:.2}'.format(r2_score(target, pred)))
print('')
met.fit(data, target)
pred = met.predict(data)
print('[EN 0.1] RMSE on training, {:.2}'.format(np.sqrt(mean_squared_error(target, pred))))
print('[EN 0.1] R2 on training, {:.2}'.format(r2_score(target, pred)))
示例15: load_boston
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import predict [as 别名]
from sklearn.cross_validation import KFold
from sklearn.linear_model import LinearRegression, ElasticNet
import numpy as np
from sklearn.datasets import load_boston
boston = load_boston()
x = np.array([np.concatenate((v, [1])) for v in boston.data])
y = boston.target
FIT_EN = False
if FIT_EN:
model = ElasticNet(fit_intercept=True, alpha=0.5)
else:
model = LinearRegression(fit_intercept=True)
model.fit(x, y)
p = np.array([model.predict(xi) for xi in x])
e = p - y
total_error = np.dot(e, e)
rmse_train = np.sqrt(total_error / len(p))
kf = KFold(len(x), n_folds=10)
err = 0
for train, test in kf:
model.fit(x[train], y[train])
p = np.array([model.predict(xi) for xi in x[test]])
e = p - y[test]
err += np.dot(e, e)
rmse_10cv = np.sqrt(err / len(x))
print('RMSE on training: {}'.format(rmse_train))
print('RMSE on 10-fold CV: {}'.format(rmse_10cv))