forked from gautam132002/crashpredict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_rnn.py
47 lines (33 loc) · 1.58 KB
/
train_rnn.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
import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
def predict_rnn():
data = pd.read_csv('data.csv')
target = data['ticket']
# Encode the 'ticket' labels
label_encoder = LabelEncoder()
target_encoded = label_encoder.fit_transform(target)
sequence_length = 20
sequences = [target_encoded[i:i+sequence_length] for i in range(len(target_encoded)-sequence_length)]
# Convert sequences to NumPy array
sequences = np.array(sequences)
X = sequences[:, :-1]
y = sequences[:, -1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Build the RNN model
model = tf.keras.Sequential([
tf.keras.layers.Embedding(input_dim=len(np.unique(target_encoded)), output_dim=50, input_length=sequence_length-1),
tf.keras.layers.LSTM(100),
tf.keras.layers.Dense(len(np.unique(target_encoded)), activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=20, batch_size=32, validation_data=(X_test, y_test))
last_sequence = target_encoded[-sequence_length+1:]
predicted_class = np.argmax(model.predict(np.expand_dims(last_sequence, axis=0)))
# Decode the predicted class
predicted_ticket = label_encoder.inverse_transform([predicted_class])[0]
# print(f"Predicted Ticket for the next event: {predicted_ticket}")
return predicted_ticket/100.0
# print(predict_rnn())