Skip to content

Commit ee66fb9

Browse files
author
nikolas_virionis
committed
style: Some code refactorings for simpler and clear as day issues and bad practices
1 parent fe4fd8d commit ee66fb9

File tree

3 files changed

+26
-49
lines changed

3 files changed

+26
-49
lines changed

polynomial_regression/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# __init__.py
22

3-
#version
4-
__version__ = "3.1.4"
3+
#version
4+
__version__ = "3.1.5"

polynomial_regression/main.py

+23-46
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import warnings
12
import numpy as np
3+
import matplotlib.pyplot as plt
24
from pylab import *
35
from sklearn.metrics import r2_score
46
from polynomial_regression import expon, log, sinusoidal, logistic, train_test
5-
import matplotlib.pyplot as plt
6-
import warnings
77

88
warnings.filterwarnings("ignore")
99

@@ -16,10 +16,10 @@ class Regression:
1616

1717
def __init__(self, x: list, y: list, train_test: bool = False):
1818
np.random.seed(2)
19-
if not len(x) == len(y):
20-
raise Exception("Invalid input for x or y")
21-
if not len(x) > 2 and len(y) > 2:
22-
raise Exception("Invalid input size for x or y")
19+
if len(x) != len(y):
20+
raise ValueError("Invalid input for x or y")
21+
if len(x) <= 2 and len(y) > 2:
22+
raise ValueError("Invalid input size for x or y")
2323
self.__x = np.array(x)
2424
self.__y = np.array(y)
2525
self.__train_test = train_test
@@ -131,11 +131,11 @@ def __regression(self, control=True):
131131
degree = 0
132132
predict = ""
133133
coefficient = []
134-
type = ""
134+
regression_type = ""
135135
train_x, test_x, train_y, test_y = train_test.split(
136136
self.__x, self.__y, self.__train_test
137137
)
138-
for i in range(-3, 32, 1):
138+
for i in range(-3, 32):
139139
category = ""
140140
coefficients = []
141141
prediction = lambda x: 0
@@ -166,12 +166,12 @@ def __regression(self, control=True):
166166
coefficients = np.polyfit(train_x, train_y, i)
167167
prediction = np.poly1d(coefficients)
168168

169-
if round(r2_score(test_y, prediction(test_x)), 4) > 0.95:
169+
if round(r2_score(test_y, prediction(test_x)), 4) == 1:
170170
r2 = r2_score(test_y, prediction(test_x))
171171
degree = i if i not in range(-2, 0) else i + 3
172172
predict = prediction
173173
coefficient = coefficients
174-
type = category
174+
regression_type = category
175175
break
176176

177177
if round(r2, 4) < round(r2_score(test_y, prediction(test_x)), 4) - (
@@ -181,50 +181,33 @@ def __regression(self, control=True):
181181
degree = i if i not in range(-2, 0) else i + 3
182182
predict = prediction
183183
coefficient = coefficients
184-
type = category
184+
regression_type = category
185185

186-
self.__set_list_return(r2, degree, coefficient, predict, type)
186+
self.__set_list_return(r2, degree, coefficient, predict, regression_type)
187187

188188
def best_regression_model(self) -> str:
189189
"""Returns the best degree of polynomial formatted as a string"""
190-
return (
191-
"\n "
192-
+ f"The best polynomial to describe the given sets' behaviour is the {self.get_full_degree()} degree polynomial"
193-
if self.__list_return[5] == "polynomial"
194-
else "The best regression model to describe the given sets' behaviour is the exponential"
195-
if self.__list_return[5] == "expon"
196-
else "The best regression model to describe the given sets' behaviour is the logarithmic"
197-
if self.__list_return[5] == "logarithm"
198-
else "The best regression model to describe the given sets' behaviour is the sinusoidal"
199-
if self.__list_return[5] == "sinusoidal"
200-
else "The best regression model to describe the given sets' behaviour is the logistic"
201-
)
190+
return f"\n The best polynomial to describe the given sets' behaviour is the {self.get_full_degree()} degree polynomial" if self.__list_return[5] == "polynomial" else f"The best regression model to describe the given sets' behaviour is the {'exponential' if self.__list_return[5] == 'expon' else self.__list_return[5]}"
191+
192+
202193

203194
def coefficient_of_determination(self) -> str:
204195
"""Returns the coefficient of determination (R²) formatted as a string"""
205-
return "\n " + f"It has a coefficient of determination of {self.get_r2():.4f}"
196+
return f"\n It has a coefficient of determination of {self.get_r2():.4f}"
206197

207198
def __r2_interpretation(self) -> str:
208199
"""Returns the coefficient of determination interpretation if needed"""
209200
if self.get_r2() < 0.45:
210-
return (
211-
"\n"
212-
+ f"This index being low, represents it is not possible to find any reliably predictable behaviour given the previous datasets, therefore the actual accuracy for the predictions will be low and highly dependent on chance"
213-
)
201+
return "\nThis index being low, represents it is not possible to find any reliably predictable behaviour given the previous datasets, therefore the actual accuracy for the predictions will be low and highly dependent on chance"
202+
214203
if self.get_r2() < 0.6:
215-
return (
216-
"\n"
217-
+ f"This index represents the predictions will not have optimal accuracy when making predictions since the given datasets don't set up an ideal predictable behaviour"
218-
)
204+
return "\nThis index represents the predictions will not have optimal accuracy when making predictions since the given datasets don't set up an ideal predictable behaviour"
205+
219206
return ""
220207

221208
def equation_text(self) -> str:
222209
"""Returns the polinomial equation formatted as a string"""
223-
return (
224-
"\n "
225-
+ f"The equation can be written as {self.equation_string()}"
226-
+ "\n and makes predictions via the get_prediction function\n"
227-
)
210+
return f"\nThe equation can be written as {self.equation_string()}\n and makes predictions via the get_prediction function\n"
228211

229212
def correlation(self) -> float:
230213
"""returns the correlation between the two datasets"""
@@ -236,10 +219,7 @@ def correlation_way(self) -> str:
236219
if self.correlation_intensity() == "nearly independent":
237220
return "negligible way"
238221

239-
if corr > 0:
240-
return "positive way"
241-
242-
return "negative way"
222+
return "positive way" if corr > 0 else "negative way"
243223

244224
def correlation_intensity(self) -> str:
245225
"""returns the intensity by which the two datasets are correlated to each other"""
@@ -253,10 +233,7 @@ def correlation_intensity(self) -> str:
253233
if abs(corr) > 0.5:
254234
return "moderately correlated"
255235

256-
if abs(corr) > 0.3:
257-
return "barely correlated"
258-
259-
return "nearly independent"
236+
return "barely correlated" if abs(corr) > 0.3 else "nearly independent"
260237

261238
def correlation_interpretation(self) -> str:
262239
"""returns the interpretation of the correlation index,

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# This call to setup() does all the work
1111
setup(
1212
name="polynomial_regression_model",
13-
version="3.1.4",
13+
version="3.1.5",
1414
description="Python package that analyses the given datasets and comes up with the best regression representation with either the smallest polynomial degree possible, to be the most reliable without overfitting or other models such as exponentials and logarithms",
1515
long_description=README,
1616
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)