Features and labels

X and y from arrays and DataFrames. Never fit on the label column.

scikit-learn wants X (features, 2-D) and y (labels, 1-D). Rows are examples. Columns of X are measurements. y is what you are trying to predict — it must not also sit inside X.

Goal

Build X and y from a NumPy array and from a DataFrame, and check shapes.

From arrays

from sklearn.datasets import make_classification

X, y = make_classification(n_samples=12, n_features=3, n_informative=2, random_state=0)
print("X shape", X.shape, "y shape", y.shape)
print("X first 3 rows:\\n", X[:3].round(2))
print("y", y)

X.shape[0] must equal y.shape[0].

From a DataFrame

df = pd.DataFrame(
    {
        "city": ["Nairobi", "Nairobi", "Mombasa", "Mombasa", "Kisumu", "Kisumu"],
        "units": [12, 7, 9, 4, 11, 3],
        "price": [10.5, 22.0, 10.5, 10.5, 22.0, 10.5],
        "high": [1, 1, 0, 0, 1, 0],
    }
)
X = df[["units", "price"]]
y = df["high"]
print(X)
print()
print("y", y.tolist())
print("X shape", X.shape, "y shape", y.shape)

Double brackets df[["units", "price"]] keep X two-dimensional. df["units"] is 1-D — most estimators reject that as X.

Names vs arrays

df = pd.DataFrame(
    {
        "units": [12, 7, 9, 4, 11, 3],
        "price": [10.5, 22.0, 10.5, 10.5, 22.0, 10.5],
        "high": [1, 1, 0, 0, 1, 0],
    }
)
X = df[["units", "price"]].to_numpy()
y = df["high"].to_numpy()
print(type(X), X.shape)
print(y)

Estimators accept DataFrames or arrays. Pipelines with ColumnTransformer prefer DataFrames so column names stay attached.

Pitfall

If you pass the whole frame as X, including high, the model can “cheat” by reading the answer. Drop the label column first.