0

I just started Tensorflow and am solving this problem but I am getting errors. The problem is that the base price for a house is 50k and each bedroom costs 50k each. So a 1 bedroom house is 100k, 2 bedroom is 150k and so on. We have to predict the cost of a 7 Bedroom house.

I have tried using 'import numpy as np' and also 'import numpy' but error still remains.

import tensorflow as tf
import numpy
from tensorflow import keras

def house_model(y_new):
    xs = numpy.array([1.0,2.0,3.0,4.0], dtype = float)
    ys = numpy.array([1.0,1.5,2.0,2.5], dtype = float)
    model = tf.keras.Sequential([keras.layers.dense(units=1, input_shape=[1])])
    model.compile(optimizer = 'sgd', loss = 'mean_squared_error')
    model.fit(xs,ys,epochs = 500)
    return model.predict(y_new)[7.0]

prediction = house_model([7.0])
print(prediction)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-23-55d468d60746> in <module>
----> 1 prediction = house_model([7.0])
      2 print(prediction)

<ipython-input-20-0e67265afcf6> in house_model(y_new)
      1 def house_model(y_new):
----> 2     xs = numpy.array([1.0,2.0,3.0,4.0], dtype = float)
      3     ys = numpy.array([1.0,1.5,2.0,2.5], dtype = float)
      4     model = tf.keras.Sequential([keras.layers.dense(units=1, input_shape=[1])])
      5     model.compile(optimizer = 'sgd', loss = 'mean_squared_error')

NameError: name 'numpy' is not defined

2 Answers2

0

It seems like numpy isn't installed/present in your python interpreter's library path. I think you need to either install the Numpy module to use it or check if the correct path is getting used by your interpreter.

kokeen
  • 326
  • 1
  • 2
  • 8
  • I have installed NumPy and how do I check the path by interpreter? – Siddharth Rana Aug 17 '19 at 05:57
  • [Here](https://stackoverflow.com/questions/14050281/how-to-check-if-a-python-module-exists-without-importing-it) This has a detailed answer to check if a module is present in your python path. – kokeen Aug 17 '19 at 06:01
0

Yes you should consider installing NumPy using !pip install numpy Also, you used a small letter d (keras.layers.dense)-this is wrong. It should be keras.layers.Dense

Sky
  • 127
  • 9