训练集中的训练数据

时间:2020-01-31 14:46:48

标签: python scikit-learn data-science

是,尝试使用 scikit-learn 在我的训练集中训练模型,但出现此错误:

 ValueError: Expected 2D array, got 1D array instead: array=[90.  4.].
 Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

步骤1:将x和y分为训练和测试集

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.4, random_state = 4)

检查新的分割x值的形状(训练和测试)

X_train = X_train.shape
X_test = X_test.shape
print(X_train)
print(X_test)

检查新分割的y值的形状(训练和测试)

y_train = y_train.shape
y_test = y_test.shape
print(y_train)
print(y_test)

步骤2:在训练集上训练我们的模型(使用物流回归)

logR = LogisticRegression()
logR = logR.fit(X_train, y_train)

运行此代码时出现错误

2 个答案:

答案 0 :(得分:0)

您似乎正在按其形状替换数据点:

X_train = X_train.shape
X_test = X_test.shape
y_train = y_train.shape
y_test = y_test.shape

删除这些行,然后重新运行。

答案 1 :(得分:0)

您做了出色的工作,但做错了一件事情:您将火车和测试数据的形状替换为1D,这就是为什么您会遇到此错误的原因

import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main {

    public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();

        String result =
                IntStream.range(1, Math.min(n, 100)).mapToObj(x ->
                        {
                            if (x % 5 == 0) {
                                switch (x % 3) {
                                    case 2:
                                        return "Bus";
                                    case 1:
                                        return "bUs";
                                    case 0:
                                        return "buS";
                                }
                            }
                            return x + "";
                        }
                ).collect(Collectors.joining(", "));

        System.out.println(result);
    }
}