Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions q01_load_data/build.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# %load q01_load_data/build.py
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split

def q01_load_data(path):
tss=pd.read_csv(path)
tss['Datetime']=pd.to_datetime(tss['Datetime'])
return tss.shape, tss







Binary file added q01_load_data/tests/test_sol.pkl
Binary file not shown.
Binary file added q01_load_data/tests/user_sol.pkl
Binary file not shown.
14 changes: 13 additions & 1 deletion q02_data_splitter/build.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
# %load q02_data_splitter/build.py
import pandas as pd
import numpy as np
from sklearn.model_selection import TimeSeriesSplit
from greyatomlib.time_series_day_02_project.q01_load_data.build import q01_load_data

def q02_data_splitter(path):
seed=9
shape,df = q01_load_data(path)
tssf= TimeSeriesSplit(n_splits=3)
trainl=()
validl=()
for train_index,valid_index in tssf.split(df):
trainl=trainl+ tuple(train_index)
validl=validl + tuple(valid_index)
return [[trainl,trainl],[validl,validl]]




Binary file added q02_data_splitter/tests/test_sol.pkl
Binary file not shown.
Binary file added q02_data_splitter/tests/user_sol.pkl
Binary file not shown.
11 changes: 11 additions & 0 deletions q03_time_plot/build.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# %load q03_time_plot/build.py
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from greyatomlib.time_series_day_02_project.q01_load_data.build import q01_load_data
plt.switch_backend('agg')
def q03_time_plot(path):
shp,df=q01_load_data(path)
plt.figure(figsize=(16, 6))
plt.plot(df['Datetime'], df['Demand'])
plt.xlabel('Time')
plt.ylabel('Demand')
plt.title('SateTime vs Demand in Australia')
plt.show()



13 changes: 12 additions & 1 deletion q04_boxplot/build.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# %load q04_boxplot/build.py
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from greyatomlib.time_series_day_02_project.q01_load_data.build import q01_load_data
plt.switch_backend('agg')


def q04_boxplot(path):

shp,df=q01_load_data(path)
plt.figure(figsize=(16, 7))
sns.factorplot(x='WorkDay', y='Demand', data=df, kind='box', size=8, aspect=float(16/7))
plt.xlabel('Workday')
plt.ylabel('Demand')
plt.title('Each workday demand in Australia')


Binary file added q04_boxplot/tests/test_sol.pkl
Binary file not shown.
Binary file added q04_boxplot/tests/user_sol.pkl
Binary file not shown.
9 changes: 9 additions & 0 deletions q05_feature_engineering/build.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# %load q05_feature_engineering/build.py
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
Expand All @@ -6,4 +7,12 @@



def q05_feature_engineering(path):
tss,df=q01_load_data(path)
p_corf=np.corrcoef(df['Temperature'], df['Demand'])
plt.scatter(df['Temperature'], df['Demand'])
plt.show()

#path='data/elecdemand.csv'
#q05_feature_engineering(path)

Binary file added q05_feature_engineering/tests/test_sol.pkl
Binary file not shown.
Binary file added q05_feature_engineering/tests/user_sol.pkl
Binary file not shown.
19 changes: 17 additions & 2 deletions q05_feature_engineering_part2/build.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
# %load q05_feature_engineering_part2/build.py
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from greyatomlib.time_series_day_02_project.q01_load_data.build import q01_load_data
from greyatomlib.time_series_day_02_project.q01_load_data.build import q01_load_data
plt.switch_backend('agg')

def q05_feature_engineering_part2(path):
shape, data = q01_load_data(path)
'write your solution here'
data['hour'] = data['Datetime'].dt.hour

plt.figure(figsize=(16, 6))

hours = []
for i in range(24):
one = data[data['hour'] == i]['Demand'].values
hours.append(one)
plt.boxplot(hours, labels=[str(i) for i in range(24)])
plt.xlabel('Hour')
plt.ylabel('Demand')
plt.title('Change in Electricity demand wrt to Hour')


Binary file added q05_feature_engineering_part2/tests/test_sol.pkl
Binary file not shown.
Binary file added q05_feature_engineering_part2/tests/user_sol.pkl
Binary file not shown.
14 changes: 12 additions & 2 deletions q05_feature_engineering_part3/build.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
# %load q05_feature_engineering_part3/build.py
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from greyatomlib.time_series_day_02_project.q01_load_data.build import q01_load_data
plt.switch_backend('agg')


def q05_feature_engineering_part3(path):
tss,df=q01_load_data(path)
df.info()
df['hour']=df['Datetime'].dt.hour
df['month']=df['Datetime'].dt.strftime('%b')

sns.factorplot(x='month', y='Demand', data=df, kind='box', order=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug','Sep', 'Oct', 'Nov', 'Dec'], size=8, aspect=float(16/7))

#path='data/elecdemand.csv'
#q05_feature_engineering_part3(path)

Binary file added q05_feature_engineering_part3/tests/test_sol.pkl
Binary file not shown.
Binary file added q05_feature_engineering_part3/tests/user_sol.pkl
Binary file not shown.
17 changes: 13 additions & 4 deletions q05_feature_engineering_part4/build.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# %load q05_feature_engineering_part2/build.py
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from greyatomlib.time_series_day_02_project.q01_load_data.build import q01_load_data
plt.switch_backend('agg')

def q05_feature_engineering_part4():


def q05_feature_engineering_part4(path):
shape, data = q01_load_data(path)
data['hour'] = data['Datetime'].dt.hour
data['month'] = data['Datetime'].dt.strftime('%b')
data['Peakhours']=data['hour'].apply(lambda x : 1 if x in range(6,20) else 0)
data['Peakmonths']=data['month'].apply(lambda x : 1 if x in ['Feb', 'May', 'Jun', 'Jul', 'Aug'] else 0)
return data

#path='data/elecdemand.csv'
#q05_feature_engineering_part4(path)

Binary file added q05_feature_engineering_part4/tests/test_sol.pkl
Binary file not shown.
Binary file added q05_feature_engineering_part4/tests/user_sol.pkl
Binary file not shown.
26 changes: 24 additions & 2 deletions q06_linear_regression/build.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
# %load q06_linear_regression/build.py
import pandas as pd
import numpy as np
import math
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

from greyatomlib.time_series_day_02_project.q01_load_data.build import q01_load_data
from greyatomlib.time_series_day_02_project.q05_feature_engineering_part4.build import q05_feature_engineering_part4
from greyatomlib.time_series_day_02_project.q02_data_splitter.build import q02_data_splitter

fe = ["WorkDay", "Peakhours", "Peakmonths"]

fe = ['WorkDay', 'Peakhours', 'Peakmonths']

def q06_linear_regression(path,columns = fe, random_state =9):
np.random.seed(random_state)
data = q05_feature_engineering_part4(path)
splits = q02_data_splitter(path)
'write your solution here'

rmse = []
for i in splits:
train = i[0]
valid = i[1]
x_train, y_train = data[fe].values[train], data['Demand'].values[train]
x_valid, y_valid = data[fe].values[valid], data['Demand'].values[valid]
model = LinearRegression()
model.fit(x_train, y_train)
pred = model.predict(x_valid)
measure = math.pow(mean_squared_error(y_valid, pred), 0.5)
rmse.append(measure)
return np.mean(rmse)



Binary file added q06_linear_regression/tests/test_sol.pkl
Binary file not shown.
Binary file added q06_linear_regression/tests/user_sol.pkl
Binary file not shown.
24 changes: 23 additions & 1 deletion q07_randomforest_regressor/build.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# %load q07_randomforest_regressor/build.py
import pandas as pd
import numpy as np
import math
Expand All @@ -6,7 +7,28 @@
from greyatomlib.time_series_day_02_project.q05_feature_engineering_part4.build import q05_feature_engineering_part4
from greyatomlib.time_series_day_02_project.q02_data_splitter.build import q02_data_splitter

fe = ["WorkDay", "Peakhours", "Peakmonths"]
fe = ['WorkDay', 'Peakhours', 'Peakmonths']

def q07_randomforest_regressor(path,columns = fe, random_state =9):
np.random.seed(random_state)
data = q05_feature_engineering_part4(path)
splits = q02_data_splitter(path)
rmse = []

for i in splits:
train = i[0]
valid = i[1]
x_train, y_train = data[fe].values[train], data['Demand'].values[train]
x_valid, y_valid = data[fe].values[valid], data['Demand'].values[valid]
model = RandomForestRegressor( n_estimators=50, min_samples_leaf=30, random_state=10)
model.fit(x_train, y_train)
pred = model.predict(x_valid)
measure = math.pow(mean_squared_error(y_valid, pred), 0.5)
rmse.append(measure)
return np.mean(rmse)



#path= 'data/elecdemand.csv'
#q07_randomforest_regressor(path)

29 changes: 28 additions & 1 deletion q08_gradientboosting_regressor/build.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# %load q08_gradientboosting_regressor/build.py
import pandas as pd
import numpy as np
import math
Expand All @@ -6,5 +7,31 @@
from greyatomlib.time_series_day_02_project.q05_feature_engineering_part4.build import q05_feature_engineering_part4
from greyatomlib.time_series_day_02_project.q02_data_splitter.build import q02_data_splitter

fe = ["WorkDay", "Peakhours", "Peakmonths"]
fe = ['WorkDay', 'Peakhours', 'Peakmonths']


def q08_gradientboosting_regressor(path,columns = fe, random_state =9):
np.random.seed(random_state)
data = q05_feature_engineering_part4(path)
splits = q02_data_splitter(path)
rmse = []

for i in splits:
train = i[0]
valid = i[1]
x_train, y_train = data[fe].values[train], data['Demand'].values[train]
x_valid, y_valid = data[fe].values[valid], data['Demand'].values[valid]
model = GradientBoostingRegressor( n_estimators=200, min_samples_leaf=10, learning_rate=0.01, random_state=random_state)
model.fit(x_train, y_train)
pred = model.predict(x_valid)
measure = math.pow(mean_squared_error(y_valid, pred), 0.5)
rmse.append(measure)
return np.mean(rmse)



#path= 'data/elecdemand.csv'
#q08_gradientboosting_regressor(path)



Binary file added test_sol.pkl
Binary file not shown.
Binary file added user_sol.pkl
Binary file not shown.