0% found this document useful (0 votes)
9 views8 pages

Assignment-Deep Learning

Uploaded by

Shashank Rah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Assignment-Deep Learning

Uploaded by

Shashank Rah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

01/11/2024, 15:41 Untitled11.

ipynb - Colab

!pip install meteostat pandas

Collecting meteostat
Downloading meteostat-1.6.8-py3-none-any.whl.metadata (4.6 kB)
Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packag
Requirement already satisfied: pytz in /usr/local/lib/python3.10/dist-packages
Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-package
Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python
Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/dis
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-pack
Downloading meteostat-1.6.8-py3-none-any.whl (31 kB)
Installing collected packages: meteostat
Successfully installed meteostat-1.6.8

from datetime import datetime, timedelta


import pandas as pd
from meteostat import Point, Daily

mesra = Point(23.4294, 85.4124, 350)

today = datetime.now()

start_date = today - timedelta(days=60)

end_date = today

data = Daily(mesra, start_date, end_date)


data = data.fetch()

if 'tmean' in data.columns:

temperatures = data[['tmean']]
else:
print("Warning: 'tmean' column not found in the data. Using 'tavg' instead.")
temperatures = data[['tavg']]

Warning: 'tmean' column not found in the data. Using 'tavg' instead.

temperatures.to_csv('temp.csv', index=True)

https://colab.research.google.com/drive/1h5vXmoqDxXdNN4I_kivnFAvEVWuzxJCY#scrollTo=JMwgEWLyK6F7&printMode=true 1/8
01/11/2024, 15:41 Untitled11.ipynb - Colab
import pandas as pd

temp_df = pd.read_csv('temp.csv', index_col=0)

temp_df

Show hidden output

Next
steps:
Generate code
with
temp_df
toggle_off View recommended
plots
New interactive
sheet

import pandas as pd
from sklearn.preprocessing import MinMaxScaler

temp_df = pd.read_csv('temp.csv', index_col=0)

temp_col_name = temp_df.columns[0]

scaler = MinMaxScaler()

temp_values = temp_df[temp_col_name].values.reshape(-1, 1)

normalized_temp = scaler.fit_transform(temp_values)

temp_df['Normalized_Temperature'] = normalized_temp

temp_df

https://colab.research.google.com/drive/1h5vXmoqDxXdNN4I_kivnFAvEVWuzxJCY#scrollTo=JMwgEWLyK6F7&printMode=true 2/8
01/11/2024, 15:41 Untitled11.ipynb - Colab

tavg Normalized_Temperature

time

2024-09-03 27.9 0.815385

2024-09-04 28.2 0.861538

2024-09-05 27.9 0.815385

2024-09-06 28.1 0.846154

2024-09-07 29.1 1.000000

2024-09-08 28.6 0.923077

2024-09-09 27.2 0.707692

2024-09-10 27.0 0.676923

2024-09-11 27.8 0.800000

2024-09-12 28.3 0.876923

2024-09-13 27.8 0.800000

2024-09-14 25.3 0.415385

2024-09-15 25.0 0.369231

2024-09-16 24.7 0.323077

2024-09-17 25.5 0.446154

2024-09-18 26.4 0.584615

2024-09-19 28.1 0.846154

2024-09-20 28.7 0.938462

2024-09-21 29.0 0.984615

2024-09-22 28.7 0.938462

2024-09-23 28.1 0.846154

2024-09-24 27.1 0.692308

2024-09-25 25.2 0.400000

2024-09-26 26.2 0.553846

2024-09-27 26.1 0.538462

2024-09-28 28.0 0.830769

2024-09-29 28.2 0.861538

2024-09-30 28.5 0.907692

2024-10-01 28.5 0.907692

2024-10-02 27.8 0.800000

2024-10-03 27.8 0.800000

https://colab.research.google.com/drive/1h5vXmoqDxXdNN4I_kivnFAvEVWuzxJCY#scrollTo=JMwgEWLyK6F7&printMode=true 3/8
01/11/2024, 15:41 Untitled11.ipynb - Colab
2024-10-04 27.7 0.784615

2024-10-05 27.6 0.769231

2024-10-06 26.7 0.630769

2024-10-07 27.1 0.692308

2024-10-08 26.5 0.600000

2024-10-09 26.4 0.584615

2024-10-10 26.3 0.569231

2024-10-11 26.9 0.661538

2024-10-12 26.9 0.661538

2024-10-13 26.4 0.584615

2024-10-14 25.6 0.461538

2024-10-15 25.3 0.415385

2024-10-16 25.8 0.492308

2024-10-17 26.1 0.538462

2024-10-18 26.0 0.523077

2024-10-19 26.4 0.584615

2024-10-20 25.6 0.461538

2024-10-21 25.4 0.430769

2024-10-22 24.7 0.323077

2024-10-23 24.9 0.353846

2024-10-24 23.4 0.123077

2024-10-25 22.6 0.000000

2024-10-26 23.1 0.076923

2024-10-27 25.2 0.400000

2024-10-28 26.0 0.523077

2024-10-29 26.0 0.523077

2024-10-30 26.2 0.553846

2024-10-31 25.4 0.430769

2024-11-01 24.0 0.215385

https://colab.research.google.com/drive/1h5vXmoqDxXdNN4I_kivnFAvEVWuzxJCY#scrollTo=JMwgEWLyK6F7&printMode=true 4/8
01/11/2024, 15:41 Untitled11.ipynb - Colab

Next
steps:
Generate code
with
temp_df
toggle_off View recommended
plots
New interactive
sheet

def create_sequences(data, n_input_days):

X, y = [], []
for i in range(len(data) - n_input_days):
X.append(data[i:i + n_input_days])
y.append(data[i + n_input_days])
return X, y

normalized_temps = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]


n_input_days = 3

X, y = create_sequences(normalized_temps, n_input_days)

print("X:", X)
print("y:", y)

X: [[0.1, 0.2, 0.3], [0.2, 0.3, 0.4], [0.3, 0.4, 0.5]]


y: [0.4, 0.5, 0.6]

import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense

normalized_temps = temp_df['Normalized_Temperature'].values

n_input_days = 3

def create_sequences(data, n_input_days):


X, y = [], []
for i in range(len(data) - n_input_days):
X.append(data[i:i + n_input_days])
y.append(data[i + n_input_days])
return np.array(X), np.array(y)

X, y = create_sequences(normalized_temps, n_input_days)

model = Sequential()
model.add(LSTM(units=50, activation='relu', input_shape=(X.shape[1], 1)))
model.add(Dense(units=1))

model.compile(loss='mse', optimizer='adam')

model.fit(X, y, epochs=50, batch_size=32)

https://colab.research.google.com/drive/1h5vXmoqDxXdNN4I_kivnFAvEVWuzxJCY#scrollTo=JMwgEWLyK6F7&printMode=true 5/8
01/11/2024, 15:41 Untitled11.ipynb - Colab

https://colab.research.google.com/drive/1h5vXmoqDxXdNN4I_kivnFAvEVWuzxJCY#scrollTo=JMwgEWLyK6F7&printMode=true 6/8
01/11/2024, 15:41 Untitled11.ipynb - Colab
<keras.src.callbacks.history.History at 0x7802688087c0>

last_n_days_temps = normalized_temps[-n_input_days:]
input_sequence = np.array([last_n_days_temps])

prediction = model.predict(input_sequence)

actual_prediction = scaler.inverse_transform(prediction)

print("Predicted temperature for the next day:", actual_prediction[0][0])

1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 533ms/step


Predicted temperature for the next day: 25.390377

import numpy as np
from tensorflow import keras
from keras.models import Sequential
from keras.layers import LSTM, Dense

import numpy as np
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense

sequence = [10, 20, 30, 40, 50, 60, 70, 80, 90]

def create_sequences(data, seq_length):


X, y = [], []
for i in range(len(data) - seq_length - 1):
X.append(data[i:(i + seq_length)])
y.append(data[i + seq_length])
return np.array(X), np.array(y)

seq_length = 3
X, y = create_sequences(sequence, seq_length)

X = X.reshape((X.shape[0], X.shape[1], 1))

model = Sequential()
model.add(SimpleRNN(units=50, activation='relu', input_shape=(seq_length, 1)))
model.add(Dense(units=1))

model.compile(loss='mse', optimizer='adam')

https://colab.research.google.com/drive/1h5vXmoqDxXdNN4I_kivnFAvEVWuzxJCY#scrollTo=JMwgEWLyK6F7&printMode=true 7/8
01/11/2024, 15:41 Untitled11.ipynb - Colab
model.fit(X, y, epochs=200, verbose=0)

input_seq = np.array(sequence[-seq_length:]).reshape((1, seq_length, 1))

predicted_number = model.predict(input_seq)[0][0]

print("Predicted next number:", predicted_number)

UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. Whe


1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 256ms/step
Predicted next number: 110.82568

import numpy as np
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense

text = "This is a sample text sequence."

chars = sorted(list(set(text)))
char_to_index = {char: index for index, char in enumerate(chars)}
index_to_char = {index: char for index, char in enumerate(chars)}

seq_length = 3
X, y = [], []
for i in range(len(text) - seq_length - 1):
X.append([char_to_index[char] for char in text[i:i + seq_length]])
y.append(char_to_index[text[i + seq_length]])

X = np.array(X)
y = np.array(y)

X = X.reshape((X.shape[0], X.shape[1], 1))

X = X / float(len(chars))

model = Sequential()
model.add(LSTM(units=50, activation='relu', input_shape=(seq_length, 1)))
model.add(Dense(units=len(chars), activation='softmax'))

model.compile(loss='sparse_categorical_crossentropy', optimizer='adam')

model.fit(X, y, epochs=100, verbose=0)

https://colab.research.google.com/drive/1h5vXmoqDxXdNN4I_kivnFAvEVWuzxJCY#scrollTo=JMwgEWLyK6F7&printMode=true 8/8

You might also like