-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.ipynb
79 lines (79 loc) · 1.88 KB
/
.ipynb
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
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Linear Regression R²: 0.5517193889089222\n",
"Random Forest R²: 0.5343406907435422\n",
"Best model saved as 'best_model.pkl'\n"
]
}
],
"source": [
"import pandas as pd\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.ensemble import RandomForestRegressor\n",
"import pickle\n",
"\n",
"\n",
"df = pd.read_csv(r'/teamspace/studios/this_studio/house_data.csv')\n",
"\n",
"\n",
"columns = ['bedrooms', 'bathrooms', 'floors', 'yr_built', 'sqft_living', 'price']\n",
"df = df[columns]\n",
"\n",
"\n",
"X = df.iloc[:, 0:5]\n",
"\n",
"y = df.iloc[:, 5]\n",
"\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)\n",
"\n",
"\n",
"lr = LinearRegression()\n",
"rf = RandomForestRegressor(random_state=42)\n",
"\n",
"\n",
"lr.fit(X_train, y_train)\n",
"rf.fit(X_train, y_train)\n",
"\n",
"\n",
"lr_score = lr.score(X_test, y_test)\n",
"rf_score = rf.score(X_test, y_test)\n",
"\n",
"print(\"Linear Regression R²:\", lr_score)\n",
"print(\"Random Forest R²:\", rf_score)\n",
"\n",
"best_model = lr if lr_score >= rf_score else rf\n",
"\n",
"\n",
"with open('best_model.pkl', 'wb') as file:\n",
" pickle.dump(best_model, file)\n",
"\n",
"print(\"Best model saved as 'best_model.pkl'\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}