smallML / model.py
soham901's picture
Upload 2 files
b59614d
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import numpy as np
# Create a small dataset with 10 rows representing house prices
X = np.array([[1000], [1500], [2000], [2500], [3000], [3500], [4000], [4500], [5000], [5500]])
y = np.array([50000, 75000, 100000, 125000, 150000, 175000, 200000, 225000, 250000, 275000])
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create an instance of the Linear Regression model
model = LinearRegression()
# Train the model on the training data
model.fit(X_train, y_train)
# Make predictions on the testing data
y_pred = model.predict(X_test)
# Evaluate the model's performance
mse = mean_squared_error(y_test, y_pred)
coef = model.coef_[0]
intercept = model.intercept_
score = model.score(X_test, y_test)
def predict(sqft):
return (model.predict([[sqft]])[0]).round(2)
def get_model_details():
return {"mse": mse, "coef": coef, "intercept": intercept, "score": score}