aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-x[-rw-r--r--]src/cli.py29
-rwxr-xr-x[-rw-r--r--]src/cost.py2
-rw-r--r--src/model.py17
-rwxr-xr-x[-rw-r--r--]src/predict.py4
-rw-r--r--src/theta2
-rwxr-xr-x[-rw-r--r--]src/train.py2
6 files changed, 32 insertions, 24 deletions
diff --git a/src/cli.py b/src/cli.py
index ce759be..0c45e7a 100644..100755
--- a/src/cli.py
+++ b/src/cli.py
@@ -1,3 +1,5 @@
+#!/usr/bin/env python3.7
+
import sys
import argparse
@@ -13,25 +15,25 @@ class CommandLineInterface:
prog="ft_linear_regression_cli",
description="CLI to interact with the ft_linear_regression project"
)
- subparsers = parser.add_subparsers(help="sub-command help", dest="subparser_name")
-
+ subparsers = parser.add_subparsers(dest="subparser_name")
parser_train = subparsers.add_parser("train", help="train the model")
parser_train.set_defaults(func=self._train)
- parser_train.add_argument("-a --alpha", type=float, default=1.0, dest="alpha", help="learning rate")
- parser_train.add_argument("-e --epoch", type=int, default=100, dest="epoch", help="number of iterations")
-
+ parser_train.add_argument("-a --alpha", type=float, default=1.0,
+ dest="alpha", help="learning rate")
+ parser_train.add_argument("-e --epoch", type=int, default=100,
+ dest="epoch", help="number of iterations")
parser_predict = subparsers.add_parser("predict", help="make a predict")
parser_predict.set_defaults(func=self._predict)
- parser_predict.add_argument("-x", type=int, help="mileage for which the prediction will be made")
-
+ parser_predict.add_argument("-x", type=int,
+ help="mileage for which the prediction will be made")
parser_cost = subparsers.add_parser("cost", help="print model cost")
parser_cost.set_defaults(func=self._cost)
-
parser_plot = subparsers.add_parser("plot", help="plot data and model")
parser_plot.set_defaults(func=self._plot)
- parser_plot.add_argument("-d --data", help="only plot data", action="store_true", dest="plot_data")
- parser_plot.add_argument("-m --model", help="only plot model", action="store_true", dest="plot_model")
-
+ parser_plot.add_argument("-d --data", help="only plot data",
+ action="store_true", dest="plot_data")
+ parser_plot.add_argument("-m --model", help="only plot model",
+ action="store_true", dest="plot_model")
self.args = parser.parse_args(sys.argv[1:])
def _train(self):
@@ -40,7 +42,7 @@ class CommandLineInterface:
def _predict(self):
if self.args.x is not None:
- print(self.model.hypothesis(self.args.x))
+ print(self.model.make_prediction(self.args.x))
else:
predict.predict_input(self.model)
@@ -54,6 +56,9 @@ class CommandLineInterface:
self.model.plot(self.args.plot_data, self.args.plot_model)
def exec_args(self):
+ if self.args.subparser_name is None:
+ print("{} --help for more information".format(sys.argv[0]))
+ return
self.args.func()
diff --git a/src/cost.py b/src/cost.py
index 0f00253..0f0743b 100644..100755
--- a/src/cost.py
+++ b/src/cost.py
@@ -1,3 +1,5 @@
+#!/usr/bin/env python3.7
+
from model import Model
diff --git a/src/model.py b/src/model.py
index ee43050..0d74a69 100644
--- a/src/model.py
+++ b/src/model.py
@@ -1,6 +1,5 @@
import numpy as np
import matplotlib.pyplot as plt
-import sklearn.preprocessing
class Model:
@@ -25,6 +24,10 @@ class Model:
def hypothesis(self, x):
return x * self.theta1 + self.theta0
+ def make_prediction(self, predict):
+ predict = (predict - self.xs.min()) / (self.xs.max() - self.xs.min())
+ return self.hypothesis(predict)
+
def cost(self):
return (1 / (2 * len(self.xs))) * sum([(self.hypothesis(x) - y) ** 2
for x, y in zip(self.xs, self.ys)])
@@ -42,8 +45,8 @@ class Model:
def _plot_model(self):
line_xs = [self.xs.min(), self.xs.max()]
- line_ys = [self.hypothesis(x) for x in line_xs]
- self.ax.plot(line_xs, line_ys)
+ line_ys = [self.make_prediction(x) for x in line_xs]
+ self.ax.plot(line_xs, line_ys, color='r')
def _partial_theta1(self):
return sum([(self.hypothesis(x) - y) * x
@@ -54,7 +57,7 @@ class Model:
for x, y in zip(self.xs, self.ys)]) / len(self.xs)
def _normalize_data(self):
- self.xs, self.ys = sklearn.preprocessing.normalize([self.xs, self.ys])
+ self.xs = (self.xs - self.xs.min()) / (self.xs.max() - self.xs.min())
def _read_theta(self):
try:
@@ -72,9 +75,3 @@ class Model:
return data[:, 0], data[:, 1]
except IOError:
print(self.datafilename, "do not exist")
-
-
-if __name__ == "__main__":
- m = Model()
- m.train()
- m.write_theta()
diff --git a/src/predict.py b/src/predict.py
index 329382a..2257109 100644..100755
--- a/src/predict.py
+++ b/src/predict.py
@@ -1,3 +1,5 @@
+#!/usr/bin/env python3.7
+
from model import Model
def predict_input(m):
@@ -8,7 +10,7 @@ def predict_input(m):
print("Bad input, you should enter a number")
else:
break
- print("The predicted price for this mileage is", m.hypothesis(x))
+ print("The predicted price for this mileage is", m.make_prediction(x))
if __name__ == "__main__":
m = Model(thetafilename="./theta")
diff --git a/src/theta b/src/theta
index 768218a..dbde3b9 100644
--- a/src/theta
+++ b/src/theta
@@ -1 +1 @@
--0.19808642684030997,0.23574835208002437 \ No newline at end of file
+-4615.249629014704,7992.7771682847515 \ No newline at end of file
diff --git a/src/train.py b/src/train.py
index 0a68916..146d0ee 100644..100755
--- a/src/train.py
+++ b/src/train.py
@@ -1,3 +1,5 @@
+#!/usr/bin/env python3.7
+
from model import Model