import pandas as pd
import numpy as np
np.random.seed(42)
n_samples = 2000
square_meters = np.random.randint(30, 150, n_samples)
rooms = np.clip(square_meters // 30, 1, 5)
floor = np.random.randint(1, 25, n_samples)
gym = np.random.choice([0, 1], n_samples, p=[0.6, 0.4])
parking = np.random.choice([0, 1], n_samples, p=[0.3, 0.7])
designer_renovation = np.random.choice([0, 1], n_samples, p=[0.4, 0.6])
location_rating = np.random.randint(1, 11, n_samples)
age = np.random.randint(0, 20, n_samples)
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.preprocessing import StandardScaler
from sklearn.feature_selection import f_regression
data = pd.read_csv("house_prices.csv")
scaler = StandardScaler()
numerical_features = ["square_meters", "rooms", "location_rating", "age"]
data[numerical_features] =
scaler.fit_transform(data[numerical_features])
X = data.drop("price", axis=1)
y = data["price"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, y_pred)
error_range = y_test - y_pred
f_values, p_values = f_regression(X_train, y_train)
feature_importances = zip(X.columns, f_values)
feature_importances = sorted(feature_importances, key=lambda
x: x[1], reverse=True)
print(f"Среднеквадратичная ошибка (MSE): {mse:.2f}")
print(f"Корень из среднеквадратичной ошибки (RMSE): {rmse:.2f}")
print(f"Коэффициент детерминации R²: {r2:.6f}")
print(f"Средняя ошибка предсказания: {np.mean(np.abs(error_range)):.2f} рублей")
print("\nВажность признаков:")
for name, importance in feature_importances:
print(f"{name}: {importance:.2f}")
Среднеквадратичная ошибка (MSE): 4815824801.42
Корень из среднеквадратичной ошибки (RMSE): 69396.14
Коэффициент детерминации R²: 0.999767
Средняя ошибка предсказания: 54295.25 рублей
Важность признаков:
square_meters: 1583.86
rooms: 1376.64
location_rating: 755.02
designer_renovation: 192.09
age: 40.96
parking: 37.01
floor: 23.43
gym: 1.68