本文整理汇总了Python中sklearn.linear_model.ElasticNet.fit方法的典型用法代码示例。如果您正苦于以下问题:Python ElasticNet.fit方法的具体用法?Python ElasticNet.fit怎么用?Python ElasticNet.fit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.linear_model.ElasticNet
的用法示例。
在下文中一共展示了ElasticNet.fit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: elasticNet
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import fit [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 )
示例2: check_ElasticNet
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import fit [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()
示例3: enet
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import fit [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 fit [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 fit [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: elastic_net
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import fit [as 别名]
def elastic_net(self):
enet = ElasticNet()
# features = ['season', 'holiday', 'workingday', 'weather', 'humidity', 'temp', 'windspeed', 'hour', 'month', 'year', 'day_of_week']
features = ['season', 'workingday', 'weather', 'humidity', 'windspeed', 'hour', 'month', 'year', 'day_of_week']
enet = ElasticNetCV()
enet.fit(self.train[features], self.train['log-count'])
return self.predict(enet, "Elastic Net", features)
示例7: train_model
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import fit [as 别名]
def train_model(features_filename):
training_data = np.loadtxt(features_filename, delimiter=",")
X = training_data[:, :-1]
y = training_data[:, -1]
model = ElasticNet(alpha=1.0, l1_ratio=0.5, fit_intercept=True,
precompute='auto', rho=None)
model.fit(X, y)
return model
示例8: fit_model_12
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import fit [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 fit [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: sklean_linear_model_elastic_net
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import fit [as 别名]
def sklean_linear_model_elastic_net():
en = ElasticNet(fit_intercept=True, alpha=0.5)
boston = load_boston()
x = boston.data
y = boston.target
kf = KFold(len(x), n_folds=10)
err = 0
for train, test in kf:
en.fit(x[train], y[train])
p = map(en.predict, x[test])
e = p - y[test]
err += np.sum(e * e)
rmse_10cv = np.sqrt(err / len(x))
print "RMSE on 10-fold CV: {}".format(rmse_10cv)
示例11: report_orig_en
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import fit [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.
示例12: fit_enet
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import fit [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
示例13: assert_regression_result
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import fit [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])
示例14: __init__
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import fit [as 别名]
def __init__(self, Dict_TrainingData, Flt_Lambda, Flt_L1):
# Only for two class
# Dict_Trainingdata
# Key : 0,1
# Row : data
self.Data1 = Dict_TrainingData[0] # N by 256 matrix
self.Data2 = Dict_TrainingData[1] # V by 256 matrix
self.Dim = len(self.Data1[0]) # 256
self.X = np.concatenate((self.Data1, self.Data2), axis=0) # N / V augmented matrix
self.X = self.X - np.mean(self.X,axis=0)
self.NumClass1 = len(self.Data1) # N
self.NumClass2 = len(self.Data2) # V
self.TotalNum = self.NumClass1 + self.NumClass2
self.Y = self.Construct_Y()
self.D = np.dot(np.transpose(self.Y), self.Y) / float(self.TotalNum) # P
self.Q = np.ones((2,1))
InitialTheta = np.array([2,5])
I = np.eye(2)
Theta = np.dot(I - np.dot(np.dot(self.Q, np.transpose(self.Q)), self.D ), InitialTheta)
Theta /= np.sqrt(np.dot(np.dot(np.transpose(Theta), self.D), Theta))
MaxIter = 10000
PrevTheta = InitialTheta
PrevB = np.ones(self.Dim)
for idx in range(MaxIter):
NewResp = np.dot(self.Y, Theta)
elas = ElasticNet(alpha=Flt_Lambda, l1_ratio=Flt_L1)
#
# # Compute Coefficient
# B = lasso.fit(X=self.X, y= NewResp).coef_
B = elas.fit(X=self.X, y= NewResp).coef_
# print B
#
# New OptScore
Part1 = I - np.dot(np.dot(self.Q, np.transpose(self.Q)),self.D)
Part2 = np.dot(Part1, np.linalg.inv(self.D))
Part3 = np.dot(Part2, np.transpose(self.Y))
WaveTheta = np.dot(np.dot(Part3, self.X), B)
# print WaveTheta
Theta = WaveTheta / np.sqrt(np.dot(np.dot(np.transpose(WaveTheta),self.D),WaveTheta))
if np.sum(np.abs(B - PrevB)) < 1e-6:
break
else:
PrevB = B
# print B
self.B = B
示例15: lasso
# 需要导入模块: from sklearn.linear_model import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.ElasticNet import fit [as 别名]
def lasso(filename, x_train_orig, x_devel_orig, x_test_orig, lab_train_orig, lab_devel_orig, lab_test_orig):
# Normalize the data
scaler_data = preprocessing.StandardScaler().fit(x_train_orig.toarray())
x_train = scaler_data.transform(x_train_orig.toarray())
x_devel = scaler_data.transform(x_devel_orig.toarray())
x_test = scaler_data.transform(x_test_orig.toarray())
scaler_lab = preprocessing.StandardScaler().fit(lab_train_orig)
lab_train = scaler_lab.transform(lab_train_orig)
lab_devel = scaler_lab.transform(lab_devel_orig)
lab_test = scaler_lab.transform(lab_test_orig)
# Elastic Net
clf = ElasticNet(alpha = 0.025, l1_ratio = 0.7)
clf.fit (x_train, lab_train)
nz = (clf.coef_ != 0)
# Se guardan los ficheros de parametros resultantes
dump_svmlight_file(x_train_orig[:, nz], lab_train_orig, filename+"_elasso.train.libsvm", zero_based=False, comment=None, query_id=None)
dump_svmlight_file(x_devel_orig[:, nz], lab_devel_orig, filename+"_elasso.devel.libsvm", zero_based=False, comment=None, query_id=None)
dump_svmlight_file(x_test_orig[:, nz], lab_test_orig, filename+"_elasso.test.libsvm", zero_based=False, comment=None, query_id=None)