Prediction of Temperature

Mohamed_Abokahf: <p>import pandas as pd<br>
from sklearn.model_selection import train_test_split<br>
from tqdm import tqdm<br>
from torch.utils.data import DataLoader, Dataset<br>
import torch<br>
import torch.nn as nn<br>
import torch.optim as optim<br>
import numpy as np # read the data which will be used for training, validation and testing of Recurrent neural network model<br>
df = pd.read_csv(‘/kaggle/input/temperature/train.csv’, header=None) print('The data consists of ', df.shape[0], 'rows and ', df.shape[1], ‘columns’) def Data_splitting(df):<br>
# i am going to split the data 80% training, 10% validation and 10% testing<br>
train_valid , test = train_test_split(df, test_size= 0.1, random_state=42, shuffle=True)</p>
<pre><code># split the train and valid data now to 80% and 10% 
train, valid = train_test_split(train_valid, test_size= 0.1, random_state=42, shuffle=True)

print('== Train has', round(len(train)/len(df) * 100, 2), '% of the data ==')
print('== Valid has', round(len(valid)/len(df) * 100, 2), '% of the data ==')
print('== Test has ', round(len(test)/len(df) * 100,2), '% of the data ==') 
return train, valid, test # get the indices for each temperature in both input and output
</code></pre>
<p>def get_indices(df):<br>
# get the input and the output indices<br>
cols = list(df.columns)<br>
input_width = 90<br>
label_width = 7<br>
step = input_width + label_width<br>
input_indices = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
label_indices = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
<span class="hashtag-raw">#print</span>(len(cols)-step)<br>
for i in range(0, len(cols), 1):<br>
if i + step &lt;= len(cols)-(step):<br>
in_la = slice(i, i + step)<br>
elif i + step == len(cols):<br>
in_la = slice(i, i + step)</p>
<pre><code>    inputs = [x for x in range(in_la.start, in_la.stop-7)]
    labels = [x for x in range(in_la.stop-7, in_la.stop)]
    input_indices.append(inputs)
    label_indices.append(labels)
    if i + step == len(cols):
        break
print(' == Now we have a list of indices for the input temperatures and the label temperatures == ')
return input_indices, label_indices def fill_df(x, y):
df_indices = pd.DataFrame(columns=['features indices', 'label indices'], index=[i for i in range(len(x))])
# filling the dataframe 
for i in range(len(df_indices)): 
    df_indices['features indices'][i] = x[i]
    df_indices['label indices'][i] = y[i]
print(" === The dataframe is filled with indices === ")
return df_indices def ready_data(df_indices, df):
for i in tqdm(range(len(df_indices))):
    lis_in = df_indices['features indices'][i]
    lis_la = df_indices['label indices'][i]
    for j in range(len(df)):
        tmp_in = [df[el][j] for el in lis_in]
        tmp_la = [df[el][j] for el in lis_la] + [0] * 83
        df_indices['features indices'][i] = tmp_in
        df_indices['label indices'][i] = tmp_la
print(" == The mapping has been finished == ")
return df_indices # implementation of the model 
</code></pre>
<p>class MyModel(nn.Module):<br>
def <strong>init</strong>(self, input_size, hidden_size, output_size, num_layers):<br>
super(MyModel, self).<strong>init</strong>()<br>
self.rnn = nn.RNN(input_size, hidden_size, num_layers, batch_first=True)<br>
self.fc = nn.Linear(hidden_size, output_size)</p>
<pre><code>def forward(self, x):
    out, _ = self.rnn(x)
    out = self.fc(out)
    return out class MyDataSet(Dataset):
def __init__(self, tensor):
    self.tensor = tensor
    
def __len__(self):
    return len(self.tensor)

def __getitem__(self, idx):
    return self.tensor[idx] # Convert the data to Numpy array 
</code></pre>
<p>def convert_to_array(df_indices):<br>
# define the features as Numpy array<br>
features = df_indices[‘features indices’].values<br>
labels = df_indices[‘label indices’].values<br>
sequences = zip(features, labels)<br>
sequences = list(sequences)<br>
sequences = torch.tensor(sequences)<br>
print(" === Converted to Numpy arrays === “)<br>
return sequences train, valid, test = Data_splitting(df) x_train, y_train = get_indices(train)<br>
x_valid, y_valid = get_indices(valid)<br>
x_test, y_test = get_indices(test) df_indices_train = fill_df(x_train, y_train)<br>
df_indices_valid = fill_df(x_valid, y_valid)<br>
df_indices_test = fill_df(x_test, y_test) res_train = ready_data(df_indices_train, df)<br>
res_valid = ready_data(df_indices_valid, df)<br>
res_test = ready_data(df_indices_test, df) resu_train = convert_to_array(res_train)<br>
resu_valid = convert_to_array(res_valid)<br>
resu_test  = convert_to_array(res_test) flattend_train = resu_train.view(-1)<br>
flattend_valid = resu_valid.view(-1)<br>
flattend_test = resu_test.view(-1) data_train = MyDataSet(flattend_train)<br>
data_valid = MyDataSet(flattend_valid)<br>
data_test= MyDataSet(flattend_test) # Configuration<br>
batch_size = 32<br>
train_loader = DataLoader(data_train, batch_size=batch_size, shuffle=True)<br>
valid_loader = DataLoader(data_valid, batch_size=batch_size)<br>
print(” === Data are loaded === “) # model hyperparameters defining<br>
input_size = 1<br>
hidden_size = 64<br>
output_size = 7<br>
num_layers = 1<br>
print(” === Hyperparameters are defined === “) # create an object from the model<br>
model = MyModel(input_size, hidden_size, output_size, num_layers)<br>
print(” === An instance of the model is created === ") # defining the loss function and optimizer</p>
<h1><a name="mean-squared-error-loss-function-1" class="anchor" href="#mean-squared-error-loss-function-1"></a>mean squared Error loss function</h1>
<p>criteria = nn.MSELoss()</p>
<h1><a name="adams-optimizer-with-learning-rate-of-0001-2" class="anchor" href="#adams-optimizer-with-learning-rate-of-0001-2"></a>Adam’s optimizer with learning rate of 0.001</h1>
<p>optimizer = optim.Adam(model.parameters(), lr=0.001)</p>
<p>print(" === The loss function and the optimizers are defined === ") # Training loop</p>
<p>num_epoch = 10<br>
for epoch in range(num_epoch):<br>
for batch in train_loader:<br>
inputs, targets = batch<br>
optimizer.zero_grad()<br>
outputs = model(inputs)<br>
loss = criteria(outputs, targets)<br>
loss.backward()<br>
optimizer.step()<br>
print(f’Epoch [{epoch + 1}/{num_epochs}], Loss: {loss.item():.4f}') ---------------------------------------------------------------------------<br>
ValueError                                Traceback (most recent call last)<br>
Cell In[291], line 6<br>
4 for epoch in range(num_epoch):<br>
5     for batch in train_loader:<br>
----&gt; 6         inputs, targets = batch<br>
7         optimizer.zero_grad()<br>
8         outputs = model(inputs)</p>
<p>ValueError: too many values to unpack (expected 2)</p>
<p><strong>Could any one tell me how to solve this problem. Thanks in advance</strong></p>

ptrblck: <p>You code is not properly formatted and thus hard to read, but based on the error message it seems <code>batch</code> does not contain two tensors and the unpacking thus fails. Print <code>batch</code> and make sure you can properly unpack it into two objects.</p>

Mohamed_Abokahf: <p>Hallo thanks a lot for the response can you please have a look at this notebook :</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://www.kaggle.com/code/mohamedabokahf/temperature-prediction">
  <header class="source">
      <img src="https://www.kaggle.com/static/images/favicon.ico" class="site-icon" width="32" height="32">

      <a href="https://www.kaggle.com/code/mohamedabokahf/temperature-prediction" target="_blank" rel="noopener nofollow ugc">kaggle.com</a>
  </header>

  <article class="onebox-body">
    <img width="100" height="100" src="https://storage.googleapis.com/kaggle-avatars/thumbnails/11492706-kg.jpg" class="thumbnail onebox-avatar">

<h3><a href="https://www.kaggle.com/code/mohamedabokahf/temperature-prediction" target="_blank" rel="noopener nofollow ugc">Temperature prediction</a></h3>

  <p>Explore and run machine learning code with Kaggle Notebooks | Using data from [Private Datasource]</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>it is working, but it is wrong. for each 90 temperatures I need the model to predict the next 7 temperatures, so I don’t know how to prepare the data before I feed the model with it. for example the input size I wrote now 90, but correct is 1 and the sequence length for the input is 90 and the output is temperature but I need 7 values so should it be 1 with seq length of 7 or what. Thanks a lot for your help</p>