# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in 

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# Any results you write to the current directory are saved as output.
from sklearn import model_selection, preprocessing, metrics
from sklearn.linear_model import LinearRegression
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
print(os.listdir("../input/ashrae-energy-prediction"))
train = pd.read_csv('../input/ashrae-energy-prediction/train.csv')
building_metadata = pd.read_csv('../input/ashrae-energy-prediction/building_metadata.csv')
weather_train = pd.read_csv('../input/ashrae-energy-prediction/weather_train.csv')
print(train.head())
np.sum(weather_train.air_temperature.values  == np.nan)
np.sum(weather_train.wind_speed.values  == np.nan)
np.sum(building_metadata.year_built.values  == np.nan)
train = train.merge(building_metadata, on='building_id', how='left')
train = train.merge(weather_train, on=['site_id', 'timestamp'], how='left')
print(train.head())
le = LabelEncoder()
le.fit(train['primary_use'].values)
train['primary_use'] = le.transform(train['primary_use'].values)
print(train.head())

y = np.log1p(train[['meter_reading']].values.reshape(-1,1))
np.save('y', y)
train = train[['square_feet', 'year_built', 'air_temperature', 'dew_temperature', 
               'sea_level_pressure', 'wind_direction', 'wind_speed', 'precip_depth_1_hr', 'floor_count']]
#train.to_csv('new_train.csv', index=False)

train = train.values
imp = SimpleImputer(missing_values=np.nan, strategy='mean')
imp.fit(train)
train = imp.transform(train)
clf = LinearRegression()
clf.fit(train, y)
test = pd.read_csv('../input/ashrae-energy-prediction/test.csv')
weather_test = pd.read_csv('../input/ashrae-energy-prediction/weather_test.csv')
sample_submission = pd.read_csv('../input/ashrae-energy-prediction/sample_submission.csv')
test = test.merge(building_metadata, on='building_id', how='left')
test = test.merge(weather_test, on=['site_id', 'timestamp'], how='left')
test['primary_use'] = le.transform(test['primary_use'].values)
primary_use = ohe_pu.transform(test['primary_use'].values.reshape(-1,1)).toarray()
test = test[['square_feet', 'year_built', 'air_temperature', 'dew_temperature', 'sea_level_pressure', 'wind_direction', 'wind_speed', 'precip_depth_1_hr', 'floor_count']]
test['square_feet'] = np.log1p(test['square_feet'].values)

#test.to_csv('new_test.csv', index=False)
test = test.values
test = np.hstack([test, primary_use])
test = imp.transform(test)
preds = np.expm1(clf.predict(test))
print(preds)
sample_submission['meter_reading'] = np.clip(preds, 0, None)
print(sample_submission.head())
sample_submission.to_csv('simple_linear_banchmar.csv', index=False)