Files
pytorch-playground/RegressionModels/BostonHousing/predict_housing.py
2025-09-29 08:59:47 +02:00

47 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Column Meaning
crim Per capita crime rate by town
zn Proportion of residential land zoned for lots over 25,000 sq.ft.
indus Proportion of non-retail business acres per town
chas Charles River dummy variable (1 if tract bounds river, 0 otherwise)
nox Nitric oxides concentration (parts per 10 million)
rm Average number of rooms per dwelling
age Proportion of owner-occupied units built prior to 1940
dis Weighted distances to five Boston employment centers
rad Index of accessibility to radial highways
tax Full-value property-tax rate per $10,000
ptratio Pupil-teacher ratio by town
black 1000(Bk - 0.63)^2, where Bk is the proportion of Black people by town
lstat % lower status of the population
medv Median value of owner-occupied homes in $1000s (target variable)
"""
import pandas as pd
import torch
import torch.nn as nn
df = pd.read_csv("./RegressionModels/BostonHousing/Boston.csv")
# print(df.iloc[:,0:14])
X = torch.tensor(df.iloc[:,0:14].values, dtype=torch.float32)
Y = torch.tensor(df["medv"].values, dtype=torch.float32)
model = torch.nn.Sequential(
torch.nn.Linear(14, 1)
)
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=5e-9)
for epoch in range(2000):
predict_y = model(X)
loss = loss_fn(predict_y, Y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if epoch % 99 ==0:
print(f'Epoch: {epoch}, Loss: {loss.item():.2f}')