Predicting Apparent Temperature using Linear regression

_Afra M P
2 min readJun 12, 2020

Apparent temperature is perceived temperature in Fahrenheit that can be computed with the help of wind speed and relative air humidity. I have used pytorch in order to implement this model.

Mortality rate increases when the increase in the temperature so it is necessary to predict the apparent temperature. We employ linear regression a machine learning algorithm in order to predict apparent temperature from the given humidity. Apparent temperature appeared to be the most important predictor of heat-related mortality for all-cause mortality. The apparent temperature is a reasonable variable for activating heat alerts and warning.

So let’s start with it…. By using the dataset from Szeged, Hungary area, between 2006 and 2016 a variety of weather variables are used in predicting apparent temperature and understanding the relationship between temperature and humidity. Firstly, import the torch and other related libraries then variables which are not required are excluded. The main variables required temperature, humidity and apparent temperture.

Step 1: Preparing dataset for training

Assign the input and target columns then convert them to numpy arrays and further to tensors. TensorDataset which allows access to rows from inputs and targets as tuples, and provides APIs for working with different datasets

It is required to find split the dataset into training and validation set after which we will be able to evaluate the outcome. With the help of DataLoader the training and validation set can be divided into batches and training set is shuffled. There is no need to shuffle validation set as it is used to evaluate our model.

Step 2: Creating a Model:

Now we write functions for training and validation by extending nn.Module class and Inside the __init__ constructor method we use nn.Linear() to instantiate weigths and biases predict the apparent , mse_loss is used to calculate the loss. We make predictions and calculate loss from these functions.

Step 3: Train the model to fit the data:

We use .backward method along with loss to find its gradient wrt weigths and biases and adjust the weights for good predictions and set all gradients to zero. The optimizer optim.SGD is used in manipulating the model’s weights & biases using gradients . SGD stands for Stochastic Gradient Descent.

The evaluate function is called which returns the loss followed by fit function.We should provide the number of epoches and learning rate in the function. Hence we obtain the outcome ….Hurray!!!

We’ve got the appropiate predictions of apparent temperature which is almost close to the ones in the dataset.

That’s all for now, I hope that this blog is really helping you and a special thanks to Jovian.ml team (mainly Aakash NS) and freecodecamp. If you are interested in learning more on pytorch and start a career in data science you can check out the following link:

https://www.youtube.com/watch?v=vo_fUOk-IKk&t=2327s

--

--