This is a Flask-based API that predicts assessedvalues for single family homes in Cambridge, MA based on several factors like interior_bedrooms, interior_fullbaths, interior_halfbaths, and overall condition. The API has two main endpoints:
/reload: Reloads the data and trains the model./predict: Predicts the rental assessedvalue for a given listing.
The data used for this project comes from the Cambridge Propertry Database FY2016-FY2025, which provides detailed information about assessed values for every property in the Ciity of Cambridge, MA. For this particular app, the data for only single family properties is used.
The dataset includes important features such as:
- Assessed Value: The assessed value of the property.
- Bedrooms: The number of interior_bedrooms in the listing.
- Full Bathrooms: The number of full baths in the property.
- Half Bathrooms: The number of half bathrooms in the property.
- Overall Property Condition: The overall quality rating of the property.
The full dataset can be accessed and downloaded from the City of Cambrigde Open Data Portal website at Cambridge Open Data Portal.
The application makes use of a simple Linear Regression Model to predict the assessed value of a single family home in Cambridge, MA based on various input features such as the number of interior_bedrooms, interior_fullbaths, interior_halfbaths, and the overall_condition.
The process of prediction is as follows:
- Data Preprocessing: The data is cleaned and processed. Non-numeric values are removed or converted, and categorical variables like
condition_overallconditionare one-hot encoded to make them suitable for machine learning models. - Model Training: A linear regression model is trained on the cleaned dataset using features like interior_bedrooms, interior_fullbaths, interior_halfbaths, and one-hot encoded condition_overallcondition values.
- Prediction: Once trained, the model can predict the assessed value based on user input, such as the number of interior_bedrooms, interior_fullbaths, interior_halfbaths, and condition_overallcondition.
By using this model, the app can provide quick assessed value predictions for single family home assessments in Cambrigde, MA based on historical data.
Before you can set up and run this app, ensure you have the following software installed:
- Python 3.9+
- pip (Python package installer)
- Virtualenv (Optional but recommended)
First, clone this repository to your local machine:
git clone https://github.com/tjhoranumass/airbnb.git
cd airbnbYou can create a virtual environment to isolate the project dependencies.
For macOS:
python3 -m venv venv
source venv/bin/activateFor Windows:
python -m venv venv
venv\Scripts\activateInstall the required Python dependencies using pip:
pip install -r requirements.txtFlask requires some environment variables to run the app correctly. Create a .env file in the project root with the following content:
FLASK_APP=app.py
FLASK_ENV=developmentFor macOS, you can set the environment variables using the following commands:
export FLASK_APP=app.py
export FLASK_ENV=developmentFor Windows, you can set the environment variables using the following commands in PowerShell:
$env:FLASK_APP = "app.py"
$env:FLASK_ENV = "development"To set up the SQLite database for the first time, run:
flask shellInside the shell, run:
from app import db
db.create_all()
exit()Once everything is set up, you can run the application with the following command:
flask runBy default, the app will run on http://127.0.0.1:5000.
You can access the Swagger documentation for the API at:
http://127.0.0.1:5000/apidocs/
To reload the data and train the model, use the /reload endpoint:
curl -X POST http://127.0.0.1:5000/reloadTo predict a rental assessedvalue, you can use the /predict endpoint. Here's an example request:
curl -X POST http://127.0.0.1:5000/predict \
-H 'Content-Type: application/json' \
-d '{
"interior_bedrooms": 2,
"interior_fullbaths": 1,
"interior_halfbaths": 1,
"condition_overallcondition_cleansed": "Good"
}'To stop the Flask app, you can press Ctrl + C in the terminal window where the app is running.
-
Environment variables not being set: Ensure you have set the environment variables correctly, especially when switching between macOS and Windows.
-
Database initialization issues: If the app crashes because of database-related errors, make sure you have run the
flask shellcommands to initialize the database properly. -
Dependency issues: Ensure that you are using the correct version of Python (3.9+) and have installed the dependencies using
pip install -r requirements.txt.
This project is licensed under the MIT License.
We use pytest for running tests on this application. Before running the tests, ensure all dependencies are installed and the application is properly set up.
- Install the required dependencies by running:
pip install -r requirements.txt- Export the
PYTHONPATHenvironment variable to ensure Python can locate the app module.
For macOS/Linux:
export PYTHONPATH=.For Windows (PowerShell):
$env:PYTHONPATH="."
pytest- Run the tests:
pytestThis will execute all the tests located in the tests/ folder and provide feedback on the application behavior.
Before deploying the application to Heroku, you need to install the Heroku CLI. You can follow the steps below to install it on your machine.
You can install the Heroku CLI using Homebrew:
brew tap heroku/brew && brew install herokuDownload and run the Heroku installer from the Heroku Dev Center.
Once installed, verify the installation by running:
heroku --versionYou should see the version of Heroku CLI installed.
Log in to your Heroku account from the terminal:
heroku loginThis will open a web browser for you to log in to your Heroku account.
Ensure your requirements.txt and Procfile are present in the project root.
- Procfile: Create a
Procfilein the root directory with the following content to tell Heroku how to run the app:
web: flask run --host=0.0.0.0 --port=$PORTRun the following command to create a new Heroku app:
heroku createAfter you've created your Heroku app, deploy your app using Git:
git add .
git commit -m "Initial commit"
git push heroku mainHeroku apps require at least one running dyno. Scale your app to run one web dyno:
heroku ps:scale web=1Once your app is deployed, you can open it in the browser using:
heroku openYour app should now be live on Heroku!