title: Using normalized weights for least squares
body: <p>I have three arrays: x, y and signal strength. I would like to fit line to x and y using <code>signal_strength</code> array as weights. The idea is to give more weights to samples with higher <code>signal_strength</code>, which is in arbitrary units. I tried the following:</p>
  <pre><code>%matplotlib notebook
  from matplotlib import pyplot as plt
  import numpy as np
  from sklearn import linear_model
  
  noise = np.random.uniform(-20,20,10)
  x = np.linspace(0,100,10) + noise
  y = np.linspace(0,50,10) + noise
  
  signal_strength = [52., 42., 29., 6., 25, 9, 78, 2, 19, 49]
  
  x = x.reshape(-1,1)
  
  model = linear_model.LinearRegression()
  model_weights = linear_model.LinearRegression()
  
  model.fit(x, y)
  model_weights.fit(x, y, sample_weight=signal_strength)
  
  line_X = np.arange(x.min(), x.max())[:, np.newaxis]
  line_y = model.predict(line_X)
  line_y_weights = model_weights.predict(line_X)
  print(&quot;LR&quot;, model.coef_)
  print(&quot;LR + weights&quot;, model_weights.coef_)
  
  plt.scatter(x,y)
  plt.plot(line_X, line_y, color=&quot;red&quot;, label=&quot;LR&quot;)
  plt.plot(line_X, line_y_weights, color=&quot;blue&quot;, label=&quot;LR + weights&quot;)
  
  plt.legend(loc=&quot;lower right&quot;)
  plt.show()
  </code></pre>
  <p>The result:</p>
  <p><a href="https://i.stack.imgur.com/kdP9q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kdP9q.png" alt="" /></a></p>
  <p>My question is, do I need to normalize the signal strength by dividing all values by the maximum? and what is the maximum and minimum values I can use as weights?</p>
  <p>I would like to understand how <code>sample_weight</code> works.</p>