#NEWLINE# def plot_linear_regression( X , y , model = LinearRegression ( ) , corr_func = 'pearsonr' , scattercolor = 'blue' , fit_style = 'k--' , legend = True , xlim = 'auto' ) : #NEWLINE# #INDENT# "Plot a linear regression line fit.\n\n    Parameters\n    ----------\n    X : numpy array, shape = [n_samples,]\n        Samples.\n    y : numpy array, shape (n_samples,)\n        Target values\n    model: object (default: sklearn.linear_model.LinearRegression)\n        Estimator object for regression. Must implement\n        a .fit() and .predict() method.\n    corr_func: str or function (default: 'pearsonr')\n        Uses `pearsonr` from scipy.stats if corr_func='pearsonr'.\n        to compute the regression slope. If not 'pearsonr', the `corr_func`,\n        the `corr_func` parameter expects a function of the form\n        func(<x-array>, <y-array>) as inputs, which is expected to return\n        a tuple `(<correlation_coefficient>, <some_unused_value>)`.\n    scattercolor: string (default: blue)\n        Color of scatter plot points.\n    fit_style: string (default: k--)\n        Style for the line fit.\n    legend: bool (default: True)\n        Plots legend with corr_coeff coef.,\n        fit coef., and intercept values.\n    xlim: array-like (x_min, x_max) or 'auto' (default: 'auto')\n        X-axis limits for the linear line fit.\n\n    Returns\n    ----------\n    regression_fit : tuple\n        intercept, slope, corr_coeff (float, float, float)\n\n    " #NEWLINE# if isinstance ( X , list ) : #NEWLINE# #INDENT# X = np . asarray ( X , dtype = np . float ) #NEWLINE# #UNINDENT# if isinstance ( y , list ) : #NEWLINE# #INDENT# y = np . asarray ( y , dtype = np . float ) #NEWLINE# #UNINDENT# if ( len ( X . shape ) == 1 ) : #NEWLINE# #INDENT# X = X [ : , np . newaxis ] #NEWLINE# #UNINDENT# model . fit ( X , y ) #NEWLINE# plt . scatter ( X , y , c = scattercolor ) #NEWLINE# if ( xlim == 'auto' ) : #NEWLINE# #INDENT# ( x_min , x_max ) = ( X [ : , 0 ] . min ( ) , X [ : , 0 ] . max ( ) ) #NEWLINE# x_min -= ( 0.2 * x_min ) #NEWLINE# x_max += ( 0.2 * x_max ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( x_min , x_max ) = xlim #NEWLINE# #UNINDENT# y_min = model . predict ( x_min ) #NEWLINE# y_max = model . predict ( x_max ) #NEWLINE# plt . plot ( [ x_min , x_max ] , [ y_min , y_max ] , fit_style , lw = 1 ) #NEWLINE# if ( corr_func == 'pearsonr' ) : #NEWLINE# #INDENT# corr_func = pearsonr #NEWLINE# #UNINDENT# ( corr_coeff , p ) = corr_func ( X [ : , 0 ] , y ) #NEWLINE# ( intercept , slope ) = ( model . intercept_ , model . coef_ [ 0 ] ) #NEWLINE# if legend : #NEWLINE# #INDENT# leg_text = ( 'intercept: %.2f\nslope: %.2f' % ( intercept , slope ) ) #NEWLINE# if corr_func : #NEWLINE# #INDENT# leg_text += ( '\ncorrelation: %.2f' % corr_coeff ) #NEWLINE# #UNINDENT# plt . legend ( [ leg_text ] , loc = 'best' ) #NEWLINE# #UNINDENT# regression_fit = ( intercept , slope , corr_coeff ) #NEWLINE# return regression_fit