Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
6.0k views
in Technique[技术] by (71.8m points)

python - ValueError: Found input variables with inconsistent numbers of samples: [1600, 400]

This is the code:

from sklearn.model_selection import train_test_split
x_train,y_train,x_test,y_test=train_test_split(x,y,test_size=0.2,random_state=2020)

from sklearn.neighbors import KNeighborsClassifier
clf=KNeighborsClassifier(n_neighbors=5,metric='euclidean')
clf

clf.fit(x_train,y_train)

This is the error I get:

---------------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)
<ipython-input-52-91f676f4a5e0> in <module>()
      1 
----> 2 clf.fit(x_train,y_train)

2 frames
/usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py in check_consistent_length(*arrays)
    210     if len(uniques) > 1:
    211         raise ValueError("Found input variables with inconsistent numbers of"
--> 212                          " samples: %r" % [int(l) for l in lengths])
    213 
    214 

ValueError: Found input variables with inconsistent numbers of samples: [1600, 400]

How can I fix this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You misunderstood how train_test_split works, if you read the docs properly, you can't miss the example:

 X_train, X_test, y_train, y_test = train_test_split(...)

Your code:

x_train,y_train,x_test,y_test=train_test_split(x,y,test_size=0.2,random_state=2020)

needs to be changed in this way:

x_train, x_test, y_train, y_test=train_test_split(x,y,test_size=0.2,random_state=2020)

The rest of the code will then run.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...