How to fix Value Error with train_test_split in Python Numpy

14,960

I had that problem. Check the library "scikit-learn". sklearn have problems with the version 0.20.0+ of scikt-learn, try to do:

Windows: pip uninstall scikit-learn
Linux: sudo python36 -m pip uninstall scikit-learn

and install:

Windows: pip install scikit-learn==0.19.1
Linux: sudo python36 -m pip install scikit-learn==0.19.1

Share:
14,960
python_beginner
Author by

python_beginner

Updated on June 04, 2022

Comments

  • python_beginner
    python_beginner about 2 years

    I am using sklearn with a numpy array. I have 2 arrays (x, y) and they should be:

    test_size=0.2
    train_size=0.8
    

    This is my current code:

    def predict():
    
        sample_data = pd.read_csv("includes\\csv.csv")
    
        x = np.array(sample_data["day"])
        y = np.array(sample_data["balance"])
    
    
        x = x.reshape(1, -1)
    
    
    
        y = y.reshape(1, -1)
    
    
    
    
        print(x)
        print(y)
    
    
    
        X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
    
    
    
        clf = LinearRegression()
        clf.fit(x_train, y_train)
    
        clf.score(x_test, y_test)
    
    

    The error is:

    ValueError: With n_samples=1, test_size=0.2 and train_size=None, the resulting train set will be empty. Adjust any of the aforementioned parameters.
    

    , and it appears in the line:

    X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
    

    Any ideas why that appears?

    • Quang Hoang
      Quang Hoang about 5 years
      Did you try: train_test_split(x, y, test_size=0.2, train_size=0.8)?
    • hpaulj
      hpaulj about 5 years
      It splits along the first axis, which in your samples is size 1. Review the sklearn docs to understand its conventions for the shapes of the inputs - number of samples versus number of features.
    • python_beginner
      python_beginner about 5 years
      yes i tried that it showed the same error
    • python_beginner
      python_beginner about 5 years
      how can iprevent that from happening @hpaulj
    • GeorgPoe
      GeorgPoe about 5 years
      you could try: x = sample_data["day"].values y = sample_data["balance"].values and remove the reshape command
    • python_beginner
      python_beginner about 5 years
      its not changing anything at all still same error
  • osmancakirio
    osmancakirio over 3 years
    this isn't working any more. Downgrading scikit-learn also downgrades numpy and hence you cause another problem using tensorflow. ImportError: numpy.core.umath failed to import