<jupyter_start><jupyter_text>Template for implementation of the simple linear regression.The code serves only for educational purposes so itis not optimized in view of speed or numerical calculations.For the real problems you should always use algorithmsfrom known,  well tested libraries, wherever possible.Author: <NAME>Date: 23.03 2018<jupyter_code>import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline
FILE_NAME = '../../datasets/data1.csv'
# functions operating on 'normal' lists
# without using any additional functions.

class MySimpleLinearRegression():
  def __init__(self):
    self._theta = [0, 0]

  def fit(self, X, Y):
    """
      Args:
        X(list): vector of numbers X^{i} i=1,...,N from the training set.
        Y(list): vector of numbers Y^{i} i=1,...,N from the training set.
      Returns:
        list: with parameters [theta0, theta1]
    """
    return [0, 0] #IMPLEMENT YOUR CODE HERE

  def getParams(self):
    """
      Returns:
        list: with parameters [theta0, theta1]
    """
    return self._theta

  def predict(self, x):
    """
      Args:
        x(number)
      Returns:
       float:  y prediction
    """
    return 0 #IMPLEMENT YOUR CODE HERE
def runTests_MySimpleLinearRegression():
  """
    Some examples how one can test your functions.
  """
  model = MySimpleLinearRegression()
  # y= 2 *x -4 no noise
  x = [0, 1, 2, 3]
  y = [-4., -2, 0, 2]
  model.fit(x, y)
  params = model.getParams()
  np.testing.assert_almost_equal(params[0], -4.)
  np.testing.assert_almost_equal(params[1], 2.)
  np.testing.assert_almost_equal(model.predict(7), 10.)

def loadData(filename):
  # We use skiprows=1 to ignore the first line in the file
  # which is just a description of columns.
  data = np.loadtxt(filename, delimiter=',', skiprows=1)
  # data matrix has three columns,
  # the third one is the dataset label =0,1,2 or 3.
  # We want to select only a subset of data corresponding to given label.
  # data[:,2] means select all rows of the third column.
  # data[data[:,2]== 0] means select all records for wich the third column is equal 0.
  dataset1 = data[data[:, 2] == 0]
  dataset2 = data[data[:, 2] == 1]
  dataset3 = data[data[:, 2] == 2]
  dataset4 = data[data[:, 2] == 3]
  return (dataset1, dataset2, dataset3, dataset4)
def plotData(data):
  """
  """
  model = MySimpleLinearRegression()
  model.fit(data[:, 0].tolist(), data[:, 1].tolist())
  yPredicted = [model.predict(x) for x in data[:, 0].tolist()]
  plt.scatter(data[:, 0], data[:, 1])
  plt.plot(data[:, 0], yPredicted)
  plt.xlim(2, 20)  # set axis X range
  plt.ylim(2, 14)  # set axis Y range
  plt.xlabel('x')
  plt.ylabel('y')
  plt.show()
datas = loadData(FILE_NAME)
for d in datas:
  plotData(d)
runTests_MySimpleLinearRegression()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13235818765608276, 'fraction_numerical': 0.026757046022119157, 'mean_word_length': 3.4864, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '409378', 'license': 'Apache-2.0', 'n_tokens_mistral': 970, 'n_tokens_neox': 928, 'n_words': 312, 'path': 'notebooks/simple_linear_regression/simple_linear_regression_template.ipynb', 'repo_name': 'wkrzemien/dataScienceAndML2020', 'size': '2812'}