Demo entry 6781277

imputation

   

Submitted by anonymous on Jan 02, 2019 at 17:17
Language: Python 3. Code size: 637 Bytes.

import pandas as pd
import numpy as np
import random
from fancyimpute import KNN

#%% import data
#URL used for debugging
#URL = 'C:/Users/NicoDreher/Desktop/myData'
df = pd.read_stata(Subset + '_' + 'pre_imputation.dta')

#%% 1NN imputation
df_1nn = pd.DataFrame(data = KNN(k=1).fit_transform(df), 
                      columns = df.columns, index = df.index)

df_1nn.to_stata(Subset + '_' + '1nn_imputation.dta')

#%% 5NN imputation
df_5nn = pd.DataFrame(data = KNN(k=5).fit_transform(df).round(), 
                      columns = df.columns, index = df.index)

df_5nn.to_stata(Subset + '_' + '5nn_imputation.dta')

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).