[Python] Runtime warning in sklearn KMeans

Discussão em 'Python' iniciado por Stack, Maio 20, 2025.

  1. Stack

    Stack Membro Participativo

    I am running k-means using sklearn but has been getting runtime warning. Can you please explain what's happening? I am a novice in sklearn, guidance much appreciated! Below is a sample code for reproducibility:

    import pandas as pd
    import numpy as np
    from sklearn.preprocessing import StandardScaler
    from sklearn.cluster import KMeans

    col1 = np.random.normal(loc=0, scale=1, size=1000)
    col2 = np.random.normal(loc=1, scale=1, size=1000)
    col3 = np.random.normal(loc=2, scale=4, size=1000)
    col4 = np.random.normal(loc=3, scale=3, size=1000)

    df = pd.DataFrame(list(zip(col1, col2, col3, col4)))

    kmeans = KMeans(n_clusters=2, init='k-means++', max_iter=300, random_state=0)
    kmeans.fit(df)


    The warnings are:

    miniconda3/lib/python3.13/site-packages/sklearn/utils/extmath.py:203: RuntimeWarning: divide by zero encountered in matmul
    ret = a @ b
    miniconda3/lib/python3.13/site-packages/sklearn/utils/extmath.py:203: RuntimeWarning: overflow encountered in matmul
    ret = a @ b
    miniconda3/lib/python3.13/site-packages/sklearn/utils/extmath.py:203: RuntimeWarning: invalid value encountered in matmul
    ret = a @ b
    miniconda3/lib/python3.13/site-packages/sklearn/cluster/_kmeans.py:237: RuntimeWarning: divide by zero encountered in matmul
    current_pot = closest_dist_sq @ sample_weight
    miniconda3/lib/python3.13/site-packages/sklearn/cluster/_kmeans.py:237: RuntimeWarning: overflow encountered in matmul
    current_pot = closest_dist_sq @ sample_weight
    miniconda3/lib/python3.13/site-packages/sklearn/cluster/_kmeans.py:237: RuntimeWarning: invalid value encountered in matmul
    current_pot = closest_dist_sq @ sample_weight

    Continue reading...

Compartilhe esta Página