import numpy as np

f = open("AutodatenbankReduziert.csv", encoding="ISO-8859-1")
header = f.readline().rstrip('\n')  # skip the header
featureNames = header.split(',')
dataset = np.loadtxt(f, delimiter=",", skiprows=1, usecols=range(2,9))
f.close()

xmax = np.max(dataset,axis=0)
xmin = np.min(dataset,axis=0)
XHat = (dataset - xmin) / (xmax-xmin)
print(np.std(XHat,axis=0))
print(np.mean(XHat,axis=0))
print(np.median(XHat,axis=0))

xbar = np.mean(dataset,axis=0) 
sigma = np.std(dataset,axis=0)
XTilde = (dataset - xbar) / sigma

from CARTRegressionTree import bRegressionTree
MainSet = np.arange(0,dataset.shape[0])
TestSet = np.arange(0,35,4)
Trainingsset= np.delete(MainSet,TestSet)

reduTree = bRegressionTree()
reduData = dataset[:,np.array([1,2,4])] #*\label{merkmaleAuswaehlen:22}
reduTree.fit(reduData[Trainingsset,:],dataset[Trainingsset,0])
PreisApprox = reduTree.predict(reduData[TestSet,:])
errorAbs = np.abs(PreisApprox - dataset[TestSet,0])
print('Durchschnittlicher Fehler auf Testmenge:',np.mean(errorAbs))
PreisApprox = reduTree.predict(reduData[:,:])
errorAbs = np.abs(PreisApprox - dataset[:,0])
print('Durchschnittlicher Fehler auf allen Daten:',np.mean(errorAbs))

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(2,1,1)
ax.plot(dataset[:,0]-xbar[0], 'k' ,marker='o')  
ax.set_ylabel('Preis')
ax = fig.add_subplot(2,1,2)
ax.plot(dataset[:,1]-xbar[1], 'k' ,marker='x')  
ax.set_ylabel('kW')

fig = plt.figure()
ax = fig.add_subplot(2,1,1)
ax.plot(XTilde[:,0], 'k' ,marker='o')  
ax.set_ylabel('Preis (Standardisiert)')
ax = fig.add_subplot(2,1,2)
ax.plot(XTilde[:,1], 'k' ,marker='x')  
ax.set_ylabel('kW (Standardisiert)')

