-1

I am trying to use machine learning Random Forest for regression problem. I am using python 3.x, and the packages numpy, matplotlib and pandas are already installed on my computer.

I am using the exact same 11 first lines of the video (https://www.youtube.com/watch?v=miI9rwH4Y4g).

My code:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dataset = pd.read_csv('testFile.csv')

X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values

I am having the following errors:

Traceback (most recent call last):
  File "D:\Cours\****************************************\RandomForestRegressionTest.py", line 9, in <module>
    y = dataset.iloc[:, 2].values
  File "C:\Users\******\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 1367, in __getitem__
    return self._getitem_tuple(key)
  File "C:\Users\******\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 1737, in _getitem_tuple
    self._has_valid_tuple(tup)
  File "C:\Users\******\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 204, in _has_valid_tuple
    if not self._has_valid_type(k, i):
  File "C:\Users\******\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 1672, in _has_valid_type
    return self._is_valid_integer(key, axis)
  File "C:\Users\******\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 1713, in _is_valid_integer
    raise IndexError("single positional indexer is out-of-bounds")
IndexError: single positional indexer is out-of-bounds

Any help ???

Thank you

3
  • 2
    Please paste the code that you're running. Commented Jul 26, 2018 at 16:34
  • I have added the code. It is exactly the same as the code of the video Commented Jul 26, 2018 at 16:44
  • 1
    For the future, please try using tags more wisely; question has actually nothing to do with machine-learning or numpy (removed) - it is actually a pure pandas question.
    – desertnaut
    Commented Jul 26, 2018 at 18:12

3 Answers 3

0

The problem was in the csv file. I edited the csv file using Notepad++, and changed all the ";" to "," and it works. It is weird because every csv file use ";" separator not ",".

I am surprised, but also happy because I found the error (wierd error).

3
  • CSV stands for "Comma Separated Values". CSV does not use a semicolon as the separator.
    – David
    Commented Jul 26, 2018 at 17:25
  • 1
    @David this is not true; CSV loosely means delimited text files, even with other field delimiters en.wikipedia.org/wiki/Comma-separated_values
    – desertnaut
    Commented Jul 26, 2018 at 18:10
  • Sorry, I should have said default separator. If someone did not specify, comma separated would naturally be assumed. The poster saying that CSV always uses semicolon is flat wrong.
    – David
    Commented Jul 27, 2018 at 5:54
0

You should try editing your csv file. Save it with csv format and upload it in the notebook. If it is in excel format you can use pd.read_xlsx('filename.xlsx')

0

If it's in Excel format I think you should use some code similar to this:

pd.read_xlsx('filename')

If that doesn't work, maybe try passing in the path of the CSV file. Make sure it's in the same directory as the project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.