import pandas as pd
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_percentage_error
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

def get_accuracy(target, prediction):
    error_percent = mean_absolute_percentage_error(target, prediction)
    accuracy = round((1-error_percent)*100,2)
    return accuracy

def decision_tree(max_leaf_nodes, train_features, test_features, train_targets, test_targets):
    model = DecisionTreeRegressor(max_leaf_nodes=max_leaf_nodes, random_state=0)
    model.fit(train_features, train_targets)
    predictions = model.predict(test_features)
    return get_accuracy(test_targets, predictions)

def random_forest(train_features, test_features, train_targets, test_targets):
    model = RandomForestRegressor()
    model.fit(train_features, train_targets)
    predictions = model.predict(test_features)
    return get_accuracy(test_targets, predictions)

def linear_regreassion(train_features, test_features, train_targets, test_targets):
    model = LinearRegression()
    model.fit(train_features, train_targets)
    predictions = model.predict(test_features)
    return get_accuracy(test_targets, predictions)

####### Melbourne Predicitons ########

melbourne_data = pd.read_csv('melb_data.csv')
melbourne_data = melbourne_data.dropna()
# for column_name in melbourne_data.columns:
#     print(column_name)
melbourne_features = ['Rooms', 'Bathroom', 'Landsize', 'BuildingArea', 'YearBuilt', 'Lattitude', 'Longtitude']

target = melbourne_data.Price
features = melbourne_data[melbourne_features]
train_features, test_features, train_targets, test_targets = train_test_split(features, target, random_state=3)

print('\n\tMelbourne House Price Guessing')
print('Decision Tree Accuracy')
for max_leaf_nodes in [5,10,50,100,1000, 5000]:
    melbourne_accuracy = decision_tree(max_leaf_nodes, train_features, test_features, train_targets, test_targets)
    print(f'  max leaf nodes: {max_leaf_nodes}\t{melbourne_accuracy}%')
melbourne_forest_accuracy = random_forest(train_features, test_features, train_targets, test_targets)
print(f'Forest accuracy: {melbourne_forest_accuracy}%')
melbourne_linear_accuracy = linear_regreassion(train_features, test_features, train_targets, test_targets)
print(f'Regression accuracy: {melbourne_linear_accuracy}%')



####### Iowa Predicitons ########

iowa_data = pd.read_csv('train.csv')
# for column_name in iowa_data.columns:
#     print(column_name)
# print(iowa_data.head())
iowa_features = ['LotArea','YearBuilt','1stFlrSF','2ndFlrSF','FullBath','BedroomAbvGr','TotRmsAbvGrd']

target = iowa_data.SalePrice
features = iowa_data[iowa_features]
train_features, test_features, train_targets, test_targets = train_test_split(features, target, random_state=3)

print('\n\tIowa House Price Guessing')
print('Decision Tree Accuracy')
for max_leaf_nodes in [5,10,50,100,1000, 5000]:
    iowa_accuracy = decision_tree(max_leaf_nodes, train_features, test_features, train_targets, test_targets)
    print(f'  max leaf nodes: {max_leaf_nodes}\t{iowa_accuracy}%')
iowa_forest_accuracy = random_forest(train_features, test_features, train_targets, test_targets)
print(f'Forest accuracy: {iowa_forest_accuracy}%')
iowa_linear_accuracy = linear_regreassion(train_features, test_features, train_targets, test_targets)
print(f'Regression accuracy: {iowa_linear_accuracy}%')