【问题标题】:Scikit-learn - What am I predicting?Scikit-learn - 我在预测什么?
【发布时间】:2021-05-25 18:22:36
【问题描述】:

我的目标是根据 6 列的 csv 数据预测数组中的 5 到 6 个数字。下面的脚本应该只预测一个数字,从 5 的数组中。我以为我可以从那里一直到整个 5 或 6,但我可能错了。

先生:

import csv
import numpy as np 
import pandas as pd
from math import sqrt
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from sklearn.preprocessing import StandardScaler

df = pd.read_csv('subdata.csv')

ft = [9,8,15,4,6]

fintest = np.array(ft)

def train():

    df.astype(np.float64)
    df.drop(['One'], axis = 1)
    X = df
    y = X['One']

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=123)

    scaler = StandardScaler()
    train_scaled = scaler.fit_transform(X_train)
    test_scaled = scaler.transform(X_test)

    tree_model = DecisionTreeRegressor()
    rf_model = RandomForestRegressor()

    tree_model.fit(train_scaled, y_train)
    rf_model.fit(train_scaled, y_train)

    rfp = rf_model.predict(fintest.reshape(1, -1))
    tmp = tree_model.predict(fintest.reshape(1, -1))

    print(rfp)
    print(tmp)

train()

您能否澄清一下,我要求此脚本在最后的 rfptmp 行中预测什么?

我的数据如下所示:

目前的脚本会出错:

    Traceback (most recent call last):
  File "C:\Users\conra\Desktop\Code\lotto\pie.py", line 43, in <module>
    train()
  File "C:\Users\conra\Desktop\Code\lotto\pie.py", line 37, in train
    rfp = rf_model.predict(fintest.reshape(1, -1))
  File "C:\Users\conra\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\ensemble\_forest.py", line 784, in predict
    X = self._validate_X_predict(X)
  File "C:\Users\conra\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\ensemble\_forest.py", line 422, in _validate_X_predict
    return self.estimators_[0]._validate_X_predict(X, check_input=True)
  File "C:\Users\conra\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\tree\_classes.py", line 402, in _validate_X_predict
    X = self._validate_data(X, dtype=DTYPE, accept_sparse="csr",
  File "C:\Users\conra\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\base.py", line 437, in _validate_data
    self._check_n_features(X, reset=reset)
  File "C:\Users\conra\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\base.py", line 365, in _check_n_features
    raise ValueError(
ValueError: X has 5 features, but DecisionTreeRegressor is expecting 6 features as input.

通过向ft 数组添加第六位数字,我可以绕过这个错误并接收到非常不准确的输出,这些输出似乎与数据没有任何关联。例如,通过将变量 ft 设置为 csv 文件中的第一行 [9,8,15,4,6,2],并将 X 和 y 设置为使用“四”标签;我得到[37.22][37.] 的输出。

我的其他问题可能会由我的第一个回答。但他们在这里:

能否请您解释一下为什么我需要传递 6 个数组?

无论我为预测传递什么数组,为什么我的预测都如此接近(全部约 35 个)?

【问题讨论】:

    标签: python machine-learning scikit-learn


    【解决方案1】:

    取决于您的数据,就像我看到的数据就是没有上下文的一个例子,所以实际上你试图训练你的数据来使用两个不同的 models 模型来预测'一个'列,即对我来说没有意义。

    错误是因为你给出了X dataframe没有列'一个',在你询问第一个'到变量YY=X['One']

    【讨论】:

      【解决方案2】:

      您定义 X 的方式是错误的。它包含 6 个功能。

      您的 y 以您定义的方式包含在您的 X 中:

      X = df #6 features
      y = X['One'] #1 feature
      

      我认为你想做的是这样的:

      X = df[['Two', 'Three', 'Four', 'Five', 'Zero']]
      y = df['One']
      

      【讨论】:

        猜你喜欢
        • 2019-01-08
        • 2015-12-19
        • 2018-01-09
        • 2015-03-11
        • 2015-01-12
        • 2016-04-01
        • 2015-03-05
        • 2016-04-03
        • 1970-01-01
        相关资源
        最近更新 更多