-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWIDS 2023 (1) (4).py
1459 lines (765 loc) · 30.6 KB
/
WIDS 2023 (1) (4).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
# C
# # 1.1) Problem Statement
#
# Accurate long-term forecasts of temperature and precipitation are an essential tool for helping people and communities prepare for and adapt to extreme weather events.
#
# Currently, purely physics-based models dominate short-term weather forecasting. But these models have a limited forecast horizon. The availability of meteorological data offers an opportunity for data scientists to improve sub-seasonal forecasts by blending physics-based forecasts with machine learning.
#
# Sub-seasonal forecasts for weather and climate conditions (lead-times ranging from 15 to more than 45 days) would help communities and industries adapt to the challenges brought on by climate change.
#
# In this project,focus on longer-term weather forecasting to help communities adapt to extreme weather events caused by climate change by generating forecasts of temperature and precipitation for one year.
# # 1.2) Objectives
# ## a) Main objective
# The objective of this project is to develop a predictive model that will forecast sub-seasonal temperatures (temperatures over a two-week period) within the United States.
#
# ## b) Specific objectives
#
# 1. To create a machine learning model that will make the predictions.
#
# 2. The model should can provide an indication of how the average temperatures in the next 14 days
#
# 3.
#
# 4.
# # 1.3)Specifying the question
#
#
# Predict the arithmetic mean of the maximum and minimum temperature over the next 14 days, for each location and start date
# # 1.4) Defining the metric of success
#
# 1. Perform Exploratory Data Analysis on [Adapting to Climate Change Data](https://www.kaggle.com/competitions/widsdatathon2023/data).
#
#
# 2. Project will be considered successful if we train a machine learning model that will be able to predict Predict the arithmetic mean of the maximum and minimum temperature over the next 14 days, for each location
#
#
# 3. Use Root Mean Squared Error as the evaluation metric.
# # 1.5) Data Relevance and Validation
#
# The data available is relevant for the intended analysis . More information can be found here:
#
# [Kaggle.](https://www.kaggle.com/competitions/widsdatathon2023/overview)
#
#
# # 1.6) Understanding the Context
#
# The data set we are to work with contains the following columns:
#
# # 1.7) The Experimental Design Taken
#
# Below are the steps taken in this analysis
#
# 1. loading the required libraries
# 2. Loading and previewing data
# 3. Cleaning the data
# 4. Feature engineering
# 5. Univariate analysis
# 6. Bivariate analysis
# 7. Multivriate analysis
# 8. Implementing the solution by training a model that will give accurate predictions
# 9. Challenging the solution and giving insights on how improvements can be made.
# # 2) Reading the data
# In[6]:
# dataframe and plotting
import pandas as pd #used to analyze data
import numpy as np #used for working with arrays
import seaborn as sns #helps you explore and understand your data using stattistical graphs, it is built on matplotlib
import plotly as px
# In[7]:
# Loading train the data into a pandas dataframe
train = pd.read_csv(r'C:\Users\USER\Desktop\PROJECTS\train_data.csv')
# In[8]:
# Loading test the data into a pandas dataframe
test = pd.read_csv(r'C:\Users\USER\Desktop\MIRIAM\WIDS 2023\test_data.csv')
# In[9]:
ss = pd.read_csv(r'C:\Users\USER\Desktop\MIRIAM\WIDS 2023\sample_solution.csv')
# # 3) Understanding the data
# In[10]:
ss.head()
# In[11]:
train.head()
# In[12]:
train.tail()
# In[13]:
train.shape
# In[14]:
test.head()
# In[15]:
#Type column to be used to split train and test set from the combined dataframe
train['type'] = '0'
test['type'] = '1'
# setting the maximum number of columns that will be displayed
pd.set_option('display.max_columns', 246)
# Combine train and test set
combined_df = pd.concat((train, test)).reset_index(drop =True)
combined_df
# In[16]:
train.columns
# In[17]:
#Checking the number of entries in train and test sets:
print("The combined dataset contains {} rows, and {} columns".format(combined_df.shape[0], combined_df.shape[1]))
# In[18]:
combined_df.columns.tolist()
# In[19]:
# checking for missing values
pd.set_option('display.max_rows', 247)
train.isnull().sum()
# In[20]:
# checking for missing values
pd.set_option('display.max_rows', 247)
test.isnull().sum()
# In[21]:
# checking for missing values
pd.set_option('display.max_rows', 247)
combined_df.isnull().sum()
# In[22]:
#check for duplicate rows
sum(combined_df.duplicated())
# In[23]:
#Checking the number of records in our train dataset
combined_df.count()
# In[24]:
combined_df.dtypes
# In[25]:
#Checking for the value counts for regions column
combined_df['climateregions__climateregion'].value_counts()
# # 2) Combining Features
# In[26]:
# getting the mean of 'wind-vwnd-925-2010-1',
combined_df['avg_wind-vwnd-925-2010'] = combined_df.iloc[:,225:245].mean(axis=1)
combined_df = combined_df.drop(combined_df.iloc[:,225:246], axis = 1)
combined_df.head()
# In[27]:
# getting the mean of 'wind-hgt-100-2010-1',
combined_df['avg_wind-hgt-100-2010'] = combined_df.iloc[:,216:225].mean(axis=1)
combined_df = combined_df.drop(combined_df.iloc[:,216:225], axis = 1)
combined_df.head()
# In[28]:
# getting the mean of 'wind-hgt-10',
combined_df['avg_wind-hgt-10-2010'] = combined_df.iloc[:,206:215].mean(axis=1)
combined_df = combined_df.drop(combined_df.iloc[:,206:216], axis = 1)
combined_df.head()
# In[29]:
# getting the mean of 'wind-uwnd-925-2010',
combined_df['avg_wind-uwnd-925-2010'] = combined_df.iloc[:,186:205].mean(axis=1)
combined_df = combined_df.drop(combined_df.iloc[:,186:206], axis = 1)
combined_df.head()
# In[30]:
# getting the mean of 'icec-2010',
combined_df['avg_icec-2010'] = combined_df.iloc[:,176:185].mean(axis=1)
combined_df = combined_df.drop(combined_df.iloc[:,176:186], axis = 1)
combined_df.head()
# In[31]:
# getting the mean of wind-hgt-500-2010,
combined_df['avg_wind-hgt-500-2010'] = combined_df.iloc[:,166:175].mean(axis=1)
combined_df =combined_df.drop(combined_df.iloc[:,166:176], axis = 1)
combined_df.head()
# In[32]:
# getting the mean of sst-2010,
combined_df['avg_sst-2010'] = combined_df.iloc[:,156:165].mean(axis=1)
combined_df = combined_df.drop(combined_df.iloc[:,156:166], axis = 1)
combined_df.head()
# In[33]:
# getting the mean of wind-hgt-850,
combined_df['avg_wind-hgt-850'] = combined_df.iloc[:,146:155].mean(axis=1)
combined_df = combined_df.drop(combined_df.iloc[:,146:156], axis = 1)
combined_df.head()
# In[34]:
# getting the mean of hgt wind-uwnd-250,
combined_df['avg_wind-uwnd-250'] = combined_df.iloc[:,121:140].mean(axis=1)
combined_df = combined_df.drop(combined_df.iloc[:,121:141], axis = 1)
combined_df.head()
# In[35]:
# getting the mean of wind-vwnd-250-2010,
combined_df['avg_wind-vwnd-250-2010'] = combined_df.iloc[:,101:120].mean(axis=1)
combined_df = combined_df.drop(combined_df.iloc[:,101:121], axis = 1)
combined_df.head()
# In[36]:
combined_df.shape
# In[37]:
pd.set_option('display.max_rows', 247)
combined_df.columns.tolist()
# # 3) Feature Engineering
# In[38]:
# Convert the 'startdate' column to datetime format
combined_df['startdate'] = pd.to_datetime(combined_df['startdate'])
# In[40]:
# Convert the 'startdate' column to datetime format
train['startdate'] = pd.to_datetime(train['startdate'])
# In[41]:
# Convert the 'startdate' column to datetime format
test['startdate'] = pd.to_datetime(test['startdate'])
# In[42]:
print('Starting date of the whole data: ', combined_df.startdate.min())
print('Ending date of the whole data: ', combined_df.startdate.max())
print('Total number of days in available in whole data: ', combined_df.startdate.nunique())
# In[43]:
print('Starting date of the data: ', train.startdate.min())
print('Ending date of the data: ', train.startdate.max())
print('Total number of days in available train data: ', train.startdate.nunique())
# In[44]:
print('Starting date of the data: ', test.startdate.min())
print('Ending date of the data: ', test.startdate.max())
print('Total number of days in available train data: ', test.startdate.nunique())
# In[ ]:
# combined_df['two_week_interval'] = combined_df.groupby(pd.Grouper(freq='2W')).ngroup()
# In[45]:
# Extract year, month and day as additional features
# Extract year, month and day as additional features
combined_df['year'] = pd.DatetimeIndex(combined_df['startdate']).year
combined_df['month'] = pd.DatetimeIndex(combined_df['startdate']).month
combined_df['day_of_month'] = pd.DatetimeIndex(combined_df['startdate']).day
combined_df['day_of_week'] = pd.DatetimeIndex(combined_df['startdate']).dayofweek
combined_df['day_of_year'] = pd.DatetimeIndex(combined_df['startdate']).dayofyear
combined_df['week_of_year'] = pd.DatetimeIndex(combined_df['startdate']).weekofyear
# In[46]:
combined_df.drop(['startdate'], inplace = True, axis = 1)
# In[47]:
combined_df.drop(['climateregions__climateregion'], inplace = True, axis = 1)
# In[49]:
combined_df.columns.tolist()
# In[185]:
new_df = combined_df[['index','lat','lon','contest-pevpr-sfc-gauss-14d__pevpr','nmme0-tmp2m-34w__nmme0mean',
'contest-wind-h10-14d__wind-hgt-10','nmme-tmp2m-56w__nmmemean','contest-rhum-sig995-14d__rhum',
'nmme-prate-34w__nmmemean','contest-wind-h100-14d__wind-hgt-100','nmme0-prate-56w__nmme0mean',
'nmme0-prate-34w__nmme0mean','contest-tmp2m-14d__tmp2m','contest-slp-14d__slp',
'contest-wind-vwnd-925-14d__wind-vwnd-925','nmme-prate-56w__nmmemean',
'contest-pres-sfc-gauss-14d__pres','contest-wind-uwnd-250-14d__wind-uwnd-250','nmme-tmp2m-34w__nmmemean',
'contest-prwtr-eatm-14d__prwtr','contest-wind-vwnd-250-14d__wind-vwnd-250','contest-precip-14d__precip',
'contest-wind-h850-14d__wind-hgt-850','contest-wind-uwnd-925-14d__wind-uwnd-925',
'contest-wind-h500-14d__wind-hgt-500','nmme0mean','elevation__elevation', 'mjo1d__phase',
'mjo1d__amplitude','mei__mei','mei__meirank','mei__nip','type','avg_wind-vwnd-925-2010',
'avg_wind-hgt-100-2010','avg_wind-hgt-10-2010','avg_wind-uwnd-925-2010','avg_icec-2010',
'avg_wind-hgt-500-2010', 'avg_sst-2010','avg_wind-hgt-850','avg_wind-uwnd-250','avg_wind-vwnd-250-2010',
'year','month','day_of_month','day_of_week','day_of_year','week_of_year']]
# In[186]:
new_df.shape
# In[132]:
# get_ipython().system('pip install sweetviz')
# In[134]:
import sweetviz as sv
# Create a sweetviz report for the dataset
report = sv.analyze(new_df)
# Display the report
report.show_html()
# In[188]:
# Separate train and test data from the combined dataframe
train_df = new_df[new_df['type'] == '0']
test_df = new_df[new_df['type'] == '1']
# Check the shapes of the split dataset
train_df.shape, test_df.shape
# In[189]:
#drop type column from train as it doesnt have any use now
train_df.drop(['type'], axis = 1, inplace = True)
train_df.info()
# In[190]:
#drop target and type column from test
test_df.drop(['type'], axis = 1, inplace= True)
test_df.info()
# In[191]:
#drop target and type column from test
test_df.drop(['contest-tmp2m-14d__tmp2m'], axis = 1, inplace= True)
# # 5)Implementing the Solution with all Identified Features
# In[193]:
# dropping rows with missing values
# Drop rows with missing values
train_df = train_df.dropna()
# In[194]:
# assigning our independent variables
features = train_df.drop(['contest-tmp2m-14d__tmp2m','index'], axis = 1)
target = train_df['contest-tmp2m-14d__tmp2m']
# In[199]:
# This step entails dividing the datasets into training and test sets
# We start by importing the neccessary libray for the same
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=42)
# In[200]:
# lets print the shapes again
print("Shape of the x Train :", X_train.shape)
print("Shape of the y Train :", y_train.shape)
print("Shape of the x Test :", X_test.shape)
print("Shape of the y Test :", y_test.shape)
# In[201]:
import lightgbm as lgb
# # storing training and validation data in LightGBM Datasets.
# train_data = lgb.Dataset(X_train, label = y_train)
# valid_data = lgb.Dataset(X_valid, label = y_valid)
#create a model using lgbm
model = LGBMRegressor()
model.fit(X_train, y_train,
eval_set=[(X_test, y_test)],
eval_metric='l2',
early_stopping_rounds=100)
# In[202]:
# make predictions
y_train_pred = model.predict(X_train, num_iteration=model.best_iteration_)
y_test_pred = model.predict(X_test, num_iteration=model.best_iteration_)
import lightgbm as lgb
from sklearn.metrics import mean_squared_error
# calculate the rmse
rmse = np.sqrt(mean_squared_error(y_train, y_train_pred))
print("RMSE:", rmse)
# In[175]:
# # Create the DMatrix from the training data
# dtrain = xgb.DMatrix(X_train, label=y_train)
# # Define the XGBoost model
# model = xgb.XGBRegressor()
# # Perform k-fold cross validation with 3 folds
# kfold = xgb.cv(model.get_params(), dtrain, num_boost_round=10, nfold=3, metrics="rmse", as_pandas=True, seed=42)
# # Print the cross-validation results
# print(kfold)
# In[176]:
# import xgboost as xgb
# from sklearn.metrics import mean_squared_error
# # # Assign your input and target data
# # X_train = your_input_train_data
# # y_train = your_target_train_data
# # X_test = your_input_test_data
# # y_test = your_target_test_data
# # Create the DMatrix from the training data
# dtrain = xgb.DMatrix(X_train, label=y_train)
# # Perform k-fold cross validation with 3 folds
# # kfold = xgb.cv(model.get_params(), dtrain, num_boost_round=10, nfold=3, metrics="rmse", as_pandas=True, seed=42)
# # Define the XGBoost model
# model = xgb.XGBRegressor()
# # Fit the model on the training data
# model.fit(X_train, y_train)
# # Make predictions on the test set
# y_pred = model.predict(X_test)
# # # Evaluate the model's accuracy on the test set
# mse = mean_squared_error(y_test, y_pred)
# print("Mean Squared Error: %.2f" % mse)
# rmse = np.sqrt(mse)
# print("Root Mean Squared Error: %.2f" % rmse)
# In[177]:
# # # Evaluate the model's accuracy on the test set
# mse = mean_squared_error(y_test, y_pred)
# print("Mean Squared Error: %.2f" % mse)
# rmse = np.sqrt(mse)
# print("Root Mean Squared Error: %.2f" % rmse)
# In[178]:
# rmse = np.sqrt(mse)
# print("Root Mean Squared Error: %.2f" % rmse)
# In[203]:
# Training base model
# Import XGBoost Regressor
import xgboost as xgb
from xgboost import XGBRegressor
#Create a XGBoost Regressor
reg = XGBRegressor(n_estimators = 1000)
# Train the model using the training sets
reg.fit(X_train, y_train,
eval_set = [(X_train, y_train), (X_test, y_test)],
verbose = 100)
# In[182]:
# Model prediction on train data
y_pred = reg.predict(X_test)
# In[205]:
pd.DataFrame(data = reg.feature_importances_,
index = reg.feature_names_in,
columns = ['importance'])
# In[ ]:
# In[155]:
# Model Evaluation
from sklearn import metrics
print('R^2:',metrics.r2_score(y_train, y_pred))
print('Adjusted R^2:',1 - (1-metrics.r2_score(y_train, y_pred))*(len(y_train)-1)/(len(y_train)-X_train.shape[1]-1))
print('MAE:',metrics.mean_absolute_error(y_train, y_pred))
print('MSE:',metrics.mean_squared_error(y_train, y_pred))
print('RMSE:',np.sqrt(metrics.mean_squared_error(y_train, y_pred)))
# # 3) Removing Redundancy
# In[97]:
# a function to get all columns of object type
obj_list = new_df.select_dtypes(include = "object").columns
print (obj_list)
# In[71]:
#Label Encoding for object to numeric conversion
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
for feat in obj_list:
new_df[feat] = le.fit_transform(new_df[feat])
print (new_df.info())
# In[98]:
# we begin by drawing a correlation matrix using the .corr
# wedevelop a mask since the correlation matrix will repeat itselt along tha main diagonal
# and duplicate itself. So, we will slice it to remove the duplicate data
# first we create an array filled with ones, assign upper side 1 and lower side 0
# and a one will be applied where 1 is set
# generate a mask for the upper triangle
corr = new_df.corr()
mask = np.triu(np.ones_like(corr, dtype = bool))
# In[99]:
# set up matplotlib figure
import plotly as plt
f, ax = plt.subplots(figsize =(11,9))
# generate a custom diverging colormap to show different color tones
cmap = sns.diverging_palette(230,20, as_cmap = True)
# draw the heatmap with mask and correct aspect ratio
sns.heatmap(corr, mask = mask, cmap= cmap, vmax=0.3,
square = True, linewidth = .5, cbar_kws = {'shrink' : .5})
# In[100]:
# get correlations, it will get diagonaland lower triangular pairs of correlation matrix
def get_redundant_pairs(new_df):
pairs_to_drop = set()
cols = new_df.columns
for i in range(0, new_df.shape[1]):
for k in range(0, i+1):
pairs_to_drop.add((cols[i], cols[k]))
return pairs_to_drop
# In[101]:
# sort correlations in decsending order
def get_top_correlations(new_df, n =5):
au_corr = new_df.corr().abs().unstack()
labels_to_drop = get_redundant_pairs(new_df)
au_corr = au_corr.drop(labels = labels_to_drop).sort_values(ascending = False)
return au_corr[0:n]
# In[103]:
print('Top Absolute Correlations')
print(get_top_correlations(new_df, 50))
# In[116]:
from statsmodels.stats.outliers_influence import variance_inflation_factor
features = new_df.columns
def compute_vif(features):
vif = pd.DataFrame()
vif["features"] = new_df.columns
vif["vif_factor"] = [variance_inflation_factor(train_df[features].values, i) for i in range(len(features))]
return vif.sort_values(by =['vif_factor'])
# In[117]:
vif_data = compute_vif(features)
vif_data
# A VIF score of 1 indicates no collinearity, while a score greater than 1 indicates that the predictor is correlated with one or more other predictors in the model. A VIF score of 5 or greater is generally considered to indicate high collinearity, and suggests that the predictor may not be providing any additional information to the model above and beyond what is already provided by the other correlated predictors. In general, a lower VIF score is better.
#
# We are going to drop features with a vif of > 5
# In[125]:
def vif4(vif_factor):
vifd = pd.DataFrame()
vifd = vif_data.loc[vif_data['vif_factor'] <= 4.0]
return vifd
result = vif4('vif_factor')
result
# # fitting the model after removing multicolinearity
# In[135]:
# assigning our independent variables
X = train_df[['avg_wind-vwnd-925-2010','day_of_week','contest-precip-14d__precip','contest-wind-vwnd-925-14d__wind-vwnd-925',
'contest-wind-uwnd-925-14d__wind-uwnd-925','avg_wind-vwnd-250-2010','contest-wind-vwnd-250-14d__wind-vwnd-250']]
Y = train_df['contest-tmp2m-14d__tmp2m']
# In[148]:
# This step entails dividing the datasets into training and test sets
# We start by importing the neccessary libray for the same
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=42)
# In[149]:
# lets print the shapes again
print("Shape of the x Train :", X_train.shape)
print("Shape of the y Train :", y_train.shape)
print("Shape of the x Test :", X_test.shape)
print("Shape of the y Test :", y_test.shape)
# In[137]:
# data normalization with sklearn
from sklearn.preprocessing import MinMaxScaler
# fit scaler on training data
norm = MinMaxScaler().fit(X_train)
# transform training data
X_train_norm = norm.transform(X_train)
# transform testing data
X_test_norm = norm.transform(X_test)
# In[138]:
# Training base model
# Import XGBoost Regressor
import xgboost as xgb
from xgboost import XGBRegressor
#Create a XGBoost Regressor
reg = XGBRegressor()
# Train the model using the training sets
reg.fit(X_train_norm, y_train)
# In[142]:
# Model prediction on train data
y_pred = reg.predict(X_test_norm)
# In[144]:
# Model Evaluation
from sklearn import metrics
print('R^2:',metrics.r2_score(y_train, y_pred))
print('Adjusted R^2:',1 - (1-metrics.r2_score(y_train, y_pred))*(len(y_train)-1)/(len(y_train)-X_test_norm.shape[1]-1))
print('MAE:',metrics.mean_absolute_error(y_train, y_pred))
print('MSE:',metrics.mean_squared_error(y_train, y_pred))
print('RMSE:',np.sqrt(metrics.mean_squared_error(y_train, y_pred)))
# In[ ]:
# In[ ]:
# In[107]:
new_df[['avg_icec-2010', 'type']].info()
# In[ ]:
from sklearn.decomposition import PCA
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# getting summary statisctics for both categorical and numerical columns
pd.set_option('display.max_rows', 55)
combined_df.describe(include = 'all')
# In[ ]:
# !pip install sweetviz
# In[ ]:
# import sweetviz as sv
# # Create a sweetviz report for the dataset
# report = sv.analyze(train)
# # Display the report
# report.show_html()
# In[ ]:
new_df.columns.tolist()
# In[ ]:
new_df.shape
# In[ ]:
# df_copy.drop('startdate', inplace = True, axis = 1)
# In[ ]:
new_df.dtypes
# In[ ]:
# filling missing values in age column
new_df.fillna(method='ffill',inplace=True)
pd.set_option('display.max_rows', 246)
new_df.isnull().sum()
# In[ ]:
# Separate train and test data from the combined dataframe
train_df = new_df[new_df['type'] == 0]