np.polyfit(x,y,deg=1) from scipy import optimize # Define a model function def model_func(x, a0, a1): return a0 + (a1*x) param_opt, param_cov = optimize.curve_fit(model_func, x_data, y_data) a0 = param_opt[0] # a0 is the intercept in y = a0 + a1*x a1 = param_opt[1] # a1 is the slope in y = a0 + a1*x import pandas as pd from statsmodels.formula.api import ols df = pd.DataFrame(dict(x_column=x_data, y_column=y_data)) # Pass data and `formula` into ols(), use and `.fit()` the model to the data model_fit = ols(formula="y_column ~ x_column", data=df).fit() # Use .predict(df) to get y_model values, then over-plot y_data with y_model y_model = model_fit.predict(df) # Extract the a0, a1 values from model_fit.params a0 = model_fit.params['Intercept'] a1 = model_fit.params['x_column'] uncertainty_in_intercept = model_fit.bse['Intercept'] uncertainty_in_slope = model_fit.bse['x_column] from sklearn.linear_model import LinearRegression # Initialize a general model model = LinearRegression(fit_intercept=True) # Load and shape the data x_data = x_raw.reshape(len(y_raw),1) y_data = y_raw.reshape(len(y_raw),1) # Fit the model to the data model_fit = model.fit(x_data, y_data) # Extract the linear model parameters intercept = model.intercept_[0] slope = model.coef_[0,0] # Extract the linear model parameters intercept = model.intercept_[0] slope = model.coef_[0,0]