09.05.2023 Views

pdfcoffee

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Regression

2. Now, we define the feature columns we will be using to train the regressor.

Our dataset, as we mentioned, consists of two features "area" a numeric

value signifying the area of the house and "type" telling if it is a "bungalow"

or "apartment":

featcols = [

tf.feature_column.numeric_column("area"),

tf.feature_column.categorical_column_with_vocabulary_list("type",[

"bungalow","apartment"])

]

3. In the next step, we define an input function to provide input for training.

The function returns a tuple containing features and labels:

def train_input_fn():

features = {"area":[1000,2000,4000,1000,2000,4000],

"type":["bungalow","bungalow","house",

"apartment","apartment","apartment"]}

labels = [ 500 , 1000 , 1500 , 700 , 1300 , 1900 ]

return features, labels

4. Next, we use the premade LinearRegressor estimator and fit it on the

training dataset:

model = tf.estimator.LinearRegressor(featcols)

model.train(train_input_fn, steps=200)

5. Now that the estimator is trained, let us see the result of the prediction:

def predict_input_fn():

features = {"area":[1500,1800],

"type":["house","apt"]}

return features

predictions = model.predict(predict_input_fn)

print(next(predictions))

print(next(predictions))

--------------------------------------------------

6. The result:

{'predictions': array([692.7829], dtype=float32)}

{'predictions': array([830.9035], dtype=float32)}

[ 96 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!