Spaces:
Sleeping
Sleeping
Rename MLProject.py to app.py
Browse files- MLProject.py → app.py +93 -93
MLProject.py → app.py
RENAMED
@@ -1,93 +1,93 @@
|
|
1 |
-
import numpy as np
|
2 |
-
import streamlit as st
|
3 |
-
import pandas as pd
|
4 |
-
from sklearn.model_selection import train_test_split
|
5 |
-
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
6 |
-
from sklearn.model_selection import train_test_split
|
7 |
-
from sklearn.tree import DecisionTreeClassifier
|
8 |
-
from sklearn.neighbors import KNeighborsRegressor
|
9 |
-
from sklearn.metrics import mean_squared_error
|
10 |
-
|
11 |
-
|
12 |
-
st.title(":red[Welcome to My ML Project]")
|
13 |
-
df=pd.read_csv(
|
14 |
-
|
15 |
-
y=df.pop("total_bill")
|
16 |
-
x=df
|
17 |
-
|
18 |
-
X_train, X_test, y_train, y_test=train_test_split(x,y,test_size=0.15,random_state=30)
|
19 |
-
|
20 |
-
numerical_data=X_train.select_dtypes("number")
|
21 |
-
cat_data=X_train.select_dtypes("object")
|
22 |
-
|
23 |
-
|
24 |
-
encoder=OneHotEncoder(sparse=False)
|
25 |
-
X_train_cat=pd.DataFrame(encoder.fit_transform(cat_data), columns=encoder.get_feature_names_out())
|
26 |
-
scaler=StandardScaler()
|
27 |
-
res=scaler.fit_transform(numerical_data)
|
28 |
-
X_train_num=pd.DataFrame(res,columns=numerical_data.columns)
|
29 |
-
Final_X_train_data=pd.concat([X_train_cat,X_train_num],axis=1)
|
30 |
-
|
31 |
-
X_test_num=X_test.select_dtypes("number")
|
32 |
-
X_test_cat=X_test.select_dtypes("object")
|
33 |
-
|
34 |
-
X_test_num_trans=scaler.transform(X_test_num)
|
35 |
-
res1=pd.DataFrame(X_test_num_trans, columns=X_test_num.columns)
|
36 |
-
|
37 |
-
|
38 |
-
X_test_cat_trans=encoder.transform(X_test_cat)
|
39 |
-
res2=pd.DataFrame(X_test_cat_trans, columns=encoder.get_feature_names_out())
|
40 |
-
|
41 |
-
Final_X_test=pd.concat([res2,res1],axis=1)
|
42 |
-
|
43 |
-
regression=KNeighborsRegressor()
|
44 |
-
regression.fit(Final_X_train_data,y_train)
|
45 |
-
y_pred=regression.predict(Final_X_test)
|
46 |
-
|
47 |
-
mean_squared_error(y_test,y_pred)
|
48 |
-
|
49 |
-
#Application
|
50 |
-
tip = st.number_input("Enter Customer Tip")
|
51 |
-
|
52 |
-
sex =["Female","Male"]
|
53 |
-
select_sex=st.selectbox("Select Customer Gender",sex)
|
54 |
-
|
55 |
-
smoker=["No","Yes"]
|
56 |
-
select_smoker=st.selectbox("Select Customer Smoker or not",smoker)
|
57 |
-
|
58 |
-
day=["Sun","Sat","Fri"]
|
59 |
-
select_day=st.selectbox("Select Day",day)
|
60 |
-
|
61 |
-
|
62 |
-
time_options = ["Dinner", "Lunch"]
|
63 |
-
select_time = st.selectbox("Select Time", time_options)
|
64 |
-
size=st.number_input("Enter Size")
|
65 |
-
|
66 |
-
if st.button("Predict total bill"):
|
67 |
-
query_point=pd.DataFrame([
|
68 |
-
{
|
69 |
-
"tip":tip,
|
70 |
-
"sex":select_sex,
|
71 |
-
"smoker":select_smoker,
|
72 |
-
"day":select_day,
|
73 |
-
"time":select_time,
|
74 |
-
"size":size
|
75 |
-
|
76 |
-
}]
|
77 |
-
)
|
78 |
-
cat_query_point=query_point.select_dtypes("object")
|
79 |
-
|
80 |
-
num_query_point=query_point.select_dtypes("number")
|
81 |
-
|
82 |
-
cat_query_point_trans = pd.DataFrame(encoder.transform(cat_query_point),columns=encoder.get_feature_names_out())
|
83 |
-
num_query_point_trans=pd.DataFrame(scaler.transform(num_query_point),columns=X_test_num.columns)
|
84 |
-
|
85 |
-
final_query_point=pd.concat([cat_query_point_trans, num_query_point_trans], axis=1)
|
86 |
-
|
87 |
-
def fun(query_point):
|
88 |
-
res=regression.predict(query_point)[0]
|
89 |
-
return res
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
st.write(fun(final_query_point))
|
|
|
1 |
+
import numpy as np
|
2 |
+
import streamlit as st
|
3 |
+
import pandas as pd
|
4 |
+
from sklearn.model_selection import train_test_split
|
5 |
+
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
6 |
+
from sklearn.model_selection import train_test_split
|
7 |
+
from sklearn.tree import DecisionTreeClassifier
|
8 |
+
from sklearn.neighbors import KNeighborsRegressor
|
9 |
+
from sklearn.metrics import mean_squared_error
|
10 |
+
|
11 |
+
|
12 |
+
st.title(":red[Welcome to My ML Project]")
|
13 |
+
df=pd.read_csv("tips.csv")
|
14 |
+
|
15 |
+
y=df.pop("total_bill")
|
16 |
+
x=df
|
17 |
+
|
18 |
+
X_train, X_test, y_train, y_test=train_test_split(x,y,test_size=0.15,random_state=30)
|
19 |
+
|
20 |
+
numerical_data=X_train.select_dtypes("number")
|
21 |
+
cat_data=X_train.select_dtypes("object")
|
22 |
+
|
23 |
+
|
24 |
+
encoder=OneHotEncoder(sparse=False)
|
25 |
+
X_train_cat=pd.DataFrame(encoder.fit_transform(cat_data), columns=encoder.get_feature_names_out())
|
26 |
+
scaler=StandardScaler()
|
27 |
+
res=scaler.fit_transform(numerical_data)
|
28 |
+
X_train_num=pd.DataFrame(res,columns=numerical_data.columns)
|
29 |
+
Final_X_train_data=pd.concat([X_train_cat,X_train_num],axis=1)
|
30 |
+
|
31 |
+
X_test_num=X_test.select_dtypes("number")
|
32 |
+
X_test_cat=X_test.select_dtypes("object")
|
33 |
+
|
34 |
+
X_test_num_trans=scaler.transform(X_test_num)
|
35 |
+
res1=pd.DataFrame(X_test_num_trans, columns=X_test_num.columns)
|
36 |
+
|
37 |
+
|
38 |
+
X_test_cat_trans=encoder.transform(X_test_cat)
|
39 |
+
res2=pd.DataFrame(X_test_cat_trans, columns=encoder.get_feature_names_out())
|
40 |
+
|
41 |
+
Final_X_test=pd.concat([res2,res1],axis=1)
|
42 |
+
|
43 |
+
regression=KNeighborsRegressor()
|
44 |
+
regression.fit(Final_X_train_data,y_train)
|
45 |
+
y_pred=regression.predict(Final_X_test)
|
46 |
+
|
47 |
+
mean_squared_error(y_test,y_pred)
|
48 |
+
|
49 |
+
#Application
|
50 |
+
tip = st.number_input("Enter Customer Tip")
|
51 |
+
|
52 |
+
sex =["Female","Male"]
|
53 |
+
select_sex=st.selectbox("Select Customer Gender",sex)
|
54 |
+
|
55 |
+
smoker=["No","Yes"]
|
56 |
+
select_smoker=st.selectbox("Select Customer Smoker or not",smoker)
|
57 |
+
|
58 |
+
day=["Sun","Sat","Fri"]
|
59 |
+
select_day=st.selectbox("Select Day",day)
|
60 |
+
|
61 |
+
|
62 |
+
time_options = ["Dinner", "Lunch"]
|
63 |
+
select_time = st.selectbox("Select Time", time_options)
|
64 |
+
size=st.number_input("Enter Size")
|
65 |
+
|
66 |
+
if st.button("Predict total bill"):
|
67 |
+
query_point=pd.DataFrame([
|
68 |
+
{
|
69 |
+
"tip":tip,
|
70 |
+
"sex":select_sex,
|
71 |
+
"smoker":select_smoker,
|
72 |
+
"day":select_day,
|
73 |
+
"time":select_time,
|
74 |
+
"size":size
|
75 |
+
|
76 |
+
}]
|
77 |
+
)
|
78 |
+
cat_query_point=query_point.select_dtypes("object")
|
79 |
+
|
80 |
+
num_query_point=query_point.select_dtypes("number")
|
81 |
+
|
82 |
+
cat_query_point_trans = pd.DataFrame(encoder.transform(cat_query_point),columns=encoder.get_feature_names_out())
|
83 |
+
num_query_point_trans=pd.DataFrame(scaler.transform(num_query_point),columns=X_test_num.columns)
|
84 |
+
|
85 |
+
final_query_point=pd.concat([cat_query_point_trans, num_query_point_trans], axis=1)
|
86 |
+
|
87 |
+
def fun(query_point):
|
88 |
+
res=regression.predict(query_point)[0]
|
89 |
+
return res
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
st.write(fun(final_query_point))
|