Home > Enterprise >  TypeError: __init__() takes 2 positional arguments but 3 were given - feature extraction using recur
TypeError: __init__() takes 2 positional arguments but 3 were given - feature extraction using recur

Time:02-01

My code is giving the error as TypeError: __init__() takes 2 positional arguments but 3 were given. Tried searching for an extra argument but couldn't get one.

Tried previously answered questions but didn't get any proper solution. My code is as follows:

features = ['EMB_CD3', 'EMB_CD45R0', 'EMB_LFA1', 'EMB_Perforin', 'EMB_Mac', 'EMB_HLA1', 'EMB_CD54', 
            'EMB_VCAM', 'Virus:1=Cox,4=B19V,6=HHV6,5=EBV,2=ADV(including double infections)', 'Viral_load_B19V-VP1_mRNA',
            'Viral_load_DNA_B19V','Virus_EBV', 'Virus_HHV-6', 'B19V-Typ1']
df_new = df[features].copy()
#KNN Imputer for missing values
imputer = KNNImputer(n_neighbors=3)
imputed = imputer.fit_transform(df_new)
df_imputed = pd.DataFrame(imputed, columns=df_new.columns)

X = df_imputed
Y = df['target'].astype(int)
#%% feature extraction using Recursive Feature Elimination
model = LogisticRegression(solver='lbfgs')
rfe = RFE(model, 10)
fit = rfe.fit(X, Y)
print("Num Features: %s" % (fit.n_features_))
print("Selected Features: %s" % (fit.support_))
print("Feature Ranking: %s" % (fit.ranking_))

compiler gives runtime error as follows:

runfile('C:/Users/drash/OneDrive/Desktop/Howto Health/preprocessing 1.py', wdir='C:/Users/drash/OneDrive/Desktop/Howto Health')
Traceback (most recent call last):
File "C:\Users\drash\OneDrive\Desktop\Howto Health\preprocessing 1.py", line 60, in <module>
    rfe = RFE(model, 10)

TypeError: __init__() takes 2 positional arguments but 3 were given

CodePudding user response:

RFE documentation and constructor information is here. Instead of writing RFE(model, 10), try RFE(model, n_features_to_select=10)

  • Related