Skip to content

xy

plot_y_vs_X(X, y, nrows=5, ncols=2, figsize=(30, 20), ylabel='y', feature_names=None)

Plots y versus each feature in X.

Parameters:

Name Type Description Default
X ndarray

2D array of input features.

required
y ndarray

1D array of target values.

required
nrows int

Number of rows in the subplot grid. Defaults to 5.

5
ncols int

Number of columns in the subplot grid. Defaults to 2.

2
figsize tuple

Size of the entire figure. Defaults to (30, 20).

(30, 20)
ylabel str

Label for the y-axis. Defaults to ‘y’.

'y'
feature_names list of str

List of feature names. Defaults to None. If None, generates feature names as x0, x1, etc.

None

Examples:

>>> from sklearn.datasets import load_diabetes
>>> from spotpython.plot.xy import plot_y_vs_X
>>> data = load_diabetes()
>>> X, y = data.data, data.target
>>> plot_y_vs_X(X, y, nrows=5, ncols=2, figsize=(20, 15))
Source code in spotpython/plot/xy.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def plot_y_vs_X(X, y, nrows=5, ncols=2, figsize=(30, 20), ylabel="y", feature_names=None):
    """
    Plots y versus each feature in X.

    Args:
        X (ndarray):
            2D array of input features.
        y (ndarray):
            1D array of target values.
        nrows (int, optional):
            Number of rows in the subplot grid. Defaults to 5.
        ncols (int, optional):
            Number of columns in the subplot grid. Defaults to 2.
        figsize (tuple, optional):
            Size of the entire figure. Defaults to (30, 20).
        ylabel (str, optional):
            Label for the y-axis. Defaults to 'y'.
        feature_names (list of str, optional):
            List of feature names. Defaults to None. If None, generates feature names as x0, x1, etc.

    Examples:
        >>> from sklearn.datasets import load_diabetes
        >>> from spotpython.plot.xy import plot_y_vs_X
        >>> data = load_diabetes()
        >>> X, y = data.data, data.target
        >>> plot_y_vs_X(X, y, nrows=5, ncols=2, figsize=(20, 15))
    """
    if feature_names is None:
        feature_names = [f"x{i}" for i in range(X.shape[1])]

    fig, axs = plt.subplots(nrows=nrows, ncols=ncols, figsize=figsize)

    for i, (ax, col) in enumerate(zip(axs.flat, feature_names)):
        x = X[:, i]
        pf = np.polyfit(x, y, 1)
        p = np.poly1d(pf)

        ax.plot(x, y, "o")
        ax.plot(x, p(x), "r--")

        ax.set_title(col + " " + ylabel)
        ax.set_xlabel(col)
        ax.set_ylabel(ylabel)

    plt.tight_layout()
    plt.show()