【问题标题】:How to spliting datasets - Number of labels=150 does not match number of samples=600如何拆分数据集 - 标签数 = 150 与样本数 = 600 不匹配
【发布时间】:2017-09-04 04:20:55
【问题描述】:

我有一个 750x256 的数据样本。

Rows = 750
Columns = 256

如果我将数据分成 20%。我将拥有X_train 600 个样本和y_train 150 个样本。

那么问题会在decisionTreeRegressor时出现

它会说Number of y_train=150 does not match number of samples=600

但如果我将 test_size 分成 50%,那么它会起作用。 有没有办法解决这个问题?我不想使用我的 test_size 的 50%。

任何帮助都会很棒!

这是我的代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import graphviz

#Load the data
dataset = pd.read_csv('new_york.csv')
dataset['Higher'] = dataset['2016-12'].gt(dataset['2016-11']).astype(int)
X = dataset.iloc[:, 6:254].values
y = dataset.iloc[:, 255].values

#Taking care of missing data
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer = imputer.fit(X[:, :248])
X[:, :248] = imputer.transform(X[:, :248])

#Split the data into train and test sets
from sklearn.cross_validation import train_test_split
X_train, X_test, y_test, y_train = train_test_split(X, y, test_size = .2,    random_state = 0)

#let's build our first model
from sklearn.tree import DecisionTreeRegressor, DecisionTreeClassifier, export_graphviz
clf = DecisionTreeClassifier(max_depth=6)
clf.fit(X_train, y_train)
clf.score(X_train, y_train)

【问题讨论】:

    标签: python pandas machine-learning scikit-learn decision-tree


    【解决方案1】:

    train_test_split() 返回X_train, X_test, y_train, y_test,您的 y_train 和 y_test 顺序错误。

    如果您使用 50% 的拆分,这不会导致错误,因为 y_train 和 y_test 将具有相同的大小(但显然是错误的值)。

    【讨论】:

      猜你喜欢
      • 2021-07-28
      • 2018-08-17
      • 1970-01-01
      • 2019-06-02
      • 2018-10-31
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多