Skip to content

Instantly share code, notes, and snippets.

@ckdotca
Created September 13, 2014 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckdotca/ef6c02cdbe8a78386c60 to your computer and use it in GitHub Desktop.
Save ckdotca/ef6c02cdbe8a78386c60 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "features_and_weights"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": "# import some libraries \n\nfrom sklearn import linear_model\nimport numpy as np",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 198
},
{
"cell_type": "code",
"collapsed": false,
"input": "# enter the features and prices of the houses on the market\n\nhouse_one_features = np.array([2, 1.5, 1]) # 2 bedroom, 1.5 bathroom, 1 floor house\nhouse_one_price = np.array([430000]) # $430,000 house\n\nhouse_two_features = np.array([4, 3, 2]) # 4 bedroom, 3 bathroom, 2 floor house \nhouse_two_price = np.array([980000]) # $980,000 house\n\nhouse_three_features = np.array([3, 2, 1.5]) # 3 bedroom, 2 bathroom, 1.5 floor house\nhouse_three_price = np.array([660000]) # $660,000 house\n",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 199
},
{
"cell_type": "code",
"collapsed": false,
"input": "# put all the features together, and all the prices together\n# the features are lined up with their corresponding house price\n\nhouse_features = np.array([house_one_features, house_two_features, house_three_features])\nhouse_prices = np.array([house_one_price, house_two_price, house_three_price])",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 200
},
{
"cell_type": "code",
"collapsed": false,
"input": "# calculate the weights based on the features and prices\n\nmodel = linear_model.LinearRegression(fit_intercept=False) # set up a model\nmodel.fit(house_features, house_prices) # calculate the weights. this single line does all the math to learn how to do the prediction\nprint 'Weights: ', model.coef_",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Weights: [[ 54400. 228000. 27200.]]\n"
}
],
"prompt_number": 201
},
{
"cell_type": "code",
"collapsed": false,
"input": "# enter the features for the house whose price we want to predict\n\nhouse_four_features = np.array([3, 2, 1]) # 3 bedroom, 2 bathroom, 1 floor house",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 202
},
{
"cell_type": "code",
"collapsed": false,
"input": "# use the weights that we just calculated to predict the price\n\npredicted_price = model.predict(house_four_features) # predict the price\nprint 'Predicted price of house: ', predicted_price",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Predicted price of house: [ 646400.]\n"
}
],
"prompt_number": 203
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment