import numpy as np

def staticImputer(X, mode='mean', feature='all'):
    if feature=='all':
        feature = np.arange(X.shape[1])
    for i in feature:
        allData = np.arange(X.shape[0])
        index = np.flatnonzero(np.isnan(X[:,i]))
        correctData = np.delete(allData,index)
        if mode == 'most':
            unique, counts = np.unique(X[correctData,i], return_counts=True)
            whichOne = np.argmax(counts)
            newValue = unique[whichOne]
        elif mode == 'median': 
            newValue = np.median(X[correctData,i])
        else:
            newValue = np.mean(X[correctData,i])
        X[index,i] = newValue
        
    return(X)
        
if __name__ == '__main__':   
    np.random.seed(42)

    f = open("AutodatenbankAllEntries.csv")
    header = f.readline().rstrip('\n')  
    featureNames = header.split(',')
    fullDataset = np.loadtxt(f, delimiter=",", skiprows=4,usecols=range(2,9)) 
    f.close()
    index = np.array([338, 361, 389, 126, 145, 308, 325, 463,465,471,472,207,213,217,218,219,483, 36, 485, 489,498,40,251,512,41,9,591,67,75,597,93,606,17,97,19])
    index = index - 6 #*\label{code:imputer:0}
    X = np.copy(fullDataset)
    X[index,4] = np.NaN #*\label{code:imputer:1}
    
    XFilled = staticImputer(X, mode='median')
    
    allIndex = np.arange(X.shape[0])
    traingsSet = np.delete(allIndex,index)
    yTrain = X[traingsSet,4]
    xTrain = X[np.ix_(traingsSet,np.array([0,1,2,3,5,6]))]
    xTest  = X[np.ix_(index,np.array([0,1,2,3,5,6]))]
    
    from randomForest import randomForestRegression
    myForest = randomForestRegression(noOfTrees=50,minLeafNodeSize=7)
    myForest.fit(xTrain,yTrain)
    XFilled[index,4] = myForest.predict(xTest)
    error = np.mean(np.abs(XFilled[index,4] - fullDataset[index,4]))
    
    from fullMLP import MLPNet
    myPredict = MLPNet(hiddenlayer=(36,12))
    myPredict.fit(xTrain,yTrain,maxIter=1200, eta=0.1)
    xMLP = myPredict.predict(xTest)
    error = np.mean(np.abs(xMLP.reshape(35,) - fullDataset[index,4]))
    
    