Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run gradient_descent.py? #5

Open
fujishoichi opened this issue Sep 15, 2019 · 2 comments
Open

How to run gradient_descent.py? #5

fujishoichi opened this issue Sep 15, 2019 · 2 comments

Comments

@fujishoichi
Copy link

Hi,

I tried running gradient_descent.py.
But it failed.

$ python gradient_descent.py 
Traceback (most recent call last):
  File "gradient_descent.py", line 73, in <module>
    x, y = np.loadtxt("data.txt", delimiter= "\t", unpack = True)
  File "/Users/fujisho/.pyenv/versions/3.7.4/lib/python3.7/site-packages/numpy/lib/npyio.py", line 1146, in loadtxt
    for x in read_data(_loadtxt_chunksize):
  File "/Users/fujisho/.pyenv/versions/3.7.4/lib/python3.7/site-packages/numpy/lib/npyio.py", line 1074, in read_data
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "/Users/fujisho/.pyenv/versions/3.7.4/lib/python3.7/site-packages/numpy/lib/npyio.py", line 1074, in <listcomp>
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "/Users/fujisho/.pyenv/versions/3.7.4/lib/python3.7/site-packages/numpy/lib/npyio.py", line 781, in floatconv
    return float(x)
ValueError: could not convert string to float: 'The dataset for gradient descent example can be downloaded from: http://themlbook.com/wiki/doku.php?id=gradient_descent'

I downloaded the CSV pointed in data.txt and loaded it instead of it, but it didn't work as well:

$ python gradient_descent.py
Traceback (most recent call last):
  File "gradient_descent.py", line 73, in <module>
    x, y = np.loadtxt("Advertising.csv", delimiter= "\t", unpack = True)
  File "/Users/fujisho/.pyenv/versions/3.7.4/lib/python3.7/site-packages/numpy/lib/npyio.py", line 1146, in loadtxt
    for x in read_data(_loadtxt_chunksize):
  File "/Users/fujisho/.pyenv/versions/3.7.4/lib/python3.7/site-packages/numpy/lib/npyio.py", line 1074, in read_data
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "/Users/fujisho/.pyenv/versions/3.7.4/lib/python3.7/site-packages/numpy/lib/npyio.py", line 1074, in <listcomp>
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "/Users/fujisho/.pyenv/versions/3.7.4/lib/python3.7/site-packages/numpy/lib/npyio.py", line 781, in floatconv
    return float(x)
ValueError: could not convert string to float: ',TV,radio,newspaper,sales'

It would be helpful if you can provide any tips to solve the problem.

@Qu3tzal
Copy link

Qu3tzal commented Jan 9, 2020

Looks like the gradient_descent.py script expects a tabular CSV.
You have to remove the header (the first line of Advertising.csv), remove the ids (the first column of Advertising.csv), and then replace the comas by tabs (real tabs, not 2/4/8 spaces).

@dluvishis
Copy link

  1. save advertising.csv to the same folder with the original file
  2. create a new folder "Illustrations" within the same folder
  3. replace lines 12 and 72 with:
    x, y = np.loadtxt("advertising.csv", delimiter= ",", skiprows=1, usecols=(2,4), unpack = True)
    to ignore the header, use "," instead of tabular and use specific columns instead of all 5

It will work now.

BilboBaagins pushed a commit to BilboBaagins/theMLbook that referenced this issue Mar 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants