-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregression.py
148 lines (121 loc) · 6.18 KB
/
regression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import sklearn
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
from sklearn.linear_model import LogisticRegression
from sklearn import linear_model
st.set_option('deprecation.showPyplotGlobalUse', False)
st.title("Regression Dashboard")
file_type = st.radio("Select the type of file:",
["CSV", "XLS"])
file_uploaded = 0
if file_type == "CSV":
st.write("***Upload your dataset in csv format:***")
file = st.file_uploader("Choose a CSV file", type="csv")
elif file_type == "XLS":
st.write("Upload your dataset in XLS format:")
file = st.file_uploader("Choose a XLS file", type="xls")
if (file is not None) and (file_type == "CSV"):
data = pd.read_csv(file)
st.dataframe(data.head())
file_uploaded = 1
elif (file is not None) and (file_type == "XLS"):
data = pd.read_excel(file)
st.dataframe(data.head())
file_uploaded = 1
if file_uploaded == 1:
reg_type = st.selectbox("Regression type: ",
['Linear', 'Logistic', 'Multivariate'])
st.write("You selected:", reg_type)
if (reg_type == "Linear"):
st.write("Select the column to be used as the independent variable (X):")
independent_var = st.selectbox("", data.columns, key='unique_key_1')
st.write("Select the column to be used as the dependent variable (Y):")
dependent_var = st.selectbox("", data.columns, key='unique_key_2')
x = data[[independent_var]]
y = data[dependent_var]
st.write("Split the dataset into training and testing sets:")
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
st.write("Fit the linear regression model:")
model = LinearRegression()
model.fit(x_train, y_train)
st.write("Predict the values for the test set:")
y_pred = model.predict(x_test)
st.write("Evaluate the model using mean absolute error and mean squared error:")
mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
st.write("Mean Absolute Error:", mae)
st.write("Mean Squared Error:", mse)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, y_pred)
st.write("Root Mean Squared Error:", rmse)
st.write("R^2 score:", r2)
m = model.coef_[0][0]
c = model.intercept_[0]
st.write("Slope (m):", m)
st.write("Y-intercept (c):", c)
st.write("Plot the regression line:")
plt.scatter(x_test, y_test, color='gray')
plt.plot(x_test, y_pred, color='red', linewidth=2)
plt.title("Linear Regression")
plt.xlabel(independent_var)
plt.ylabel(dependent_var)
st.pyplot()
elif (reg_type == "Multivariate"):
independent_vars = st.multiselect("Select independent variables", data.columns.tolist(), default=[data.columns[1]])
X = data[independent_vars]
dependent_var = st.selectbox("Select dependent variable", data.columns.tolist(),index=data.columns.tolist().index(data.columns[-1]))
y = data[dependent_var]
reg = linear_model.LinearRegression()
reg.fit(X, y)
y_pred = reg.predict(X)
mse = mean_squared_error(y, y_pred)
rmse = np.sqrt(mse)
mae = mean_absolute_error(y, y_pred)
r2 = r2_score(y, y_pred)
st.write("Mean Absolute Error:", mae)
st.write("Mean Squared Error:", mse)
st.write("R2 score:", r2)
st.write("Root Mean Square Error:", rmse)
bed = st.number_input("Total bedrooms: ", value=0, step=1)
bath = st.number_input("Total bathrooms: ", value=0.0, step=0.1)
sqliv = st.number_input("Sqft living: ", value=0, step=1)
sqlot = st.number_input("Sqft lot: ", value=0, step=1)
floors = st.number_input("Total floors: ", value=0.0, step=0.1)
view = st.number_input("View: ", value=0, step=1)
waterfront = st.number_input("Waterfront: ", value=0.0, step=0.1)
condition = st.number_input("Condition: ", value=0, step=1)
sqftAbv = st.number_input("Sqft above: ", value=0, step=1)
sqftBase = st.number_input("Sqft basement: ", value=0, step=1)
yrBuilt = st.number_input("Year Built: ", value=0, step=1)
yrReno = st.number_input("Year renovated: ", value=0, step=1)
instance = pd.DataFrame([[bed, bath, sqliv, sqlot, floors, waterfront, view, condition, sqftAbv, sqftBase, yrBuilt, yrReno]], columns=["bedrooms", "bathrooms", "sqft_living", "sqft_lot", "floors", "waterfront", "view", 'condition', 'sqft_above', 'sqft_basement', 'yr_built', 'yr_renovated'])
predict = reg.predict(instance)
st.write("The house price is: ", predict[0])
elif (reg_type == "Logistic"):
data = data.drop(columns = ['Id'])
le = LabelEncoder()
data['Species'] = le.fit_transform(data['Species'])
X = data.drop(columns = ['Species'])
Y = data['Species']
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.25)
model = LogisticRegression()
model.fit(X_train, Y_train)
st.write("Accuracy:", model.score(X_test, Y_test) * 100)
sepLen = st.number_input("Enter the sepal length:", value=0.0, step=0.1)
sepWidth = st.number_input("Enter the sepal width:", value=0.0, step=0.1)
petLen = st.number_input("Enter the petal length:", value=0.0, step=0.1)
petWidth = st.number_input("Enter the petal width:", value=0.0, step=0.1)
instance = pd.DataFrame([[sepLen, sepWidth, petLen, petWidth]], columns=["SepalLengthCm", "SepalWidthCm", "PetalLengthCm", "PetalWidthCm"])
prediction = model.predict(instance)
if prediction[0] == 0:
st.write("The species is: Iris-setosa")
elif prediction[0] == 1:
st.write("The species is: Iris-versicolor")
else:
st.write("The species is: Iris-virginica")