Spaces:
Sleeping
Sleeping
File size: 6,727 Bytes
eaf0a9d 5f6c2e9 eaf0a9d a14426e eaf0a9d a14426e eaf0a9d a14426e eaf0a9d a14426e eaf0a9d a14426e eaf0a9d a14426e eaf0a9d a14426e eaf0a9d a14426e eaf0a9d |
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
import streamlit as st
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_california_housing
import pickle
# Load the data
california = fetch_california_housing()
df = pd.DataFrame(california.data, columns=california.feature_names)
df['MedHouseVal'] = california.target
# Prepare the data for the model
X = df[['MedInc']]
y = df['MedHouseVal']
# Pairplot to visualize relationships between features and the target
plt.show()
plt.figure(figsize=(10, 8))
plt.show()
# Scatter plot for specific features against the target variable
features = ['MedInc', 'AveRooms', 'AveOccup', 'HouseAge']
for feature in features:
plt.figure(figsize=(6, 4))
plt.scatter(df[feature], df['MedHouseVal'], alpha=0.3)
plt.title(f'MedHouseVal vs {feature}')
plt.xlabel(feature)
plt.ylabel('MedHouseVal')
plt.show()
#5
# Select the predictor and target variable
X = df[['MedInc']]
y = df['MedHouseVal']
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
print("Training and testing data split done.")
#6 7 and 8
#lineare regression model
model = LinearRegression()
# Fitting the model on the training data
model.fit(X_train, y_train)
# Making predictions on the test data
y_pred = model.predict(X_test)
# Evaluating the model
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"Mean Squared Error: {mse}")
print(f"R-squared: {r2}")
# Plot the regression line
plt.figure(figsize=(8, 6))
plt.scatter(X_test, y_test, color='blue', alpha=0.3, label='Actual')
plt.plot(X_test, y_pred, color='red', linewidth=2, label='Predicted')
plt.title('Simple Linear Regression: MedInc vs MedHouseVal')
plt.xlabel('MedInc')
plt.ylabel('MedHouseVal')
plt.legend()
plt.show()
#Split the data into training (80%) and testing (20%) sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Print the sizes of the training and testing sets
print(f"Training set size: {X_train.shape[0]} samples")
print(f"Testing set size: {X_test.shape[0]} samples")
# Create the linear regression model
model = LinearRegression()
# Fit the model on the training data
model.fit(X_train, y_train)
# Print the coefficients
print(f"Coefficients: {model.coef_}")
print(f"Intercept: {model.intercept_}")
# Make predictions on the test data
y_pred = model.predict(X_test)
# Calculate RMSE and R-squared
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, y_pred)
print(f"Root Mean Squared Error (RMSE): {rmse}")
print(f"R-squared: {r2}")
# Scatter plot of actual vs. predicted values
plt.figure(figsize=(8, 6))
plt.scatter(y_test, y_pred, color='blue', alpha=0.3)
plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'k--', lw=2, color='green')
plt.title('Multilinear Regression: Actual vs. Predicted MedHouseVal')
plt.xlabel('Actual MedHouseVal')
plt.ylabel('Predicted MedHouseVal')
plt.show()
#comparing the performance between RMSE and R-squared values
# Simple Linear Regression
# Select a single predictor
X_single = df[['MedInc']]
y = df['MedHouseVal']
# Split the data into training and testing sets
X_train_single, X_test_single, y_train_single, y_test_single = train_test_split(X_single, y, test_size=0.2, random_state=42)
# Create the linear regression model
model_single = LinearRegression()
# Fit the model on the training data
model_single.fit(X_train_single, y_train_single)
# Make predictions on the test data
y_pred_single = model_single.predict(X_test_single)
# Evaluate the model
mse_single = mean_squared_error(y_test_single, y_pred_single)
rmse_single = np.sqrt(mse_single)
r2_single = r2_score(y_test_single, y_pred_single)
print(f"Simple Linear Regression - RMSE: {rmse_single}")
print(f"Simple Linear Regression - R-squared: {r2_single}")
# Multilinear Regression
# Select multiple predictors
X_multi = df[['MedInc', 'AveRooms', 'HouseAge', 'AveOccup']]
y = df['MedHouseVal']
# Split the data into training and testing sets
X_train_multi, X_test_multi, y_train_multi, y_test_multi = train_test_split(X_multi, y, test_size=0.2, random_state=42)
# Create the linear regression model
model_multi = LinearRegression()
# Fit the model on the training data
model_multi.fit(X_train_multi, y_train_multi)
# Make predictions on the test data
y_pred_multi = model_multi.predict(X_test_multi)
# Evaluate the model
mse_multi = mean_squared_error(y_test_multi, y_pred_multi)
rmse_multi = np.sqrt(mse_multi)
r2_multi = r2_score(y_test_multi, y_pred_multi)
print(f"Multilinear Regression - RMSE: {rmse_multi}")
print(f"Multilinear Regression - R-squared: {r2_multi}")
#Residual Plot for Multilinear Regression
residuals = y_test_multi - y_pred_multi
plt.figure(figsize=(8, 6))
plt.scatter(y_pred_multi, residuals, color='blue', alpha=0.3)
plt.hlines(y=0, xmin=y_pred_multi.min(), xmax=y_pred_multi.max(), colors='red', linestyles='--', lw=2)
plt.title('Residual Plot: Multilinear Regression')
plt.xlabel('Predicted MedHouseVal')
plt.ylabel('Residuals')
plt.show()
# Save the model
with open("linear_regression_model.pkl", "wb") as file:
pickle.dump(model, file)
# Load the model
with open("linear_regression_model.pkl", "rb") as file:
model = pickle.load(file)
# Sidebar for user input features
st.sidebar.header('User Input Features')
selected_feature = st.sidebar.selectbox('Select feature for visualization', df.columns)
selected_target = st.sidebar.selectbox('Select target variable', df.columns)
# Display the raw data if checkbox is selected
if st.checkbox('Show raw data'):
st.write(df)
# Visualization of selected feature
st.subheader(f'Distribution of {selected_feature}')
plt.figure(figsize=(10, 6))
plt.hist(df[selected_feature], bins=30, edgecolor='black')
st.pyplot(plt)
# Scatter plot of selected feature vs target
st.subheader(f'Scatter plot of {selected_feature} vs {selected_target}')
plt.figure(figsize=(10, 6))
plt.scatter(df[selected_feature], df[selected_target], alpha=0.3)
plt.xlabel(selected_feature)
plt.ylabel(selected_target)
st.pyplot(plt)
# Prediction
st.subheader('Predict Median House Value')
# Input values for prediction
input_values = {}
for feature in X.columns:
input_values[feature] = st.number_input(f'Enter {feature}', value=float(df[feature].mean()))
if st.button('Predict'):
input_data = np.array([list(input_values.values())])
prediction = model.predict(input_data)
st.write(f'Predicted Median House Value: {prediction[0]}')
# Display data
if st.checkbox('Show raw data'):
st.write(df)
|