title: Need help understanding Python linear regression model code issue (sklearn)
body: <p>I'm working on my first Linear Regression code using a Tech with Tim video (<a href="https://www.youtube.com/watch?v=45ryDIPHdGg" rel="nofollow noreferrer">https://www.youtube.com/watch?v=45ryDIPHdGg</a>) and have run into a snag. I'm using the UCI student data from here: <a href="https://archive.ics.uci.edu/ml/datasets/Student+Performance" rel="nofollow noreferrer">https://archive.ics.uci.edu/ml/datasets/Student+Performance</a></p>
  <p>My initial model code ran fine. Then I iterated to find an optimal accuracy model, which was fine. Where it starts to go off the rails is that I tried to then inject those optimal coefficients into a new model, then run two predictions:</p>
  <ol>
  <li><p>The very first model (pre-optimization loop)</p>
  </li>
  <li><p>The optimized model</p>
  </li>
  </ol>
  <p>Against the same x_test1 data set. To compare the two, I simply summed the squared difference between predicted and actual y values. Then I also recorded the final accuracy of both models.</p>
  <p>I've done something wrong because the accuracy of my new &quot;optimized&quot; model is the same or lower as the very first model, and the difference values is very similar as well. I expected the optimized model to have much less error and a higher accuracy.</p>
  <p>Can someone help me to see the error? I suspect the error lies after the plot section of code. Thanks in advance, code below.</p>
  <pre><code># Import libraries
  import pandas as pd
  import numpy as np
  import sklearn
  import pickle
  import matplotlib.pyplot as plt
  from sklearn import linear_model
  from math import sqrt
  from sklearn.linear_model import LinearRegression
  from matplotlib import style
  
  # from sklearn.utils import shuffle
  
  # Read in Data
  data = pd.read_csv(&quot;student-mat.csv&quot;, sep=&quot;;&quot;)
  
  # Slice data to include only desired headings
  data = data[[&quot;G1&quot;, &quot;G2&quot;, &quot;G3&quot;, &quot;studytime&quot;, &quot;failures&quot;, &quot;absences&quot;]]
  
  # Define the attribute we are trying to predict; called &quot;label&quot;.
  # Others are &quot;features&quot; and used to predict label
  predict = &quot;G3&quot;
  
  # Create array of features and label
  X = np.array(data.drop([predict], 1))
  y = np.array(data[predict])
  
  # Split data into training and testing data.  90% used for training, 10% testing
  # Test size 0.1 = 10% of array size
  x_train1, x_test1, y_train1, y_test1 = sklearn.model_selection.train_test_split(X, y, test_size=0.1)
  
  # Create 1st linear model and fit
  linear = linear_model.LinearRegression()
  linear.fit(x_train1, y_train1)
  
  # Compute accuracy of model
  acc = linear.score(x_test1, y_test1)
  
  # Iterate for a given number of times (max_iter) to find an optimal accuracy value and record best coefficients
  loop_num = 1
  max_iter = 1000
  best_acc = acc
  best_coef = linear.coef_
  best_int = linear.intercept_
  acc_counter = [acc]
  
  print(&quot;\nInitial Accuracy: %4.3f&quot; % acc)
  
  while loop_num &lt; max_iter + 1:
      x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(X, y, test_size=0.1)
      linear2 = linear_model.LinearRegression()
      linear2.fit(x_train, y_train)
      acc = linear2.score(x_test, y_test)
      acc_counter.append(acc)
      print(&quot;\nAccuracy of run &quot; + str(loop_num) + &quot; is: %4.3f&quot; % acc)
      if acc &gt; best_acc:
          print(&quot;\n\tBetter accuracy found.&quot;)
          best_acc = acc
          best_coef = linear2.coef_
          best_int = linear2.intercept_
          print(&quot;Co: \n&quot;, linear2.coef_)
          print(&quot;Intercept: \n&quot;, linear2.intercept_)
      else:
          print(&quot;\n\tFit Discarded.&quot;)
      loop_num += 1
  
  print(&quot;\nBest Acccuracy: \n%4.3f&quot; % best_acc)
  print(&quot;\nBest Coefficients: \n&quot;, best_coef)
  print(&quot;\nBest Intercept: \n&quot;, best_int)
  
  # Plot Accuracy over time
  x_scale = []
  for x in range(max_iter + 1):
      x_scale.append(x)
  
  plt.plot(x_scale, acc_counter, color='green', linestyle='dashed', linewidth=3, marker='o',
           markerfacecolor='blue', markersize=5)
  
  ymax = max(acc_counter)
  ymin = min(acc_counter)
  xpos = acc_counter.index(ymax)
  xmax = x_scale[xpos]
  annot_max_acc = str(ymax)
  plt.annotate('Max Accuracy = ' + annot_max_acc[0:4], xy=(xmax, ymax), xycoords='data', xytext=(.8, .95),
               textcoords='axes fraction',
               arrowprops=dict(facecolor='black', shrink=0.05), horizontalalignment='right', verticalalignment='top')
  plt.ylim(ymin, 1.0)
  plt.xlabel('Run Number')
  plt.ylabel('Accuracy')
  plt.title('Prediction Accuracy over Time')
  plt.show()
  
  # Create model with best coefficients from above
  new_model = linear_model.LinearRegression()
  new_model.intercept_ = best_int
  new_model.coef_ = best_coef
  
  # Predict y values for 1st model (not best) then compute difference between predictions and actual values
  print(&quot;\n\n\nBREAK&quot;)
  comp = []
  predictions = linear.predict(x_test1)
  for x in range(len(predictions)):
      print(predictions[x], x_test1[x], y_test1[x])
      diff = sqrt((predictions[x] - y_test1[x])**2)
      print(&quot;\tDifference is &quot;, diff)
      comp.append(diff)
  print(&quot;\n\n\nBREAK&quot;)
  print(comp)
  print(&quot;\nSum of errors is &quot;, sum(comp))
  
  # Predict y values of best model (with optimal coefficients from above) using same x_test1 values as 1st model
  # then compute difference between predictions and actual values
  print(&quot;\n\n\nBREAK&quot;)
  comp2 = []
  predictions_new_model = new_model.predict(x_test1)
  for x in range(len(predictions_new_model)):
      print(predictions_new_model[x], x_test1[x], y_test1[x])
      diff2 = sqrt((predictions_new_model[x] - y_test1[x])**2)
      print(&quot;\tDifference is &quot;, diff2)
      comp2.append(diff2)
  
  print(&quot;\n\n\nBREAK&quot;)
  print(comp2)
  print(&quot;\nSum of errors is &quot;, sum(comp2))
  
  print(&quot;\n\n\nFirst model fit difference: &quot;, sum(comp))
  print(&quot;\nSecond model fit difference &quot;, sum(comp2))
  
  print('\n1st model score: ',linear.score(x_train1, y_train1))
  
  print('\nBest model score: ',new_model.score(x_train1, y_train1))
  </code></pre>