ValueError:查询数据维度必须与训练数据维度匹配-Python SciPy ValueErrror

时间:2019-10-10 07:52:36

标签: python python-3.x tensorflow scikit-learn

我正在尝试让计算机预先猜测玩家将要选择的内容,具体取决于玩家在先前尝试中选择的内容:

import random
from sklearn.datasets import load_iris 
from sklearn.neighbors import KNeighborsClassifier 
import numpy as np 
from sklearn.model_selection import train_test_split

iris_dataset=load_iris()

while True:
    print()
    print("Do you choose rock, paper or scissors?")
    guessArray = []
    resultArray = []
    guess = str(input())
    guess = guess.lower()
    guessNum = 0
    if guess == 'rock': 
        guessArray.append(1)
    elif guess == 'paper':
        guessArray.append(2)
    elif guess == 'scissors':
        guessArray.append(3)
    else:
        guessArray.append(4)
    if guess == 'rock': 
        guessNum = 1
    elif guess == 'paper':
        guessNum = 2
    elif guess == 'scissors':
        guessNum = 3
    else:
        guessNum = 4
    choices = ['rock', 'paper', 'scissors']
    print('You guessed', guess)
    X_train, X_test, y_train, y_test = train_test_split(iris_dataset["data"], iris_dataset["target"], random_state=0)
    kn = KNeighborsClassifier(n_neighbors=1) 
    kn.fit(X_train, y_train)
    x_new = np.array([guessArray])
    prediction = kn.predict(x_new)
    computer_guess = format(prediction)
    print('Computer guess', computer_guess)
    if guess in choices:
        if guessNum == computer_guess:
            result = 'Tie'
        elif guessNum == 1:
            if computer_guess == 3:
                result = 'Win'
            elif computer_guess == 2:
                result = 'Loose'
        elif guess == 2:
            if computer_guess == 2:
                result = 'Loose'
            elif computer_guess == 1:
                result = 'Win'
        elif guess == 3:
            if computer_guess == 2:
                result = 'Win'
            elif computer_guess == 1:
                result = 'Loose'
    else: 
        result = 'Invalid'

    resultArray.append(result)
    #Print result



    #print("Predicted target value: {}\n".format(prediction)) 
    #print("Predicted feature name: {}\n".format
    #    (iris_dataset["target_names"][prediction])) 
    #print("Test score: {:.2f}".format(kn.score(X_test, y_test))

代码可以正常运行,甚至可以正常打印所有内容,直到“ prediction = kn.predict(x_new)”行,然后一切都变得混乱。我发现我需要以某种方式重塑x_new变量,但是由于我今天才开始使用python编程,除了.net和c#的真正基础知识之外,从未与其他机器学习合作过。

Traceback (most recent call last):
  File "rockpaperscissors.py", line 40, in <module>
    prediction = kn.predict(x_new)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\neighbors\classification.py", line 149, in predict
    neigh_dist, neigh_ind = self.kneighbors(X)
  File "C:\Users\viktor.landstrom.LEARNET\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\neighbors\base.py", line 454, in kneighbors
    for s in gen_even_slices(X.shape[0], n_jobs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\joblib\parallel.py", line 1003, in __call__
    if self.dispatch_one_batch(iterator):
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\joblib\parallel.py", line 834, in dispatch_one_batch
    self._dispatch(tasks)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\joblib\parallel.py", line 753, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\joblib\_parallel_backends.py", line 201, in apply_async
    result = ImmediateResult(func)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\joblib\_parallel_backends.py", line 582, in __init__
    self.results = batch()
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\joblib\parallel.py", line 256, in __call__
    for func, args, kwargs in self.items]
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\joblib\parallel.py", line 256, in <listcomp>
    for func, args, kwargs in self.items]
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\neighbors\base.py", line 291, in _tree_query_parallel_helper
    return tree.query(data, n_neighbors, return_distance)
  File "sklearn\neighbors\binary_tree.pxi", line 1317, in sklearn.neighbors.kd_tree.BinaryTree.query
ValueError: query data dimension must match training data dimension

0 个答案:

没有答案
相关问题