# import some libraries
from sklearn import linear_model
import numpy as np
# enter the features and prices of the houses on the market
house_one_features = np.array([2, 1.5, 1]) # 2 bedroom, 1.5 bathroom, 1 floor house
house_one_price = np.array([430000]) # $430,000 house
house_two_features = np.array([4, 3, 2]) # 4 bedroom, 3 bathroom, 2 floor house
house_two_price = np.array([980000]) # $980,000 house
house_three_features = np.array([3, 2, 1.5]) # 3 bedroom, 2 bathroom, 1.5 floor house
house_three_price = np.array([660000]) # $660,000 house
# put all the features together, and all the prices together
# the features are lined up with their corresponding house price
house_features = np.array([house_one_features, house_two_features, house_three_features])
house_prices = np.array([house_one_price, house_two_price, house_three_price])
# calculate the weights based on the features and prices
model = linear_model.LinearRegression(fit_intercept=False) # set up a model
model.fit(house_features, house_prices) # calculate the weights. this single line does all the math to learn how to do the prediction
print 'Weights: ', model.coef_
Weights: [[ 54400. 228000. 27200.]]
# enter the features for the house whose price we want to predict
house_four_features = np.array([3, 2, 1]) # 3 bedroom, 2 bathroom, 1 floor house
# use the weights that we just calculated to predict the price
predicted_price = model.predict(house_four_features) # predict the price
print 'Predicted price of house: ', predicted_price
Predicted price of house: [ 646400.]