pandas 如何在 Python Numpy 中使用 train_test_split 修复值错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56396950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:23:36  来源:igfitidea点击:

How to fix Value Error with train_test_split in Python Numpy

pythonpandasnumpysklearn-pandas

提问by python_beginner

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

我正在使用带有 numpy 数组的 sklearn。我有 2 个数组 (x, y),它们应该是:

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?

任何想法为什么会出现?

回答by Alejandro Aldana

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

我有那个问题。检查库“scikit-learn”。sklearn scikt-learn 0.20.0+版本有问题,尝试做:

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

视窗: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

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