jeevan0704/sequential-model-for-sequential_dataset
0
1"""2Utility functions for data preprocessing and model handling3"""4import numpy as np5 6def normalize_sequence(sequence, method='minmax'):7 """8 Normalize a sequence using specified method9 10 Args:11 sequence: Input sequence (numpy array or list)12 method: 'minmax' or 'standard'13 14 Returns:15 Normalized sequence16 """17 sequence = np.array(sequence)18 19 if method == 'minmax':20 min_val = np.min(sequence)21 max_val = np.max(sequence)22 if max_val - min_val == 0:23 return sequence24 return (sequence - min_val) / (max_val - min_val)25 26 elif method == 'standard':27 mean = np.mean(sequence)28 std = np.std(sequence)29 if std == 0:30 return sequence31 return (sequence - mean) / std32 33 return sequence34 35def pad_sequence(sequence, max_length, padding_value=0):36 """37 Pad sequence to a fixed length38 39 Args:40 sequence: Input sequence41 max_length: Target length42 padding_value: Value to use for padding43 44 Returns:45 Padded sequence46 """47 sequence = np.array(sequence)48 49 if len(sequence) >= max_length:50 return sequence[:max_length]51 52 padding = np.full(max_length - len(sequence), padding_value)53 return np.concatenate([sequence, padding])54 55def reshape_for_lstm(data, timesteps, features):56 """57 Reshape data for LSTM input (samples, timesteps, features)58 59 Args:60 data: Input data61 timesteps: Number of timesteps62 features: Number of features63 64 Returns:65 Reshaped data66 """67 data = np.array(data)68 69 if len(data.shape) == 1:70 # Flatten array, reshape to (samples, timesteps, features)71 total_elements = data.shape[0]72 samples = total_elements // (timesteps * features)73 return data[:samples * timesteps * features].reshape(samples, timesteps, features)74 75 return data.reshape(-1, timesteps, features)76 77def validate_input_shape(data, expected_shape):78 """79 Validate that input data matches expected shape80 81 Args:82 data: Input data83 expected_shape: Tuple of expected dimensions84 85 Returns:86 Boolean indicating if shape is valid87 """88 data = np.array(data)89 90 if len(data.shape) != len(expected_shape):91 return False92 93 for i, (actual, expected) in enumerate(zip(data.shape, expected_shape)):94 if expected is not None and actual != expected:95 return False96 97 return True98 99def format_prediction_output(prediction, class_names=None):100 """101 Format prediction output for display102 103 Args:104 prediction: Model prediction (numpy array)105 class_names: Optional list of class names106 107 Returns:108 Formatted prediction dictionary109 """110 prediction = np.array(prediction)111 112 result = {113 'raw_prediction': prediction.tolist()114 }115 116 # Handle classification predictions117 if len(prediction.shape) == 2 and prediction.shape[1] > 1:118 predicted_class = np.argmax(prediction, axis=1)[0]119 confidence = np.max(prediction, axis=1)[0]120 121 result['predicted_class'] = int(predicted_class)122 result['confidence'] = float(confidence)123 124 if class_names and predicted_class < len(class_names):125 result['class_name'] = class_names[predicted_class]126 127 # Add probabilities for all classes128 result['probabilities'] = {129 f'class_{i}': float(prob) 130 for i, prob in enumerate(prediction[0])131 }132 133 # Handle regression predictions134 elif len(prediction.shape) == 1 or prediction.shape[1] == 1:135 result['value'] = float(prediction.flatten()[0])136 137 return result138 