Predict a house’s sale price
Your listings platform already records completed sales with their final price. Spavik learns from them and estimates a sale price for each new listing from square footage, bedrooms, neighborhood and year built.
- Returns
- An estimated sale price, in dollars
- Language
- Python
In your app
Listing #218
estimated price
Show the estimated price on the property listing.
The data you need
One row per past case. Spavik learns how the inputs relate to the column you want to predict.
Completed San Francisco sales, with square footage, bedrooms, neighborhood, year built and final sale price in dollars.
| Column | Role | Meaning |
|---|---|---|
sqft | Input | Living area in square feet |
bedrooms | Input | Number of bedrooms |
neighborhood | Input | Neighborhood name |
year_built | Input | Year of construction |
price | Predicted | Final sale price in dollars |
The code
A complete, commented script. Set SPAVIK_API_KEY in your server environment, never in browser code.
from spavik import Spavik
# Set SPAVIK_API_KEY in your server environment.
spavik = Spavik()
# sf_sales.csv: sqft, bedrooms, neighborhood,
# year_built, price (final sale price in USD).
history = "sf_sales.csv"
report = spavik.evaluate(history, target="price")
if report["assessment"]["status"] != "better_than_baseline":
raise ValueError(report["assessment"]["message"])
# Create the model from known sale prices.
model = spavik.train(history, target="price")
# Listing #218: provide the same input columns.
# The sale price is unknown, so do not include it.
listing = {
"sqft": 1450,
"bedrooms": 3,
"neighborhood": "Noe Valley",
"year_built": 1925,
}
result = model.predict(listing)
# Show the estimated price on the listing.
price = result.lignes[0]["prediction"]
print(f"Estimated price: ${price:,.0f}")
How it works
- 1
Check your data
evaluate()tells you, for free, whether your history does better than a simple guess. - 2
Learn from your history
train()creates the model once, for free. Keep it and reuse it for every new case. - 3
Ask for predictions
predict()returns a value for each new row, from your server. One credit per row. - 4
Keep it accurate
Report what actually happened with
submit_outcomes(), checkperformance(), thenrefresh().
Questions
How much history do I need?
At least 10 rows, with at least 2 different values in the column you predict. Use completed sales only, with the price actually paid. evaluate() tells you for free whether your history does better than a simple guess before you train anything.
What does it cost?
Checking your data with evaluate() and creating the model with train() are free. Each predicted row costs one credit on the default engine. You train once and keep the model, so you pay only for the predictions you request.