if we think for secondwhat would best fitting line look likesuch line would invariably have the least error/residuali the difference between the predicted and observed would be least for such line the ordinary least squares criterion is one such technique to identify the best fitting line the algorithm tries to minimize the error with respect to slope and intercept it uses the squared error formshown as followsq yobserved predicted whereq is the total squared error we minimize the total error to get the slope and intercept of the best fitting line training now that we have the background on linear regression and olswe'll get started with the model building the linear regression model is exposed through scikit-learn' linear_model module like all machine learning algorithms in scikitthis also works on the familiar fit(and predict(theme the following snippet prepares the linear regression object for us in [ ] train_df_new yy total_count values reshape(- , lin_reg linear_model linearregression(one simple way of proceeding would be call the fit(function to build our linear regression model and then call the predict(function on the test dataset to get the predictions for evaluation we also want to keep in mind the aspects of overfitting and reduce its affects and obtain generalizable model as discussed in the previous section and earlier cross validation is one method to keep overfitting in check we thus use the -fold cross validation (specifically -foldas shown in the following snippet in [ ]predicted cross_val_predict(lin_regxycv= the function cross_val_predict(is exposed through model_selection module of sklearn this function takes the model objectpredictorsand targets as inputs we specify the in -fold using the cv parameter in our examplewe use -fold cross validation this function returns cross validated prediction values as fitted by the model object we use scatter plot to analyze our predictions the following snippet uses matplotlib to generate scatter plot between residuals and observed values in [ ]figax plt subplots(ax scatter(yy-predictedax axhline(lw= ,color='black'ax set_xlabel('observed'ax set_ylabel('residual'plt show(