[Python] Different answers with imputation of missing values

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

  1. Stack

    Stack Membro Participativo

    I have made a classifier using Logistic Regression and for testing it I used the breast cancer dataset available at:

    https://archive.ics.uci.edu/ml/datasets/Breast Cancer Wisconsin (Original)

    This dataset contains missing values so I have changed those values with three options:

    1. Fill them with a value that is below any data from the dataset
    2. Use a Imputer with the data frame
    3. Use a Imputer, but instead of using the data frame I have used an array of numpy

    The issue is that the results from option (1) and (3) are almost similar, but option (2) makes a huge Type II error. My code and results are:

    import pandas as pd
    import numpy as np
    from sklearn import preprocessing, model_selection, linear_model
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import Imputer
    from sklearn.impute import SimpleImputer
    from sklearn.metrics import accuracy_score,confusion_matrix
    import seaborn as sns
    import matplotlib.pyplot as plt

    def readfile(name):
    df=pd.read_csv(nombre,names=['id', 'clump_thickness','unif_cell_size',
    'unif_cell_shape', 'marg_adhesion', 'single_epith_cell_size',
    'bare_nuclei', 'bland_chromatin', 'normal_nucleoli','mitoses','class'])
    return df

    def outlier(df):
    #OPTION 1
    df.drop(['id'], 1, inplace=True)
    df.replace('?', -99999, inplace=True)
    return df

    def mediaFill(df):
    #OPTION 2
    df.replace('?',np.NaN,inplace=True)
    imp=SimpleImputer(missing_values=np.NaN)
    idf=pd.DataFrame(imp.fit_transform(df))
    idf.columns=df.columns
    idf.index=df.index
    return idf

    def funcFill():
    #OPTION 3
    data = np.genfromtxt("breast-cancer-wisconsin.data",delimiter=",")
    X = data[:,1:-1]
    X[X == '?'] = 'NaN'
    imputer = Imputer()
    X = imputer.fit_transform(X)
    y = data[:, -1].astype(int)
    X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.2)
    lg=linear_model.LogisticRegression(solver="liblinear")
    lg.fit(X_train,y_train)
    predictions = lg.predict(X_test)
    cm=confusion_matrix(y_test,predictions)
    print(cm)
    score = lg.score(X_test, y_test)
    print(score)

    def LogisticFunc(df):
    X = np.array(df.drop(['class'],1))
    y = np.array(df['class'])
    labels=[2,4]
    X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size = 0.2)
    clf = linear_model.LogisticRegression(solver="liblinear")
    clf.fit(X_train,y_train)
    y_pred = clf.predict(X_test)
    conf = confusion_matrix(y_test, y_pred, labels)
    print (conf)
    print (accuracy_score(y_pred,y_test))

    def main():
    file="breast-cancer-wisconsin.data"
    df=readfile(file)
    df=outlier(df)
    LogisticFunc(df)

    df=readfile(file)
    df=mediaFill(df)
    LogisticFunc(df)

    df=readfile(file)
    funcFill()

    if __name__=="__main__":
    main()


    My results are: Option 1:

    [[97 1]
    [ 2 40]]


    Option 2:

    [[89 0]
    [51 0]]


    Option 3:

    [[92 2]
    [ 2 44]]


    Why does it differ too much Option 2?

    Continue reading...

Compartilhe esta Página