Spaces:
Sleeping
Sleeping
adding new module of regression
Browse files- app.py +39 -2
- regression.py +72 -0
- requirements.txt +2 -0
app.py
CHANGED
@@ -25,8 +25,45 @@ import time
|
|
25 |
# st.write("Welcome to the Home Page")
|
26 |
|
27 |
def regressor():
|
28 |
-
st.
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
def NLP():
|
32 |
st.title("Contact Page")
|
|
|
25 |
# st.write("Welcome to the Home Page")
|
26 |
|
27 |
def regressor():
|
28 |
+
train, test = st.tabs(['Train','Test'])
|
29 |
+
|
30 |
+
with train:
|
31 |
+
st.title("Regression/Train data")
|
32 |
+
spectra = st.file_uploader("**Upload file**", type={"csv", "txt"})
|
33 |
+
|
34 |
+
if spectra is not None:
|
35 |
+
spectra_df = pd.read_csv(spectra)
|
36 |
+
|
37 |
+
st.write(spectra_df.head(5))
|
38 |
+
# st.write("Headers", spectra_df.columns.tolist())
|
39 |
+
st.write("**Total Rows**", spectra_df.shape[0])
|
40 |
+
|
41 |
+
st.divider()
|
42 |
+
|
43 |
+
option = st.text_input("**Select Output Column**:")
|
44 |
+
st.divider()
|
45 |
+
|
46 |
+
if option:
|
47 |
+
st.write("**You have selected output column**: ", option)
|
48 |
+
|
49 |
+
y = spectra_df[option]
|
50 |
+
X= spectra_df.drop(option, axis=1)
|
51 |
+
|
52 |
+
# Define the columns with your content
|
53 |
+
col1, col2 = st.columns([4,1], gap="small")
|
54 |
+
|
55 |
+
# Add content to col1
|
56 |
+
with col1:
|
57 |
+
st.write("Train data excluding output")
|
58 |
+
st.write(X.head(5))
|
59 |
+
|
60 |
+
# Add content to col2
|
61 |
+
with col2:
|
62 |
+
st.write("Output")
|
63 |
+
st.write(y.head(5))
|
64 |
+
|
65 |
+
st.divider()
|
66 |
+
|
67 |
|
68 |
def NLP():
|
69 |
st.title("Contact Page")
|
regression.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet, LogisticRegression
|
2 |
+
from sklearn.preprocessing import PolynomialFeatures
|
3 |
+
from sklearn.tree import DecisionTreeRegressor
|
4 |
+
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
|
5 |
+
from sklearn.svm import SVR
|
6 |
+
from xgboost import XGBRegressor
|
7 |
+
from lightgbm import LGBMRegressor
|
8 |
+
from sklearn.metrics import mean_squared_error, r2_score
|
9 |
+
from sklearn.model_selection import train_test_split
|
10 |
+
from sklearn.model_selection import train_test_split
|
11 |
+
from xgboost import XGBRegressor
|
12 |
+
|
13 |
+
from lightgbm import LGBMRegressor
|
14 |
+
|
15 |
+
|
16 |
+
class RegressionModels:
|
17 |
+
def __init__(self):
|
18 |
+
self.data = None
|
19 |
+
self.X_train = None
|
20 |
+
self.X_test = None
|
21 |
+
self.y_train = None
|
22 |
+
self.y_test = None
|
23 |
+
self.models = {
|
24 |
+
'Linear Regression': LinearRegression(),
|
25 |
+
'Polynomial Regression': LinearRegression(),
|
26 |
+
'Ridge Regression': Ridge(),
|
27 |
+
'Lasso Regression': Lasso(),
|
28 |
+
'ElasticNet Regression': ElasticNet(),
|
29 |
+
'Logistic Regression': LogisticRegression(),
|
30 |
+
'Decision Tree Regression': DecisionTreeRegressor(),
|
31 |
+
'Random Forest Regression': RandomForestRegressor(),
|
32 |
+
'Gradient Boosting Regression': GradientBoostingRegressor(),
|
33 |
+
'Support Vector Regression (SVR)': SVR(),
|
34 |
+
'XGBoost': XGBRegressor(),
|
35 |
+
'LightGBM': LGBMRegressor()
|
36 |
+
}
|
37 |
+
|
38 |
+
def add_data(self, X, y):
|
39 |
+
self.data = (X, y)
|
40 |
+
|
41 |
+
def split_data(self, test_size=0.2, random_state=None):
|
42 |
+
if self.data is None:
|
43 |
+
raise ValueError("No data provided. Use add_data method to add data first.")
|
44 |
+
X, y = self.data
|
45 |
+
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(X, y, test_size=test_size, random_state=random_state)
|
46 |
+
|
47 |
+
def fit(self, model_name):
|
48 |
+
if self.X_train is None or self.y_train is None:
|
49 |
+
raise ValueError("Data not split. Use split_data method to split data into training and testing sets.")
|
50 |
+
model = self.models[model_name]
|
51 |
+
model.fit(self.X_train, self.y_train)
|
52 |
+
|
53 |
+
def train(self, model_name):
|
54 |
+
if self.X_train is None or self.y_train is None or self.X_test is None:
|
55 |
+
raise ValueError("Data not split. Use split_data method to split data into training and testing sets.")
|
56 |
+
model = self.models[model_name]
|
57 |
+
model.fit(self.X_train, self.y_train)
|
58 |
+
y_pred = model.predict(self.X_test)
|
59 |
+
return y_pred
|
60 |
+
|
61 |
+
def evaluate(self, model_name):
|
62 |
+
if self.X_test is None or self.y_test is None:
|
63 |
+
raise ValueError("Data not split. Use split_data method to split data into training and testing sets.")
|
64 |
+
model = self.models[model_name]
|
65 |
+
y_pred = model.predict(self.X_test)
|
66 |
+
mse = mean_squared_error(self.y_test, y_pred)
|
67 |
+
r2 = r2_score(self.y_test, y_pred)
|
68 |
+
return mse, r2
|
69 |
+
|
70 |
+
def predict(self, model_name, X):
|
71 |
+
model = self.models[model_name]
|
72 |
+
return model.predict(X)
|
requirements.txt
CHANGED
@@ -1,4 +1,6 @@
|
|
|
|
1 |
matplotlib==3.8.3
|
2 |
pandas==1.5.3
|
|
|
3 |
scikit_learn==1.4.1.post1
|
4 |
streamlit==1.32.0
|
|
|
1 |
+
matplotlib==3.7.0
|
2 |
matplotlib==3.8.3
|
3 |
pandas==1.5.3
|
4 |
+
scikit_learn==1.2.1
|
5 |
scikit_learn==1.4.1.post1
|
6 |
streamlit==1.32.0
|