YoneSlapWind80085 commited on
Commit
eaf0a9d
·
verified ·
1 Parent(s): 0340664

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import pandas as pd
4
+ from sklearn.linear_model import LinearRegression
5
+ from sklearn.model_selection import train_test_split
6
+ from sklearn.datasets import fetch_california_housing
7
+ import pickle
8
+
9
+ # Load the data
10
+ california = fetch_california_housing()
11
+ df = pd.DataFrame(california.data, columns=california.feature_names)
12
+ df['MedHouseVal'] = california.target
13
+
14
+ # Prepare the data for the model
15
+ X = df[['MedInc']]
16
+ y = df['MedHouseVal']
17
+
18
+ # Split the data into training and testing sets
19
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
20
+
21
+ # Train the model
22
+ model = LinearRegression()
23
+ model.fit(X_train, y_train)
24
+
25
+ # Save the model
26
+ with open("linear_regression_model.pkl", "wb") as file:
27
+ pickle.dump(model, file)
28
+
29
+ # Load the model
30
+ with open("linear_regression_model.pkl", "rb") as file:
31
+ model = pickle.load(file)
32
+
33
+ # Streamlit app
34
+ st.title('California Housing Price Prediction')
35
+
36
+ med_inc = st.number_input('Enter Median Income:', min_value=0.0, step=0.01)
37
+
38
+ if st.button('Predict'):
39
+ X_new = np.array([[med_inc]])
40
+ prediction = model.predict(X_new)
41
+ st.write(f'Predicted Median House Value: {prediction[0]}')
42
+
43
+ # Display data
44
+ if st.checkbox('Show raw data'):
45
+ st.write(df)