import sys !pip install numpy !pip install pandas !pip install scikit-learn
The first step is to download out train test data. We will be training a model on the train data and make predictions on test data. We submit our predictions
#Donwload the datasets !rm -rf data !mkdir data !wget https://s3.eu-central-1.wasabisys.com/aicrowd-practice-challenges/public/power/v0.1/train.csv !wget https://s3.eu-central-1.wasabisys.com/aicrowd-practice-challenges/public/power/v0.1/test.csv !mv train.csv data/train.csv !mv test.csv data/test.csv
import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.neural_network import MLPClassifier from sklearn.linear_model import LinearRegression from sklearn.svm import SVC from sklearn.metrics import mean_absolute_error,mean_squared_error
all_data_path = "data/train.csv" #path where data is stored
all_data = pd.read_csv(all_data_path) #load data in dataframe using pandas
all_data = all_data.drop(['Date'],axis=1) all_data = all_data.drop(['Time'],axis=1)
We can see the dataset contains 9 columns,where columns 1 and 2 are not of use for the regression, so we drop them and the columns 3-8 denotes the different type of information about the power consumed and the last column tells the reading of Sub_metering_3.
Split Data into Train and Validation 🔪¶
- The next step is to think of a way to test how well our model is performing. we cannot use the test data given as it does not contain the data labels for us to verify.
- The workaround this is to split the given training data into training and validation. Typically validation sets give us an idea of how our model will perform on unforeseen data. it is like holding back a chunk of data while training our model and then using it to for the purpose of testing. it is a standard way to fine-tune hyperparameters in a model.
- There are multiple ways to split a dataset into validation and training sets. following are two popular ways to go about it, k-fold, leave one out. 🧐
- Validation sets are also used to avoid your model from overfitting on the train dataset.
X_train, X_val= train_test_split(all_data, test_size=0.2, random_state=42)
- We have decided to split the data with 20 % as validation and 80 % as training.
- To learn more about the train_test_split function click here. 🧐
- This is of course the simplest way to validate your model by simply taking a random chunk of the train set and setting it aside solely for the purpose of testing our train model on unseen data. as mentioned in the previous block, you can experiment 🔬 with and choose more sophisticated techniques and make your model better.
- Now, since we have our data splitted into train and validation sets, we need to get the corresponding labels separated from the data.
- with this step we are all set move to the next step with a prepared dataset.
#change the column number according the column which contains the label
X_train,y_train = X_train.iloc[:,:-1],X_train.iloc[:,-1] X_val,y_val = X_val.iloc[:,:-1],X_val.iloc[:,-1]
Define the Model¶
We have fixed our data and now we are ready to train our model.
Remember that there are no hard-laid rules here. you can mix and match regressors, it is advisable to read up on the numerous techniques and choose the best fit for your solution , experimentation is the key.
A good model does not depend solely on the classifier but also on the features you choose. So make sure to analyse and understand your data well and move forward with a clear view of the problem at hand. you can gain important insight from here.🧐
regressor = LinearRegression()
- To start you off, We have used a basic Linear Regression here.
- But you can tune parameters and increase the performance. To see the list of parameters visit here.
- Do keep in mind there exist sophisticated techniques for everything, the key as quoted earlier is to search them and experiment to fit your implementation.
Got a warning! Dont worry, its just beacuse the number of iteration is very less(defined in the classifier in the above cell).Increase the number of iterations and see if the warning vanishes and also see how the performance changes.Do remember increasing iterations also increases the running time.( Hint: max_iter=500)
Predict on Validation¶
Now we predict our trained classifier on the validation set and evaluate our model
y_pred = regressor.predict(X_val)
Evaluate the Performance¶
- We have used basic metrics to quantify the performance of our model.
- This is a crucial step, you should reason out the metrics and take hints to improve aspects of your model.
- Do read up on the meaning and use of different metrics. there exist more metrics and measures, you should learn to use them correctly with respect to the solution,dataset and other factors.
- Mean Absolute Error and Root Mean Square Error are the metrics for this challenge
mse = mean_squared_error(y_val,y_pred) mae = mean_absolute_error(y_val,y_pred)
print("MSE of the model is :" ,mse) print("MAE of the model is :" ,mae)
Testing Phase 😅¶
We are almost done. We trained and validated on the training data. Now its the time to predict on test set and make a submission.
final_test_path = "data/test.csv" final_test = pd.read_csv(final_test_path)
final_test = final_test.drop(['Date'],axis=1) final_test = final_test.drop(['Time'],axis=1)
Predict Test Set¶
Time for the moment of truth! Predict on test set and time to make the submission.
submission = regressor.predict(final_test)
#change the header according to the submission guidelines
submission = pd.DataFrame(submission) submission.to_csv('submission.csv',header=['sub_metering_3'],index=False)
🚧 Note :
- Do take a look at the submission format.
- The submission file should contain a header.
- Follow all submission guidelines strictly to avoid inconvenience.
try: from google.colab import files files.download('submission.csv') except: print('only in colab')