-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
65 lines (52 loc) · 2.28 KB
/
app.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
import streamlit as st
import joblib
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.metrics.pairwise import rbf_kernel
def ratio_name(function_transformer, feature_names_in):
return ["ratio"]
def column_ratio(X):
return X[:, [0]] / X[:, [1]]
class ClusterSimilarity(BaseEstimator, TransformerMixin):
def __init__(self, n_clusters=10, gamma=1.0, random_state=None):
self.n_clusters = n_clusters
self.gamma = gamma
self.random_state = random_state
def fit(self, X, y=None, sample_weight=None):
self.kmeans_ = KMeans(self.n_clusters, n_init=10,
random_state=self.random_state)
self.kmeans_.fit(X, sample_weight=sample_weight)
return self
def transform(self, X):
return rbf_kernel(X, self.kmeans_.cluster_centers_, gamma=self.gamma)
def get_feature_names_out(self, names=None):
return [f"Cluster {i} similarity" for i in range(self.n_clusters)]
model = joblib.load("./model/my_california_housing_model.pkl")
st.title("California Housing Prediction")
st.write("This app predicts housing prices in California based on various features.")
longitude = st.number_input("Longitude")
latitude = st.number_input("Latitude")
housing_median_age = st.number_input("Housing Median Age")
total_rooms = st.number_input("Total Rooms")
total_bedrooms = st.number_input("Total Bedrooms")
population = st.number_input("Population")
households = st.number_input("Households")
median_income = st.number_input("Median Income")
ocean_proximity = st.selectbox("Ocean Proximity",
['<1H OCEAN', 'INLAND', 'NEAR OCEAN', 'NEAR BAY', 'ISLAND'])
if st.button("Predict"):
input_data = pd.DataFrame({
'longitude': [longitude],
'latitude': [latitude],
'housing_median_age': [housing_median_age],
'total_rooms': [total_rooms],
'total_bedrooms': [total_bedrooms],
'population': [population],
'households': [households],
'median_income': [median_income],
'ocean_proximity': [ocean_proximity]
})
input = pd.DataFrame(input_data)
prediction = model.predict(input)
st.write(f"The predicted house value is: ${prediction[0]:,.2f}")