试图理解 ML 上的示例脚本

Trying to understand an example script on ML

我正在尝试完成有关机器学习的示例脚本:Common pitfalls in interpretation of coefficients of linear models 但我在理解某些步骤时遇到了问题。脚本的开头如下所示:

import numpy as np
import scipy as sp
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.datasets import fetch_openml

survey = fetch_openml(data_id=534, as_frame=True)

# We identify features `X` and targets `y`: the column WAGE is our
# target variable (i.e., the variable which we want to predict).
X = survey.data[survey.feature_names]
X.describe(include="all")

X.head()

# Our target for prediction is the wage.
y = survey.target.values.ravel()
survey.target.head()

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

train_dataset = X_train.copy()
train_dataset.insert(0, "WAGE", y_train)
_ = sns.pairplot(train_dataset, kind='reg', diag_kind='kde')

我的问题在行

y = survey.target.values.ravel()
survey.target.head()

如果我们在这些行之后立即检查 survey.target.head(),输出是

Out[36]: 
0    5.10
1    4.95
2    6.67
3    4.00
4    7.50
Name: WAGE, dtype: float64

模型如何知道WAGE是目标变量?是否不必明确声明?

survey.target.values.ravel()是为了展平数组,但在本例中没有必要。 survey.target 是一个 pd 系列(即 1 列数据框),survey.target.values 是一个 numpy 数组。您可以将两者都用于 train/test 拆分,因为 survey.target 中只有 1 列。

type(survey.target)
pandas.core.series.Series

type(survey.target.values)
numpy.ndarray

如果我们只使用 survey.target,您会发现回归会起作用:

y = survey.target

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

train_dataset = X_train.copy()
train_dataset.insert(0, "WAGE", y_train)
sns.pairplot(train_dataset, kind='reg', diag_kind='kde')

如果您有另一个数据集,例如鸢尾花,我想根据其余部分回归花瓣宽度。您可以使用方括号 [] 调用 data.frame 的列:

from sklearn.datasets import load_iris
from sklearn.linear_model import LinearRegression

dat = load_iris(as_frame=True).frame

X = dat[['sepal length (cm)','sepal width (cm)','petal length (cm)']]
y = dat[['petal width (cm)']]

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

LR = LinearRegression()
LR.fit(X_train,y_train)
plt.scatter(x=y_test,y=LR.predict(X_test))