Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,97 +1,62 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
from sklearn.model_selection import train_test_split
|
|
|
|
| 4 |
from sklearn.linear_model import LinearRegression
|
| 5 |
-
from sklearn.metrics import
|
| 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 |
-
st.
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
if not data.empty:
|
| 51 |
-
X = data.drop(columns=[target])
|
| 52 |
-
y = data[target]
|
| 53 |
-
|
| 54 |
-
# Kategorik sütunları işle
|
| 55 |
-
categorical_features = [col_name for col_name, col_type in features_info if col_type == "Kategorik"]
|
| 56 |
-
column_transformer = ColumnTransformer(
|
| 57 |
-
transformers=[
|
| 58 |
-
('cat', OneHotEncoder(handle_unknown='ignore'), categorical_features)
|
| 59 |
-
],
|
| 60 |
-
remainder='passthrough'
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
X = column_transformer.fit_transform(X)
|
| 64 |
-
|
| 65 |
-
# Veri bölme (eğitim için daha fazla veri gerektiğinde burayı düzenleyebilirsiniz)
|
| 66 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 67 |
-
|
| 68 |
-
# Model eğitimi
|
| 69 |
model = LinearRegression()
|
| 70 |
model.fit(X_train, y_train)
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
if len(X_test) > 0:
|
| 74 |
-
y_pred = model.predict(X_test)
|
| 75 |
-
mse = mean_squared_error(y_test, y_pred)
|
| 76 |
-
st.write(f"Ortalama Kare Hata (MSE): {mse:.2f}")
|
| 77 |
-
else:
|
| 78 |
-
st.write("Model değerlendirmesi için yeterli veri yok.")
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
user_input = []
|
| 83 |
-
for col_name, col_type in features_info:
|
| 84 |
-
if col_type == "Sayısal":
|
| 85 |
-
value = st.number_input(f"Tahmin için {col_name} değeri girin", value=0.0, key=f"pred_{col_name}")
|
| 86 |
-
else:
|
| 87 |
-
options = [option.strip() for option in st.session_state[f"opt_{col_name}"].split(',')]
|
| 88 |
-
value = st.selectbox(f"Tahmin için {col_name} seçin", options, key=f"pred_{col_name}")
|
| 89 |
-
user_input.append(value)
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
user_input_processed = column_transformer.transform(user_input_df)
|
| 94 |
-
prediction = model.predict(user_input_processed)
|
| 95 |
-
st.write(f"Ev fiyatı tahmini: {prediction[0]:.2f}")
|
| 96 |
-
else:
|
| 97 |
-
st.write("Lütfen önce veri girişi yapın.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
from sklearn.model_selection import train_test_split
|
| 5 |
+
from sklearn.preprocessing import StandardScaler
|
| 6 |
from sklearn.linear_model import LinearRegression
|
| 7 |
+
from sklearn.metrics import r2_score
|
| 8 |
+
|
| 9 |
+
# Veri Yükleme ve Ön İşleme (Kaggle Notebook'tan uyarlanmıştır)
|
| 10 |
+
@st.cache_data
|
| 11 |
+
def load_and_preprocess_data():
|
| 12 |
+
data = pd.read_csv('Housing.csv')
|
| 13 |
+
|
| 14 |
+
# Gereksiz sütunu sil
|
| 15 |
+
data.drop('date', axis=1, inplace=True)
|
| 16 |
+
|
| 17 |
+
# Aykırı değerleri işle
|
| 18 |
+
data = data[data['bedrooms'] != 33]
|
| 19 |
+
|
| 20 |
+
# Saçma değerleri düzelt
|
| 21 |
+
data.loc[data['bathrooms'] == 0, 'bathrooms'] = 1
|
| 22 |
+
data.loc[data['bedrooms'] == 0, 'bedrooms'] = 1
|
| 23 |
+
|
| 24 |
+
# Kategorik sütunlar için binary encoding
|
| 25 |
+
binary_columns = ['waterfront', 'view', 'condition']
|
| 26 |
+
def binary_encode(df, column, positive_value):
|
| 27 |
+
df[column] = df[column].apply(lambda x: 1 if x == positive_value else 0)
|
| 28 |
+
for col in binary_columns:
|
| 29 |
+
binary_encode(data, col, data[col].max())
|
| 30 |
+
|
| 31 |
+
# Log dönüşümü
|
| 32 |
+
data['sqft_living'] = np.log(data['sqft_living'])
|
| 33 |
+
data['sqft_lot'] = np.log(data['sqft_lot'])
|
| 34 |
+
data['sqft_above'] = np.log(data['sqft_above'])
|
| 35 |
+
data.loc[data['sqft_basement'] != 0, 'sqft_basement'] = np.log(data.loc[data['sqft_basement'] != 0, 'sqft_basement'])
|
| 36 |
+
|
| 37 |
+
# Normalleştirme
|
| 38 |
+
scaler = StandardScaler()
|
| 39 |
+
numerical_cols = ['bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'sqft_above', 'sqft_basement']
|
| 40 |
+
data[numerical_cols] = scaler.fit_transform(data[numerical_cols])
|
| 41 |
+
|
| 42 |
+
return data
|
| 43 |
+
|
| 44 |
+
data = load_and_preprocess_data()
|
| 45 |
+
|
| 46 |
+
# Model Eğitimi
|
| 47 |
+
@st.cache_data
|
| 48 |
+
def train_model(data):
|
| 49 |
+
X = data.drop('price', axis=1)
|
| 50 |
+
y = data['price']
|
| 51 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=7)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
model = LinearRegression()
|
| 53 |
model.fit(X_train, y_train)
|
| 54 |
+
return model, X_test, y_test
|
| 55 |
|
| 56 |
+
model, X_test, y_test = train_model(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# Streamlit Arayüzü
|
| 59 |
+
st.title("Ev Fiyatı Tahmin Uygulaması")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# Kenar Çubuğu Filtreleri
|
| 62 |
+
st.sidebar.header("Filtreler")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|