Я работаю над использованием sklearn
train_test_split
для создания обучающего набора и тестирования моих данных.
Мой скрипт ниже:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import neighbors
# function to perform one hot encoding and dropping the original item
# in this case its the part number
def encode_and_bind(original_dataframe, feature_to_encode):
dummies = pd.get_dummies(original_dataframe[[feature_to_encode]])
res = pd.concat([original_dataframe, dummies], axis=1)
res = res.drop([feature_to_encode], axis=1)
return(res)
# read in data from csv
data = pd.read_csv('export2.csv')
# one hot encode the part number
new = encode_and_bind(data, 'PART_NO')
# create the labels, or field we are trying to estimate
label = new['TOTAL_DAYS_TO_COMPLETE']
# remove the header
label = label[1:]
# create the data, or the data that is to be estimated
thedata = new.drop('TOTAL_DAYS_TO_COMPLETE', axis=1)
# remove the header
thedata = thedata[1:]
print(label.shape)
print(thedata.shape)
# # split into training and testing sets
train_data, train_classes, test_data, test_classes = train_test_split(thedata, label, test_size = 0.3)
# create a knn model
knn = neighbors.KNeighborsRegressor()
# fit it with our data
knn.fit(train_data, train_classes)
Запустив его, я получаю следующее:
C: \ Users \ jerry \ Desktop> python test.py (6262,) (6262, 253) Traceback
(последний вызов был последним): файл "test.py", строка 37, в
knn.fit (train_data, train_classes) Файл "C: \ Python367-64 \ lib \ site-packages \ sklearn \ neighbors \ base.py", строка
872, в форме
X, y = check_X_y (X, y, "csr", multi_output = True) Файл "C: \ Python367-64 \ lib \ site-packages \ sklearn \ utils \ validation.py", строка
729, в check_X_y
check_consistent_length (X, y) Файл "C: \ Python367-64 \ lib \ site-packages \ sklearn \ utils \ validation.py", строка
205, в check_consistent_length
"samples:% r"% [int (l) для l в длинах]) ValueError: Найдены входные переменные с непоследовательным количеством выборок: [4383, 1879]
Итак, похоже, что и мои X
и Y
имеют одинаковое количество строк (6262), но различное количество столбцов, поскольку я думал, что Y
должен был быть только один столбец метки или значение, которое вы пытаетесь предсказать.
Как я могу использовать train_test_split
, чтобы дать мне набор данных для обучения и тестирования, который я могу использовать для регрессора KNN?