Example #1
0
    def test_lm_fit(self):
        my_lm = lm("y~.", data=df1)
        my_lm.fit()
        self.assertEqual(my_lm.intercept, 1.0)
        self.assertEqual(my_lm.coefs[1], 3.5)

        my_lm = lm("y~.-1", data=df2)
        my_lm.fit()
        self.assertEqual(my_lm.coefs[0], 1.5)
Example #2
0
 def test_lm_predict_c(self):
     """
     This is further extension to the project, making sure the not implemented error is raised
     """
     predict_df = pd.DataFrame({"x": [0, 1]})
     my_lm = lm("y~.", data=df1)
     my_lm.fit()
     self.assertRaises(NotImplementedError,
                       lambda: my_lm.predict(predict_df, interval='c'))
Example #3
0
 def test_lm_summary(self):
     """
     testing if the summary returned has the correct formula used for the model
     """
     formula = "y~."
     my_lm = lm(formula, data=df1)
     my_lm.fit()
     summary = my_lm.summary(print_summary=False)
     lm_summary_formula = summary.splitlines()[2].split(',')[0].split(
         '=')[1].strip()
     self.assertEqual(formula, lm_summary_formula)
Example #4
0
    def test_lm_predict(self):
        """
        Testing the predicted values for the linear model, to make sure the closed form solution works as expected
        """
        predict_df = pd.DataFrame({"x": [0, 1]})
        expected_y = [1, 4.5]

        my_lm = lm("y~.", data=df1)
        my_lm.fit()
        y_hat = my_lm.predict(predict_df, interval=None)
        self.assertListEqual(y_hat.tolist(), expected_y)
Example #5
0
 def test_lm_plot_all(self):
     my_lm = lm("y~.", data=df1)
     my_lm.fit()
     self.assertRaises(NotImplementedError, lambda: my_lm.plot(which=None))
Example #6
0
    def test_lm_plot_3(self):
        """ Testing to see if the plot was created and no error happened while creating the plot"""

        my_lm = lm("y~.", data=df1)
        my_lm.fit()
        my_lm.plot(which=3)
Example #7
0
import pandas as pd

from src.utility.FormulaParser import FormulaParser
from src.models.lm import lm
from src.models.knnRegressor import knnRegressor
from src.models.knnClassifier import knnClassifier

from src.models.BaseModel import BaseModel

if __name__ == "__main__":
    model_formula = FormulaParser('dist~speed', ["speed", "dist"])

    df = pd.DataFrame({"x": [0, 0, 1, 1], "y": [0, 2, 4, 5]})
    print('Data to fit the model: \n', df)
    my_lm = lm("y~.", data=df)
    my_lm.fit()
    my_lm.summary()
    #
    df1 = pd.DataFrame({"x": [0, 0, 1, 1], "y": [0, 2, 4, 5]})
    predict_df = pd.DataFrame({"x": [0, 1]})
    my_lm = lm("y~.", data=df1)
    my_lm.fit()
    my_lm.summary()
    y_hat = my_lm.predict(predict_df)
    print(y_hat)

    my_knn = knnRegressor("y~.", data=df1)
    my_knn.fit()
    print(my_knn.predict(df1))

    df2 = pd.DataFrame({"x": [0, 1, 2, 3], "y": [0, 0, 1, 1]})