def cv_LinearRegression_It( xM, yV, n_splits = 5, scoring = 'median_absolute_error', N_it = 10, disp = False, ldisp = False):
"""
N_it times iteration is performed for cross_validation in order to make further average effect.
The flag of 'disp' is truned off so each iteration will not shown.
"""
cv_score_le = list()
for ni in range( N_it):
cv_score_l = cv_LinearRegression( xM, yV, n_splits = n_splits, scoring = scoring, disp = disp)
cv_score_le.extend( cv_score_l)
o_d = {'mean': np.mean( cv_score_le),
'std': np.std( cv_score_le),
'list': cv_score_le}
if disp or ldisp:
print('{0}: mean(+/-std) --> {1}(+/-{2})'.format( scoring, o_d['mean'], o_d['std']))
return o_d
python类median_absolute_error()的实例源码
def cv_LinearRegression_ci_It( xM, yV, n_splits = 5, scoring = 'median_absolute_error', N_it = 10, disp = False, ldisp = False):
"""
N_it times iteration is performed for cross_validation in order to make further average effect.
The flag of 'disp' is truned off so each iteration will not shown.
"""
cv_score_le = list()
ci_le = list()
for ni in range( N_it):
cv_score_l, ci_l = cv_LinearRegression_ci( xM, yV, n_splits = n_splits, scoring = scoring, disp = disp)
cv_score_le.extend( cv_score_l)
ci_le.extend( ci_l)
o_d = {'mean': np.mean( cv_score_le),
'std': np.std( cv_score_le),
'list': cv_score_le,
'ci': ci_le}
if disp or ldisp:
print('{0}: mean(+/-std) --> {1}(+/-{2})'.format( scoring, o_d['mean'], o_d['std']))
return o_d
def cv_LinearRegression_ci_pred_It( xM, yV, n_splits = 5, scoring = 'median_absolute_error', N_it = 10, disp = False, ldisp = False):
"""
N_it times iteration is performed for cross_validation in order to make further average effect.
The flag of 'disp' is truned off so each iteration will not shown.
"""
cv_score_le = list()
ci_le = list()
yVp_ltype_l = list() # yVp_ltype is list type of yVp not matrix type
for ni in range( N_it):
cv_score_l, ci_l, yVp_ltype = cv_LinearRegression_ci_pred( xM, yV, n_splits = n_splits, scoring = scoring, disp = disp)
cv_score_le.extend( cv_score_l)
ci_le.extend( ci_l)
yVp_ltype_l.append( yVp_ltype)
o_d = {'mean': np.mean( cv_score_le),
'std': np.std( cv_score_le),
'list': cv_score_le,
'ci': ci_le,
'yVp': yVp_ltype_l}
if disp or ldisp:
print('{0}: mean(+/-std) --> {1}(+/-{2})'.format( scoring, o_d['mean'], o_d['std']))
return o_d
def cv_LinearRegression_ci_It( xM, yV, n_splits = 5, scoring = 'median_absolute_error', N_it = 10, disp = False, ldisp = False):
"""
N_it times iteration is performed for cross_validation in order to make further average effect.
The flag of 'disp' is truned off so each iteration will not shown.
"""
cv_score_le = list()
ci_le = list()
for ni in range( N_it):
cv_score_l, ci_l = cv_LinearRegression_ci( xM, yV, n_splits = n_splits, scoring = scoring, disp = disp)
cv_score_le.extend( cv_score_l)
ci_le.extend( ci_l)
o_d = {'mean': np.mean( cv_score_le),
'std': np.std( cv_score_le),
'list': cv_score_le,
'ci': ci_le}
if disp or ldisp:
print('{0}: mean(+/-std) --> {1}(+/-{2})'.format( scoring, o_d['mean'], o_d['std']))
return o_d
def cv_LinearRegression_ci_pred_It( xM, yV, n_splits = 5, scoring = 'median_absolute_error', N_it = 10, disp = False, ldisp = False):
"""
N_it times iteration is performed for cross_validation in order to make further average effect.
The flag of 'disp' is truned off so each iteration will not shown.
"""
cv_score_le = list()
ci_le = list()
yVp_ltype_l = list() # yVp_ltype is list type of yVp not matrix type
for ni in range( N_it):
cv_score_l, ci_l, yVp_ltype = cv_LinearRegression_ci_pred( xM, yV, n_splits = n_splits, scoring = scoring, disp = disp)
cv_score_le.extend( cv_score_l)
ci_le.extend( ci_l)
yVp_ltype_l.append( yVp_ltype)
o_d = {'mean': np.mean( cv_score_le),
'std': np.std( cv_score_le),
'list': cv_score_le,
'ci': ci_le,
'yVp': yVp_ltype_l}
if disp or ldisp:
print('{0}: mean(+/-std) --> {1}(+/-{2})'.format( scoring, o_d['mean'], o_d['std']))
return o_d
def cv_LinearRegression_It(xM, yV, n_folds=5, scoring='median_absolute_error', N_it=10, disp=False, ldisp=False):
"""
N_it times iteration is performed for cross_validation in order to make further average effect.
The flag of 'disp' is truned off so each iteration will not shown.
"""
cv_score_le = list()
for ni in range(N_it):
cv_score_l = cv_LinearRegression(
xM, yV, n_folds=n_folds, scoring=scoring, disp=disp)
cv_score_le.extend(cv_score_l)
o_d = {'mean': np.mean(cv_score_le),
'std': np.std(cv_score_le),
'list': cv_score_le}
if disp or ldisp:
print('{0}: mean(+/-std) --> {1}(+/-{2})'.format(scoring,
o_d['mean'], o_d['std']))
return o_d
def cv_LinearRegression_ci_It(xM, yV, n_folds=5, scoring='median_absolute_error', N_it=10, disp=False, ldisp=False):
"""
N_it times iteration is performed for cross_validation in order to make further average effect.
The flag of 'disp' is truned off so each iteration will not shown.
"""
cv_score_le = list()
ci_le = list()
for ni in range(N_it):
cv_score_l, ci_l = cv_LinearRegression_ci(
xM, yV, n_folds=n_folds, scoring=scoring, disp=disp)
cv_score_le.extend(cv_score_l)
ci_le.extend(ci_l)
o_d = {'mean': np.mean(cv_score_le),
'std': np.std(cv_score_le),
'list': cv_score_le,
'ci': ci_le}
if disp or ldisp:
print('{0}: mean(+/-std) --> {1}(+/-{2})'.format(scoring,
o_d['mean'], o_d['std']))
return o_d
def gs_Ridge(xM, yV, alphas_log=(1, -1, 9), n_folds=5, n_jobs=-1, scoring='r2'):
"""
Parameters
-------------
scoring: mean_absolute_error, mean_squared_error, median_absolute_error, r2
"""
print('If scoring is not r2 but error metric, output score is revered for scoring!')
print(xM.shape, yV.shape)
clf = linear_model.Ridge()
#parmas = {'alpha': np.logspace(1, -1, 9)}
parmas = {'alpha': np.logspace(*alphas_log)}
kf_n_c = model_selection.KFold(n_splits=n_folds, shuffle=True)
kf_n = kf_n_c.split(xM)
gs = model_selection.GridSearchCV(
clf, parmas, scoring=scoring, cv=kf_n, n_jobs=n_jobs)
gs.fit(xM, yV)
return gs
jgrid (james-90X3A's conflicted copy 2016-04-21).py 文件源码
项目:jamespy_py3
作者: jskDr
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def cv_LinearRegression_It( xM, yV, n_folds = 5, scoring = 'median_absolute_error', N_it = 10, disp = False, ldisp = False):
"""
N_it times iteration is performed for cross_validation in order to make further average effect.
The flag of 'disp' is truned off so each iteration will not shown.
"""
cv_score_le = list()
for ni in range( N_it):
cv_score_l = cv_LinearRegression( xM, yV, n_folds = n_folds, scoring = scoring, disp = disp)
cv_score_le.extend( cv_score_l)
o_d = {'mean': np.mean( cv_score_le),
'std': np.std( cv_score_le),
'list': cv_score_le}
if disp or ldisp:
print('{0}: mean(+/-std) --> {1}(+/-{2})'.format( scoring, o_d['mean'], o_d['std']))
return o_d
jgrid (james-90X3A's conflicted copy 2016-04-21).py 文件源码
项目:jamespy_py3
作者: jskDr
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def cv_LinearRegression_ci_It( xM, yV, n_folds = 5, scoring = 'median_absolute_error', N_it = 10, disp = False, ldisp = False):
"""
N_it times iteration is performed for cross_validation in order to make further average effect.
The flag of 'disp' is truned off so each iteration will not shown.
"""
cv_score_le = list()
ci_le = list()
for ni in range( N_it):
cv_score_l, ci_l = cv_LinearRegression_ci( xM, yV, n_folds = n_folds, scoring = scoring, disp = disp)
cv_score_le.extend( cv_score_l)
ci_le.extend( ci_l)
o_d = {'mean': np.mean( cv_score_le),
'std': np.std( cv_score_le),
'list': cv_score_le,
'ci': ci_le}
if disp or ldisp:
print('{0}: mean(+/-std) --> {1}(+/-{2})'.format( scoring, o_d['mean'], o_d['std']))
return o_d
jgrid (james-90X3A's conflicted copy 2016-04-21).py 文件源码
项目:jamespy_py3
作者: jskDr
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def cv_LinearRegression_ci_pred_It( xM, yV, n_folds = 5, scoring = 'median_absolute_error', N_it = 10, disp = False, ldisp = False):
"""
N_it times iteration is performed for cross_validation in order to make further average effect.
The flag of 'disp' is truned off so each iteration will not shown.
"""
cv_score_le = list()
ci_le = list()
yVp_ltype_l = list() # yVp_ltype is list type of yVp not matrix type
for ni in range( N_it):
cv_score_l, ci_l, yVp_ltype = cv_LinearRegression_ci_pred( xM, yV, n_folds = n_folds, scoring = scoring, disp = disp)
cv_score_le.extend( cv_score_l)
ci_le.extend( ci_l)
yVp_ltype_l.append( yVp_ltype)
o_d = {'mean': np.mean( cv_score_le),
'std': np.std( cv_score_le),
'list': cv_score_le,
'ci': ci_le,
'yVp': yVp_ltype_l}
if disp or ldisp:
print('{0}: mean(+/-std) --> {1}(+/-{2})'.format( scoring, o_d['mean'], o_d['std']))
return o_d
def cv_LinearRegression_It( xM, yV, n_folds = 5, scoring = 'median_absolute_error', N_it = 10, disp = False, ldisp = False):
"""
N_it times iteration is performed for cross_validation in order to make further average effect.
The flag of 'disp' is truned off so each iteration will not shown.
"""
cv_score_le = list()
for ni in range( N_it):
cv_score_l = cv_LinearRegression( xM, yV, n_folds = n_folds, scoring = scoring, disp = disp)
cv_score_le.extend( cv_score_l)
o_d = {'mean': np.mean( cv_score_le),
'std': np.std( cv_score_le),
'list': cv_score_le}
if disp or ldisp:
print('{0}: mean(+/-std) --> {1}(+/-{2})'.format( scoring, o_d['mean'], o_d['std']))
return o_d
def cv_LinearRegression_ci_It( xM, yV, n_folds = 5, scoring = 'median_absolute_error', N_it = 10, disp = False, ldisp = False):
"""
N_it times iteration is performed for cross_validation in order to make further average effect.
The flag of 'disp' is truned off so each iteration will not shown.
"""
cv_score_le = list()
ci_le = list()
for ni in range( N_it):
cv_score_l, ci_l = cv_LinearRegression_ci( xM, yV, n_folds = n_folds, scoring = scoring, disp = disp)
cv_score_le.extend( cv_score_l)
ci_le.extend( ci_l)
o_d = {'mean': np.mean( cv_score_le),
'std': np.std( cv_score_le),
'list': cv_score_le,
'ci': ci_le}
if disp or ldisp:
print('{0}: mean(+/-std) --> {1}(+/-{2})'.format( scoring, o_d['mean'], o_d['std']))
return o_d
def cv_LinearRegression_ci_pred_It( xM, yV, n_folds = 5, scoring = 'median_absolute_error', N_it = 10, disp = False, ldisp = False):
"""
N_it times iteration is performed for cross_validation in order to make further average effect.
The flag of 'disp' is truned off so each iteration will not shown.
"""
cv_score_le = list()
ci_le = list()
yVp_ltype_l = list() # yVp_ltype is list type of yVp not matrix type
for ni in range( N_it):
cv_score_l, ci_l, yVp_ltype = cv_LinearRegression_ci_pred( xM, yV, n_folds = n_folds, scoring = scoring, disp = disp)
cv_score_le.extend( cv_score_l)
ci_le.extend( ci_l)
yVp_ltype_l.append( yVp_ltype)
o_d = {'mean': np.mean( cv_score_le),
'std': np.std( cv_score_le),
'list': cv_score_le,
'ci': ci_le,
'yVp': yVp_ltype_l}
if disp or ldisp:
print('{0}: mean(+/-std) --> {1}(+/-{2})'.format( scoring, o_d['mean'], o_d['std']))
return o_d
def gs_Ridge( xM, yV, alphas_log = (1, -1, 9), n_folds = 5, n_jobs = -1, scoring = 'r2'):
"""
Parameters
-------------
scoring: mean_absolute_error, mean_squared_error, median_absolute_error, r2
"""
print(xM.shape, yV.shape)
clf = linear_model.Ridge()
#parmas = {'alpha': np.logspace(1, -1, 9)}
parmas = {'alpha': np.logspace( *alphas_log)}
kf_n = cross_validation.KFold( xM.shape[0], n_folds=n_folds, shuffle=True)
gs = grid_search.GridSearchCV( clf, parmas, scoring = scoring, cv = kf_n, n_jobs = n_jobs)
gs.fit( xM, yV)
return gs
def estimate_accuracy(yEv, yEv_calc, disp = False):
"""
It was originally located in jchem. However now it is allocated here
since the functionality is more inline with jutil than jchem.
"""
r_sqr = metrics.r2_score( yEv, yEv_calc)
RMSE = np.sqrt( metrics.mean_squared_error( yEv, yEv_calc))
MAE = metrics.mean_absolute_error( yEv, yEv_calc)
DAE = metrics.median_absolute_error( yEv, yEv_calc)
if disp:
print("r^2={0:.2e}, RMSE={1:.2e}, MAE={2:.2e}, DAE={3:.2e}".format( r_sqr, RMSE, MAE, DAE))
return r_sqr, RMSE, MAE, DAE
def cv_LinearRegression( xM, yV, n_splits = 5, scoring = 'median_absolute_error', disp = False):
"""
metrics.explained_variance_score(y_true, y_pred) Explained variance regression score function
metrics.mean_absolute_error(y_true, y_pred) Mean absolute error regression loss
metrics.mean_squared_error(y_true, y_pred[, ...]) Mean squared error regression loss
metrics.median_absolute_error(y_true, y_pred) Median absolute error regression loss
metrics.r2_score(y_true, y_pred[, ...]) R^2 (coefficient of determination) regression score function.
"""
if disp:
print(xM.shape, yV.shape)
clf = linear_model.LinearRegression()
kf5_c = model_selection.KFold( n_splits=n_splits, shuffle=True)
kf5 = kf5_c.split( xM)
cv_score_l = list()
for train, test in kf5:
# clf.fit( xM[train,:], yV[train,:])
# yV is vector but not a metrix here. Hence, it should be treated as a vector
clf.fit( xM[train,:], yV[train])
yVp_test = clf.predict( xM[test,:])
if scoring == 'median_absolute_error':
cv_score_l.append( metrics.median_absolute_error(yV[test], yVp_test))
else:
raise ValueError( "{} scoring is not supported.".format( scoring))
if disp: # Now only this flag is on, the output will be displayed.
print('{}: mean, std -->'.format( scoring), np.mean( cv_score_l), np.std( cv_score_l))
return cv_score_l
def cv_LinearRegression_ci_pred( xM, yV, n_splits = 5, scoring = 'median_absolute_error', disp = False):
"""
metrics.explained_variance_score(y_true, y_pred) Explained variance regression score function
metrics.mean_absolute_error(y_true, y_pred) Mean absolute error regression loss
metrics.mean_squared_error(y_true, y_pred[, ...]) Mean squared error regression loss
metrics.median_absolute_error(y_true, y_pred) Median absolute error regression loss
metrics.r2_score(y_true, y_pred[, ...]) R^2 (coefficient of determination) regression score function.
"""
if disp:
print(xM.shape, yV.shape)
clf = linear_model.LinearRegression()
kf5_c = model_selection.KFold( n_splits=n_splits, shuffle=True)
kf5 = kf5_c.split( xM)
cv_score_l = list()
ci_l = list()
yVp = yV.copy()
for train, test in kf5:
# clf.fit( xM[train,:], yV[train,:])
# yV is vector but not a metrix here. Hence, it should be treated as a vector
clf.fit( xM[train,:], yV[train])
yVp_test = clf.predict( xM[test,:])
yVp[test] = yVp_test
# Additionally, coef_ and intercept_ are stored.
coef = np.array(clf.coef_).tolist()
intercept = np.array(clf.intercept_).tolist()
ci_l.append( (clf.coef_, clf.intercept_))
if scoring == 'median_absolute_error':
cv_score_l.append( metrics.median_absolute_error(yV[test], yVp_test))
else:
raise ValueError( "{} scoring is not supported.".format( scoring))
if disp: # Now only this flag is on, the output will be displayed.
print('{}: mean, std -->'.format( scoring), np.mean( cv_score_l), np.std( cv_score_l))
return cv_score_l, ci_l, yVp.A1.tolist()
def cv_LinearRegression_ci_pred_full_Ridge( xM, yV, alpha, n_splits = 5, shuffle=True, disp = False):
"""
Note - scoring is not used. I may used later. Not it is remained for compatibility purpose.
metrics.explained_variance_score(y_true, y_pred) Explained variance regression score function
metrics.mean_absolute_error(y_true, y_pred) Mean absolute error regression loss
metrics.mean_squared_error(y_true, y_pred[, ...]) Mean squared error regression loss
metrics.median_absolute_error(y_true, y_pred) Median absolute error regression loss
metrics.r2_score(y_true, y_pred[, ...]) R^2 (coefficient of determination) regression score function.
"""
if disp:
print(xM.shape, yV.shape)
# print( 'alpha of Ridge is', alpha)
clf = linear_model.Ridge( alpha)
kf5_c = model_selection.KFold( n_splits=n_splits, shuffle=shuffle)
kf5 = kf5_c.split( xM)
cv_score_l = list()
ci_l = list()
yVp = yV.copy()
for train, test in kf5:
# clf.fit( xM[train,:], yV[train,:])
# yV is vector but not a metrix here. Hence, it should be treated as a vector
clf.fit( xM[train,:], yV[train])
yVp_test = clf.predict( xM[test,:])
yVp[test] = yVp_test
# Additionally, coef_ and intercept_ are stored.
ci_l.append( (clf.coef_, clf.intercept_))
y_a = np.array( yV[test])[:,0]
yp_a = np.array( yVp_test)[:,0]
cv_score_l.extend( np.abs(y_a - yp_a).tolist())
return cv_score_l, ci_l, yVp.A1.tolist()
def estimate_accuracy4(yEv, yEv_calc, disp = False):
"""
It was originally located in jchem. However now it is allocated here
since the functionality is more inline with jutil than jchem.
"""
r_sqr = metrics.r2_score( yEv, yEv_calc)
RMSE = np.sqrt( metrics.mean_squared_error( yEv, yEv_calc))
MAE = metrics.mean_absolute_error( yEv, yEv_calc)
DAE = metrics.median_absolute_error( yEv, yEv_calc)
if disp:
print("r^2={0:.2e}, RMSE={1:.2e}, MAE={2:.2e}, DAE={3:.2e}".format( r_sqr, RMSE, MAE, DAE))
return r_sqr, RMSE, MAE, DAE
def eval_score( model, X_test, y_test, string = "Test", graph = False):
print()
print( "Evaluation of", string)
print('--------')
yP = model.predict(X_test)
score_r2 = metrics.r2_score(y_test, yP)
score_MedAE = metrics.median_absolute_error(y_test, yP)
print('Accuracy')
print('R2: {0:f}, MedAE: {1:f}'.format(score_r2, score_MedAE))
print()
if graph:
kutil.regress_show4( y_test, yP)
def cv_LinearRegression( xM, yV, n_splits = 5, scoring = 'median_absolute_error', disp = False):
"""
metrics.explained_variance_score(y_true, y_pred) Explained variance regression score function
metrics.mean_absolute_error(y_true, y_pred) Mean absolute error regression loss
metrics.mean_squared_error(y_true, y_pred[, ...]) Mean squared error regression loss
metrics.median_absolute_error(y_true, y_pred) Median absolute error regression loss
metrics.r2_score(y_true, y_pred[, ...]) R^2 (coefficient of determination) regression score function.
"""
if disp:
print(xM.shape, yV.shape)
clf = linear_model.LinearRegression()
kf5_c = model_selection.KFold( n_splits=n_splits, shuffle=True)
kf5 = kf5_c.split( xM)
cv_score_l = list()
for train, test in kf5:
# clf.fit( xM[train,:], yV[train,:])
# yV is vector but not a metrix here. Hence, it should be treated as a vector
clf.fit( xM[train,:], yV[train])
yVp_test = clf.predict( xM[test,:])
if scoring == 'median_absolute_error':
cv_score_l.append( metrics.median_absolute_error(yV[test], yVp_test))
else:
raise ValueError( "{} scoring is not supported.".format( scoring))
if disp: # Now only this flag is on, the output will be displayed.
print('{}: mean, std -->'.format( scoring), np.mean( cv_score_l), np.std( cv_score_l))
return cv_score_l
def cv_LinearRegression_ci( xM, yV, n_splits = 5, scoring = 'median_absolute_error', disp = False):
"""
metrics.explained_variance_score(y_true, y_pred) Explained variance regression score function
metrics.mean_absolute_error(y_true, y_pred) Mean absolute error regression loss
metrics.mean_squared_error(y_true, y_pred[, ...]) Mean squared error regression loss
metrics.median_absolute_error(y_true, y_pred) Median absolute error regression loss
metrics.r2_score(y_true, y_pred[, ...]) R^2 (coefficient of determination) regression score function.
"""
if disp:
print(xM.shape, yV.shape)
clf = linear_model.LinearRegression()
kf5_c = model_selection.KFold( n_splits=n_splits, shuffle=True)
kf5 = kf5_c.split( xM)
cv_score_l = list()
ci_l = list()
for train, test in kf5:
# clf.fit( xM[train,:], yV[train,:])
# yV is vector but not a metrix here. Hence, it should be treated as a vector
clf.fit( xM[train,:], yV[train])
yVp_test = clf.predict( xM[test,:])
# Additionally, coef_ and intercept_ are stored.
ci_l.append( (clf.coef_, clf.intercept_))
if scoring == 'median_absolute_error':
cv_score_l.append( metrics.median_absolute_error(yV[test], yVp_test))
else:
raise ValueError( "{} scoring is not supported.".format( scoring))
if disp: # Now only this flag is on, the output will be displayed.
print('{}: mean, std -->'.format( scoring), np.mean( cv_score_l), np.std( cv_score_l))
return cv_score_l, ci_l
def cv_LinearRegression_ci_pred( xM, yV, n_splits = 5, scoring = 'median_absolute_error', disp = False):
"""
metrics.explained_variance_score(y_true, y_pred) Explained variance regression score function
metrics.mean_absolute_error(y_true, y_pred) Mean absolute error regression loss
metrics.mean_squared_error(y_true, y_pred[, ...]) Mean squared error regression loss
metrics.median_absolute_error(y_true, y_pred) Median absolute error regression loss
metrics.r2_score(y_true, y_pred[, ...]) R^2 (coefficient of determination) regression score function.
"""
if disp:
print(xM.shape, yV.shape)
clf = linear_model.LinearRegression()
kf5_c = model_selection.KFold( n_splits=n_splits, shuffle=True)
kf5 = kf5_c.split( xM)
cv_score_l = list()
ci_l = list()
yVp = yV.copy()
for train, test in kf5:
# clf.fit( xM[train,:], yV[train,:])
# yV is vector but not a metrix here. Hence, it should be treated as a vector
clf.fit( xM[train,:], yV[train])
yVp_test = clf.predict( xM[test,:])
yVp[test] = yVp_test
# Additionally, coef_ and intercept_ are stored.
coef = np.array(clf.coef_).tolist()
intercept = np.array(clf.intercept_).tolist()
ci_l.append( (clf.coef_, clf.intercept_))
if scoring == 'median_absolute_error':
cv_score_l.append( metrics.median_absolute_error(yV[test], yVp_test))
else:
raise ValueError( "{} scoring is not supported.".format( scoring))
if disp: # Now only this flag is on, the output will be displayed.
print('{}: mean, std -->'.format( scoring), np.mean( cv_score_l), np.std( cv_score_l))
return cv_score_l, ci_l, yVp.A1.tolist()
def cv_LinearRegression_ci_pred_full_Ridge( xM, yV, alpha, n_splits = 5, shuffle=True, disp = False):
"""
Note - scoring is not used. I may used later. Not it is remained for compatibility purpose.
metrics.explained_variance_score(y_true, y_pred) Explained variance regression score function
metrics.mean_absolute_error(y_true, y_pred) Mean absolute error regression loss
metrics.mean_squared_error(y_true, y_pred[, ...]) Mean squared error regression loss
metrics.median_absolute_error(y_true, y_pred) Median absolute error regression loss
metrics.r2_score(y_true, y_pred[, ...]) R^2 (coefficient of determination) regression score function.
"""
if disp:
print(xM.shape, yV.shape)
# print( 'alpha of Ridge is', alpha)
clf = linear_model.Ridge( alpha)
kf5_c = model_selection.KFold( n_splits=n_splits, shuffle=shuffle)
kf5 = kf5_c.split( xM)
cv_score_l = list()
ci_l = list()
yVp = yV.copy()
for train, test in kf5:
# clf.fit( xM[train,:], yV[train,:])
# yV is vector but not a metrix here. Hence, it should be treated as a vector
clf.fit( xM[train,:], yV[train])
yVp_test = clf.predict( xM[test,:])
yVp[test] = yVp_test
# Additionally, coef_ and intercept_ are stored.
ci_l.append( (clf.coef_, clf.intercept_))
y_a = np.array( yV[test])[:,0]
yp_a = np.array( yVp_test)[:,0]
cv_score_l.extend( np.abs(y_a - yp_a).tolist())
return cv_score_l, ci_l, yVp.A1.tolist()
def cv_LinearRegression_ci_pred_full( xM, yV, n_splits = 5, shuffle=True, disp = False):
"""
Note - scoring is not used. I may used later. Not it is remained for compatibility purpose.
metrics.explained_variance_score(y_true, y_pred) Explained variance regression score function
metrics.mean_absolute_error(y_true, y_pred) Mean absolute error regression loss
metrics.mean_squared_error(y_true, y_pred[, ...]) Mean squared error regression loss
metrics.median_absolute_error(y_true, y_pred) Median absolute error regression loss
metrics.r2_score(y_true, y_pred[, ...]) R^2 (coefficient of determination) regression score function.
"""
if disp:
print(xM.shape, yV.shape)
clf = linear_model.LinearRegression()
kf5_c = model_selection.KFold( n_splits=n_splits, shuffle=shuffle)
kf5 = kf5_c.split( xM)
cv_score_l = list()
ci_l = list()
yVp = yV.copy()
for train, test in kf5:
# clf.fit( xM[train,:], yV[train,:])
# yV is vector but not a metrix here. Hence, it should be treated as a vector
clf.fit( xM[train,:], yV[train])
yVp_test = clf.predict( xM[test,:])
yVp[test] = yVp_test
# Additionally, coef_ and intercept_ are stored.
ci_l.append( (clf.coef_, clf.intercept_))
y_a = np.array( yV[test])[:,0]
yp_a = np.array( yVp_test)[:,0]
cv_score_l.extend( np.abs(y_a - yp_a).tolist())
return cv_score_l, ci_l, yVp.A1.tolist()
def estimate_accuracy4(yEv, yEv_calc, disp = False):
r_sqr = metrics.r2_score( yEv, yEv_calc)
RMSE = np.sqrt( metrics.mean_squared_error( yEv, yEv_calc))
MAE = metrics.mean_absolute_error( yEv, yEv_calc)
DAE = metrics.median_absolute_error( yEv, yEv_calc)
if disp:
print("r^2={0:.2e}, RMSE={1:.2e}, MAE={2:.2e}, DAE={3:.2e}".format( r_sqr, RMSE, MAE, DAE))
return r_sqr, RMSE, MAE, DAE
def cv_LinearRegression_ci(xM, yV, n_folds=5, scoring='median_absolute_error', disp=False):
"""
metrics.explained_variance_score(y_true, y_pred) Explained variance regression score function
metrics.mean_absolute_error(y_true, y_pred) Mean absolute error regression loss
metrics.mean_squared_error(y_true, y_pred[, ...]) Mean squared error regression loss
metrics.median_absolute_error(y_true, y_pred) Median absolute error regression loss
metrics.r2_score(y_true, y_pred[, ...]) R^2 (coefficient of determination) regression score function.
"""
if disp:
print(xM.shape, yV.shape)
clf = linear_model.LinearRegression()
kf5_c = model_selection.KFold(n_splits=n_folds, shuffle=True)
kf5 = kf5_c.split(xM)
cv_score_l = list()
ci_l = list()
for train, test in kf5:
# clf.fit( xM[train,:], yV[train,:])
# yV is vector but not a metrix here. Hence, it should be treated as a
# vector
clf.fit(xM[train, :], yV[train])
yVp_test = clf.predict(xM[test, :])
# Additionally, coef_ and intercept_ are stored.
ci_l.append((clf.coef_, clf.intercept_))
if scoring == 'median_absolute_error':
cv_score_l.append(
metrics.median_absolute_error(yV[test], yVp_test))
else:
raise ValueError("{} scoring is not supported.".format(scoring))
if disp: # Now only this flag is on, the output will be displayed.
print('{}: mean, std -->'.format(scoring),
np.mean(cv_score_l), np.std(cv_score_l))
return cv_score_l, ci_l
def cv_LinearRegression_ci_pred_full_Ridge(xM, yV, alpha, n_folds=5, shuffle=True, disp=False):
"""
Note - scoring is not used. I may used later. Not it is remained for compatibility purpose.
metrics.explained_variance_score(y_true, y_pred) Explained variance regression score function
metrics.mean_absolute_error(y_true, y_pred) Mean absolute error regression loss
metrics.mean_squared_error(y_true, y_pred[, ...]) Mean squared error regression loss
metrics.median_absolute_error(y_true, y_pred) Median absolute error regression loss
metrics.r2_score(y_true, y_pred[, ...]) R^2 (coefficient of determination) regression score function.
"""
if disp:
print(xM.shape, yV.shape)
# print( 'alpha of Ridge is', alpha)
clf = linear_model.Ridge(alpha)
kf5_c = model_selection.KFold(n_splits=n_folds, shuffle=shuffle)
kf5 = kf5_c.split(xM)
cv_score_l = list()
ci_l = list()
yVp = yV.copy()
for train, test in kf5:
# clf.fit( xM[train,:], yV[train,:])
# yV is vector but not a metrix here. Hence, it should be treated as a
# vector
clf.fit(xM[train, :], yV[train])
yVp_test = clf.predict(xM[test, :])
yVp[test] = yVp_test
# Additionally, coef_ and intercept_ are stored.
ci_l.append((clf.coef_, clf.intercept_))
y_a = np.array(yV[test])[:, 0]
yp_a = np.array(yVp_test)[:, 0]
cv_score_l.extend(np.abs(y_a - yp_a).tolist())
return cv_score_l, ci_l, yVp.A1.tolist()
def cv_LinearRegression_ci_pred_full(xM, yV, n_folds=5, shuffle=True, disp=False):
"""
Note - scoring is not used. I may used later. Not it is remained for compatibility purpose.
metrics.explained_variance_score(y_true, y_pred) Explained variance regression score function
metrics.mean_absolute_error(y_true, y_pred) Mean absolute error regression loss
metrics.mean_squared_error(y_true, y_pred[, ...]) Mean squared error regression loss
metrics.median_absolute_error(y_true, y_pred) Median absolute error regression loss
metrics.r2_score(y_true, y_pred[, ...]) R^2 (coefficient of determination) regression score function.
"""
if disp:
print(xM.shape, yV.shape)
clf = linear_model.LinearRegression()
kf5_c = model_selection.KFold(n_splits=n_folds, shuffle=shuffle)
kf5 = kf5_c.split(xM)
cv_score_l = list()
ci_l = list()
yVp = yV.copy()
for train, test in kf5:
# clf.fit( xM[train,:], yV[train,:])
# yV is vector but not a metrix here. Hence, it should be treated as a
# vector
clf.fit(xM[train, :], yV[train])
yVp_test = clf.predict(xM[test, :])
yVp[test] = yVp_test
# Additionally, coef_ and intercept_ are stored.
ci_l.append((clf.coef_, clf.intercept_))
y_a = np.array(yV[test])[:, 0]
yp_a = np.array(yVp_test)[:, 0]
cv_score_l.extend(np.abs(y_a - yp_a).tolist())
return cv_score_l, ci_l, yVp.A1.tolist()