diff --git a/q01_load_data/build.py b/q01_load_data/build.py index a29c139..1cf1938 100644 --- a/q01_load_data/build.py +++ b/q01_load_data/build.py @@ -1,7 +1,15 @@ +# %load q01_load_data/build.py import pandas as pd import numpy as np from sklearn.model_selection import train_test_split +path = 'data/elecdemand.csv' - +def q01_load_data(path): + data = pd.read_csv(path) + data['Datetime'] = pd.to_datetime(data['Datetime']) + shape=data.shape + return shape,data + + diff --git a/q01_load_data/tests/test_sol.pkl b/q01_load_data/tests/test_sol.pkl new file mode 100644 index 0000000..7912fb6 Binary files /dev/null and b/q01_load_data/tests/test_sol.pkl differ diff --git a/q01_load_data/tests/user_sol.pkl b/q01_load_data/tests/user_sol.pkl new file mode 100644 index 0000000..2ad49fc Binary files /dev/null and b/q01_load_data/tests/user_sol.pkl differ diff --git a/q02_data_splitter/build.py b/q02_data_splitter/build.py index b6c715f..9852d80 100644 --- a/q02_data_splitter/build.py +++ b/q02_data_splitter/build.py @@ -1,7 +1,14 @@ +# %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 +path = 'data/elecdemand.csv' +def q02_data_splitter(path): + np.random.seed(9) + shape,data = q01_load_data(path) + tscv = TimeSeriesSplit(n_splits=2) + split_data = list(tscv.split(data)) + return split_data - diff --git a/q02_data_splitter/tests/test_sol.pkl b/q02_data_splitter/tests/test_sol.pkl new file mode 100644 index 0000000..a3e9cc5 Binary files /dev/null and b/q02_data_splitter/tests/test_sol.pkl differ diff --git a/q02_data_splitter/tests/user_sol.pkl b/q02_data_splitter/tests/user_sol.pkl new file mode 100644 index 0000000..70c2333 Binary files /dev/null and b/q02_data_splitter/tests/user_sol.pkl differ diff --git a/q03_time_plot/build.py b/q03_time_plot/build.py index bf18743..a3e8047 100644 --- a/q03_time_plot/build.py +++ b/q03_time_plot/build.py @@ -1,7 +1,17 @@ +# %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') +path = 'data/elecdemand.csv' +def q03_time_plot(path): + shape,data = q01_load_data(path) + plt.figure(figsize=(16,7)) + plt.plot(data.Datetime,data.Demand) + plt.title('Electricity Demand in Australia for a year') + plt.xlabel('Time') + plt.ylabel('Demand') + plt.show() diff --git a/q04_boxplot/build.py b/q04_boxplot/build.py index c69f931..21aeab6 100644 --- a/q04_boxplot/build.py +++ b/q04_boxplot/build.py @@ -1,7 +1,22 @@ +# %load q04_boxplot/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') +path='data/elecdemand.csv' + +def q04_boxplot(path): + shape,data = q01_load_data(path) + data.head() + plt.figure(figsize=(16,7)) + sns.factorplot(x='WorkDay', y='Demand', data=data, kind='box', size=8, aspect=float(16/7)) + plt.xlabel('Workday') + plt.ylabel('Demand') + plt.title('Change in Electricity Demand wrt to Demand') + plt.show() + + + - diff --git a/q04_boxplot/tests/test_sol.pkl b/q04_boxplot/tests/test_sol.pkl new file mode 100644 index 0000000..f863f64 Binary files /dev/null and b/q04_boxplot/tests/test_sol.pkl differ diff --git a/q04_boxplot/tests/user_sol.pkl b/q04_boxplot/tests/user_sol.pkl new file mode 100644 index 0000000..44dda79 Binary files /dev/null and b/q04_boxplot/tests/user_sol.pkl differ diff --git a/q05_feature_engineering/build.py b/q05_feature_engineering/build.py index 97e29e7..62b6347 100644 --- a/q05_feature_engineering/build.py +++ b/q05_feature_engineering/build.py @@ -1,9 +1,19 @@ +# %load q05_feature_engineering/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') - +path = 'data/elecdemand.csv' +def q05_feature_engineering(path): + shape,data = q01_load_data(path) + corr = data['Temperature'].corr(data['Demand']) + plt.figure(figsize=(16,6)) + plt.scatter(data['Temperature'],data['Demand']) + plt.title('Temperature vs. Demand') + plt.xlabel('Temperature') + plt.ylabel('Demand') + plt.show() diff --git a/q05_feature_engineering/tests/test_sol.pkl b/q05_feature_engineering/tests/test_sol.pkl new file mode 100644 index 0000000..c8990f6 Binary files /dev/null and b/q05_feature_engineering/tests/test_sol.pkl differ diff --git a/q05_feature_engineering/tests/user_sol.pkl b/q05_feature_engineering/tests/user_sol.pkl new file mode 100644 index 0000000..9f2b9ec Binary files /dev/null and b/q05_feature_engineering/tests/user_sol.pkl differ diff --git a/q05_feature_engineering_part2/build.py b/q05_feature_engineering_part2/build.py index 53e6749..2ff0951 100644 --- a/q05_feature_engineering_part2/build.py +++ b/q05_feature_engineering_part2/build.py @@ -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 +path = 'data/elecdemand.csv' plt.switch_backend('agg') +def q05_feature_engineering_part2(path): + shape, data = q01_load_data(path) + data['hour'] = data['Datetime'].dt.hour + + hour = [] + for i in range(24): + hour_demand = data[data['hour'] == i]['Demand'].values + hour.append(hour_demand) + plt.figure(figsize=(16,6)) + plt.boxplot(hour,labels=[str(i) for i in range(24)]) + plt.xlabel('Hour') + plt.ylabel('Demand') + plt.title('Change in Electricity demand wrt to Hour') + plt.show() - diff --git a/q05_feature_engineering_part2/tests/test_sol.pkl b/q05_feature_engineering_part2/tests/test_sol.pkl new file mode 100644 index 0000000..2f666a1 Binary files /dev/null and b/q05_feature_engineering_part2/tests/test_sol.pkl differ diff --git a/q05_feature_engineering_part2/tests/user_sol.pkl b/q05_feature_engineering_part2/tests/user_sol.pkl new file mode 100644 index 0000000..9a90e98 Binary files /dev/null and b/q05_feature_engineering_part2/tests/user_sol.pkl differ diff --git a/q05_feature_engineering_part3/build.py b/q05_feature_engineering_part3/build.py index 7da14f7..f1f046d 100644 --- a/q05_feature_engineering_part3/build.py +++ b/q05_feature_engineering_part3/build.py @@ -1,8 +1,16 @@ +# %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') +path = 'data/elecdemand.csv' +def q05_feature_engineering_part3(path): + shape,data = q01_load_data(path) + data['Month'] = data['Datetime'].dt.strftime('%b') + sns.factorplot(x='Month',y='Demand',data=data,kind='box',size=8,aspect=(16/9)) + plt.title('Change in Demand of Electricity wrt Month') + plt.show() - diff --git a/q05_feature_engineering_part3/tests/test_sol.pkl b/q05_feature_engineering_part3/tests/test_sol.pkl new file mode 100644 index 0000000..017cf66 Binary files /dev/null and b/q05_feature_engineering_part3/tests/test_sol.pkl differ diff --git a/q05_feature_engineering_part3/tests/user_sol.pkl b/q05_feature_engineering_part3/tests/user_sol.pkl new file mode 100644 index 0000000..4d921c9 Binary files /dev/null and b/q05_feature_engineering_part3/tests/user_sol.pkl differ diff --git a/q05_feature_engineering_part4/build.py b/q05_feature_engineering_part4/build.py index 2731397..3c02c3c 100644 --- a/q05_feature_engineering_part4/build.py +++ b/q05_feature_engineering_part4/build.py @@ -1,9 +1,19 @@ +# %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 +import seaborn as sns from greyatomlib.time_series_day_02_project.q01_load_data.build import q01_load_data -plt.switch_backend('agg') +path = 'data/elecdemand.csv' -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 >= 6) & (x < 20)) else 0) + data['Peakmonths'] = data['month'].apply(lambda x: 1 if x in ['Feb', 'May', 'Jun', 'Jul', 'Aug'] else 0) + + return data + diff --git a/q05_feature_engineering_part4/tests/test_sol.pkl b/q05_feature_engineering_part4/tests/test_sol.pkl new file mode 100644 index 0000000..fca5817 Binary files /dev/null and b/q05_feature_engineering_part4/tests/test_sol.pkl differ diff --git a/q05_feature_engineering_part4/tests/user_sol.pkl b/q05_feature_engineering_part4/tests/user_sol.pkl new file mode 100644 index 0000000..49a7e7f Binary files /dev/null and b/q05_feature_engineering_part4/tests/user_sol.pkl differ diff --git a/q06_linear_regression/build.py b/q06_linear_regression/build.py index 8c11052..7b89231 100644 --- a/q06_linear_regression/build.py +++ b/q06_linear_regression/build.py @@ -1,3 +1,4 @@ +# %load q06_linear_regression/build.py import pandas as pd import numpy as np import math @@ -6,7 +7,26 @@ 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) + + + diff --git a/q06_linear_regression/tests/test_sol.pkl b/q06_linear_regression/tests/test_sol.pkl new file mode 100644 index 0000000..e0cbf28 Binary files /dev/null and b/q06_linear_regression/tests/test_sol.pkl differ diff --git a/q06_linear_regression/tests/user_sol.pkl b/q06_linear_regression/tests/user_sol.pkl new file mode 100644 index 0000000..6794af1 Binary files /dev/null and b/q06_linear_regression/tests/user_sol.pkl differ diff --git a/q07_randomforest_regressor/build.py b/q07_randomforest_regressor/build.py index 4cdb470..7d02947 100644 --- a/q07_randomforest_regressor/build.py +++ b/q07_randomforest_regressor/build.py @@ -1,3 +1,4 @@ +# %load q07_randomforest_regressor/build.py import pandas as pd import numpy as np import math @@ -6,7 +7,26 @@ 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'] +path = 'data/elecdemand.csv' +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) + '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 = 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) + + diff --git a/q08_gradientboosting_regressor/build.py b/q08_gradientboosting_regressor/build.py index e661aac..70b2a99 100644 --- a/q08_gradientboosting_regressor/build.py +++ b/q08_gradientboosting_regressor/build.py @@ -1,3 +1,4 @@ +# %load q08_gradientboosting_regressor/build.py import pandas as pd import numpy as np import math @@ -6,5 +7,23 @@ 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=9) + 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) + diff --git a/test_sol.pkl b/test_sol.pkl new file mode 100644 index 0000000..cec104b Binary files /dev/null and b/test_sol.pkl differ diff --git a/user_sol.pkl b/user_sol.pkl new file mode 100644 index 0000000..fb41a08 Binary files /dev/null and b/user_sol.pkl differ