本文整理汇总了Python中sklearn.linear_model.ElasticNet类的典型用法代码示例。如果您正苦于以下问题:Python ElasticNet类的具体用法?Python ElasticNet怎么用?Python ElasticNet使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ElasticNet类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_ElasticNet
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
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: report_ff_en
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
示例4: enet
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)
示例5: enet_granger_causality_test
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
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
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: __init__
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
示例9: fit_model_12
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()
示例10: predict_linear
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
示例11: sklean_linear_model_elastic_net
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)
示例12: report_orig_en
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.
示例13: fit_enet
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
示例14: create_ml_classifier
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]
示例15: ElasticNetRegression
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)