sklearn-fluent 0.4.1__py3-none-any.whl → 0.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
sklearn_fluent/main.py CHANGED
@@ -1,46 +1,153 @@
1
- def req(xlist, ylist, linearreg):
1
+ def req(xlist, ylist):
2
2
  from sklearn.linear_model import LinearRegression
3
+ from sklearn.preprocessing import PolynomialFeatures
4
+ from sklearn.pipeline import make_pipeline
5
+ from sklearn.model_selection import train_test_split
3
6
  import numpy as np
4
7
 
5
- if linearreg == True:
6
- if len(ylist) > 50:
7
- from sklearn.model_selection import train_test_split
8
- x_train, x_test, y_train, y_test = train_test_split(np.array(xlist).reshape(-1, 1), np.array(ylist).reshape(-1, 1), test_size=0.2)
9
- model = LinearRegression()
10
- model.fit(x_train, y_train)
11
- accuracy = round(model.score(x_test, y_test))
12
-
13
- x_train = np.array(xlist).reshape(-1, 1)
14
- y_train = np.array(ylist).reshape(-1, 1)
15
- model = LinearRegression()
16
- model.fit(x_train, y_train)
17
- elif linearreg == False:
18
- x_train = np.array(xlist)
19
- y_train = np.array(ylist)
20
- model = LinearRegression()
21
- model.fit(x_train, y_train)
22
-
23
- a = model.intercept_
24
- b = model.coef_
25
- letters = list('abcdefghijklmnopqrstuvwxyz')
26
- reqletters = []
27
- for i in range(0, len(b)):
28
- reqletters.append(letters[i])
29
- newvars = []
30
- for i in range(len(reqletters)):
31
- try:
32
- new = str(round(b[0][i], 4)) + reqletters[i] # Extract single element
33
- except:
34
- new = str(round(float(b[0][0]), 4)) + reqletters[i] # Extract single element
35
- newvars.append(new)
36
- try:
37
- mainvar = round(a[0], 4) # Extract single element
38
- except:
39
- mainvar = round(float(a[0]), 4)
40
- newvars.append(mainvar)
41
- last = " + ".join(list(map(str, newvars)))
42
-
43
- try:
44
- return f"function: {last}\naccuracy: {accuracy * 100}%"
45
- except:
46
- return f"function: {last}"
8
+ x_arr = np.array(xlist)
9
+ y_arr = np.array(ylist)
10
+
11
+ best_model = None
12
+ best_score = -float('inf')
13
+ best_degree = 0 # 0 for multi-linear, 1,2,3 for poly
14
+ final_poly_feature_names = None
15
+
16
+ # Determine if xlist is for a single feature or multiple features
17
+ is_single_feature = False
18
+ if x_arr.ndim == 1:
19
+ is_single_feature = True
20
+ x_processed_for_poly = x_arr.reshape(-1, 1)
21
+ elif x_arr.ndim == 2 and x_arr.shape[1] == 1:
22
+ is_single_feature = True
23
+ x_processed_for_poly = x_arr
24
+ else: # Multiple features
25
+ x_processed_linear = x_arr
26
+
27
+ if is_single_feature:
28
+ possible_degrees = []
29
+ if len(y_arr) >= 2: possible_degrees.append(1)
30
+ if len(y_arr) >= 3: possible_degrees.append(2)
31
+ if len(y_arr) >= 4: possible_degrees.append(3) # Max degree set to 3 for simplicity
32
+
33
+ if not possible_degrees and len(y_arr) >=2 : # Should at least have degree 1 if enough points
34
+ possible_degrees.append(1)
35
+ elif not possible_degrees:
36
+ return "Error: Not enough data points to fit a model."
37
+
38
+ for degree in possible_degrees:
39
+ current_pipeline = make_pipeline(
40
+ PolynomialFeatures(degree=degree, include_bias=False),
41
+ LinearRegression()
42
+ )
43
+
44
+ score_on_test = len(y_arr) > 50
45
+ if score_on_test:
46
+ x_train, x_test, y_train, y_test = train_test_split(
47
+ x_processed_for_poly, y_arr, test_size=0.2, random_state=42
48
+ )
49
+ current_pipeline.fit(x_train, y_train)
50
+ current_score = current_pipeline.score(x_test, y_test)
51
+ # Re-fit on full data for final model parameters if this is the best model
52
+ else:
53
+ current_pipeline.fit(x_processed_for_poly, y_arr)
54
+ current_score = current_pipeline.score(x_processed_for_poly, y_arr)
55
+
56
+ if current_score > best_score:
57
+ best_score = current_score
58
+ best_degree = degree
59
+ # If scored on test, refit on full data to get final coefficients
60
+ if score_on_test:
61
+ current_pipeline.fit(x_processed_for_poly, y_arr)
62
+ best_model = current_pipeline
63
+
64
+ if best_model:
65
+ final_poly_feature_names = best_model.named_steps['polynomialfeatures'].get_feature_names_out()
66
+ model_for_coeffs = best_model.named_steps['linearregression']
67
+ else: # Fallback if no model was chosen (should not happen if possible_degrees is populated)
68
+ return "Error: Could not determine a suitable model."
69
+
70
+ else: # Multiple Linear Regression
71
+ best_degree = 0 # Mark as multi-linear
72
+ model_for_coeffs = LinearRegression()
73
+ score_on_test = len(y_arr) > 50
74
+ if score_on_test:
75
+ x_train, x_test, y_train, y_test = train_test_split(
76
+ x_processed_linear, y_arr, test_size=0.2, random_state=42
77
+ )
78
+ # Fit a separate model for scoring, then fit the main model on all data
79
+ scoring_model = LinearRegression()
80
+ scoring_model.fit(x_train, y_train)
81
+ best_score = scoring_model.score(x_test, y_test)
82
+ model_for_coeffs.fit(x_processed_linear, y_arr) # Fit on full data
83
+ else:
84
+ model_for_coeffs.fit(x_processed_linear, y_arr)
85
+ best_score = model_for_coeffs.score(x_processed_linear, y_arr)
86
+ best_model = model_for_coeffs # The model itself is the LinearRegression instance
87
+
88
+ accuracy = best_score
89
+ a_raw = best_model.intercept_ if hasattr(best_model, 'intercept_') else best_model.named_steps['linearregression'].intercept_
90
+ b_raw = best_model.coef_ if hasattr(best_model, 'coef_') else best_model.named_steps['linearregression'].coef_
91
+
92
+ if hasattr(b_raw, 'flatten'):
93
+ b_flat = b_raw.flatten()
94
+ else:
95
+ b_flat = np.array([b_raw])
96
+
97
+ num_coeffs = b_flat.shape[0]
98
+ equation_parts = []
99
+
100
+ if best_degree > 0: # Polynomial (single original feature)
101
+ # Coefficients from b_flat correspond to feature names from final_poly_feature_names
102
+ # e.g., names: ['x0', 'x0^2', 'x0^3'], coeffs: [c_for_x, c_for_x^2, c_for_x^3]
103
+ for i in range(num_coeffs):
104
+ coeff_val = b_flat[i]
105
+ # Use feature name but replace 'x0' with 'x' for display
106
+ # and handle powers, e.g., x0 -> x, x0^2 -> x^2
107
+ feature_term = final_poly_feature_names[i].replace('x0', 'x')
108
+ if feature_term == 'x^1': # if PolynomialFeatures outputs x0^1
109
+ feature_term = 'x'
110
+
111
+ # For highest power term first in equation string (optional, current is ascending power from polyfeatures)
112
+ # This loop iterates based on PolynomialFeatures output order (usually x, x^2, x^3)
113
+ # To reverse for display: iterate b_flat and final_poly_feature_names in reverse.
114
+ # For now, using PolyFeatures order: c1x + c2x^2 + ...
115
+
116
+ # Standard display: c_n*x^n + ... + c_1*x. Coeffs are typically for x, x^2, ...
117
+ # Let's stick to the order from PolynomialFeatures for simplicity of matching coeffs.
118
+ # Term will be like "coeff*x", "coeff*x^2"
119
+ current_term_display = feature_term
120
+
121
+ if i == 0: # First coefficient term
122
+ equation_parts.append(f"{round(coeff_val, 4)}{current_term_display}")
123
+ else:
124
+ sign = "+" if coeff_val >= 0 else "-"
125
+ equation_parts.append(f"{sign} {round(abs(coeff_val), 4)}{current_term_display}")
126
+
127
+ else: # Multiple Linear Regression (best_degree == 0)
128
+ letters = list('abcdefghijklmnopqrstuvwxyz')
129
+ reqletters = [letters[i % len(letters)] for i in range(num_coeffs)]
130
+ if num_coeffs > 0:
131
+ equation_parts.append(f"{round(b_flat[0], 4)}{reqletters[0]}")
132
+ for i in range(1, num_coeffs):
133
+ coeff_val = b_flat[i]
134
+ sign = "+" if coeff_val >= 0 else "-"
135
+ equation_parts.append(f"{sign} {round(abs(coeff_val), 4)}{reqletters[i]}")
136
+
137
+ # Process and add intercept term
138
+ if isinstance(a_raw, (np.ndarray, list)):
139
+ mainvar_val = a_raw[0] if len(a_raw) > 0 else 0.0
140
+ else:
141
+ mainvar_val = a_raw
142
+ mainvar_rounded = round(float(mainvar_val), 4)
143
+
144
+ intercept_sign = "+" if mainvar_rounded >= 0 else "-"
145
+ if not equation_parts: # Only intercept
146
+ last = f"{mainvar_rounded}"
147
+ else:
148
+ equation_parts.append(f"{intercept_sign} {round(abs(mainvar_rounded), 4)}")
149
+ last = " ".join(equation_parts)
150
+ if last.startswith("+ "):
151
+ last = last[2:]
152
+
153
+ return f"f(x): {last}\naccuracy: {round(accuracy * 100, 2)}%"
@@ -1,25 +1,25 @@
1
- Metadata-Version: 2.2
2
- Name: sklearn_fluent
3
- Version: 0.4.1
4
- Summary: Linear/Multli Regression Mathematical Function in one line of code
5
- Author: YusiferZendric (Aditya Singh)
6
- Author-email: <yzendric@gmail.com>
7
- Keywords: python,sklearn,mathematical functions,functions,linear regressions
8
- Classifier: Development Status :: 1 - Planning
9
- Classifier: Intended Audience :: Developers
10
- Classifier: Programming Language :: Python :: 3
11
- Classifier: Operating System :: Unix
12
- Classifier: Operating System :: MacOS :: MacOS X
13
- Classifier: Operating System :: Microsoft :: Windows
14
- Description-Content-Type: text/markdown
15
- Requires-Dist: scikit-learn
16
- Dynamic: author
17
- Dynamic: author-email
18
- Dynamic: classifier
19
- Dynamic: description
20
- Dynamic: description-content-type
21
- Dynamic: keywords
22
- Dynamic: requires-dist
23
- Dynamic: summary
24
-
25
- Just provide x and y list and there you have it the Mathemtical function + accuracy based on the x and y list.
1
+ Metadata-Version: 2.4
2
+ Name: sklearn_fluent
3
+ Version: 0.5.0
4
+ Summary: Linear/Multli Regression Mathematical Function in one line of code
5
+ Author: YusiferZendric (Aditya Singh)
6
+ Author-email: <yzendric@gmail.com>
7
+ Keywords: python,sklearn,mathematical functions,functions,linear regressions
8
+ Classifier: Development Status :: 1 - Planning
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Operating System :: Unix
12
+ Classifier: Operating System :: MacOS :: MacOS X
13
+ Classifier: Operating System :: Microsoft :: Windows
14
+ Description-Content-Type: text/markdown
15
+ Requires-Dist: scikit-learn
16
+ Dynamic: author
17
+ Dynamic: author-email
18
+ Dynamic: classifier
19
+ Dynamic: description
20
+ Dynamic: description-content-type
21
+ Dynamic: keywords
22
+ Dynamic: requires-dist
23
+ Dynamic: summary
24
+
25
+ Just provide x and y list and there you have it the Mathemtical function + accuracy based on the x and y list.
@@ -0,0 +1,6 @@
1
+ sklearn_fluent/__init__.py,sha256=_tBxgz2zPN6jg4RCDM51m2dIKTgxaMQSzrWfBXhuIio,60
2
+ sklearn_fluent/main.py,sha256=v5rLViHh6K2gdUTKv1xtUOrN7jf2wbCjjaEcMXwRG0E,7085
3
+ sklearn_fluent-0.5.0.dist-info/METADATA,sha256=Rk37vXW0ctgrKWRgxWbNhv-fJ3mzZi4O4AM9cfFvLGQ,913
4
+ sklearn_fluent-0.5.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
5
+ sklearn_fluent-0.5.0.dist-info/top_level.txt,sha256=YEMvxTSoqj_0VwfTl7iVj75ickR3Va1qvZJ6ePOmwWE,15
6
+ sklearn_fluent-0.5.0.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.8.0)
2
+ Generator: setuptools (80.9.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,6 +0,0 @@
1
- sklearn_fluent/__init__.py,sha256=_tBxgz2zPN6jg4RCDM51m2dIKTgxaMQSzrWfBXhuIio,60
2
- sklearn_fluent/main.py,sha256=SmTcBV6vG-noI0Hxc3pCtJGWj3eGTUXRSx86Rls9YzY,1633
3
- sklearn_fluent-0.4.1.dist-info/METADATA,sha256=EYju593j_MCYI9l-QF7JfokQ6ozS8Cvr7K0aQtkcAaI,938
4
- sklearn_fluent-0.4.1.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
5
- sklearn_fluent-0.4.1.dist-info/top_level.txt,sha256=YEMvxTSoqj_0VwfTl7iVj75ickR3Va1qvZJ6ePOmwWE,15
6
- sklearn_fluent-0.4.1.dist-info/RECORD,,