Advertisement
here2share

# fft_predict_pattern.py

Aug 10th, 2023 (edited)
893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.28 KB | None | 0 0
  1. # fft_predict_pattern.py
  2.  
  3. from math import pi, cos, sin, sqrt
  4.  
  5. # pi as string for the digits variable https://pastebin.com/JW4ii30e
  6. digits =
  7.  
  8. # Convert to list of integers
  9. full_digits = list(map(int, digits))
  10.  
  11. # Define the pattern length and sliding window size
  12. training_length = 500
  13. window_size = 20
  14.  
  15. # Define the length of the test set
  16. test_length = len(digits) - training_length
  17.  
  18. # Define the Fourier transform function
  19. def fourier_transform(digits):
  20.     n = len(digits)
  21.     fft = []
  22.     for k in range(n):
  23.         re = 0
  24.         im = 0
  25.         for j in range(n):
  26.             a = 2 * pi * j * k / n
  27.             re += digits[j] * cos(a)
  28.             im -= digits[j] * sin(a)
  29.         fft.append(re + im * 1j)
  30.     return fft
  31.  
  32. def fft_predict_pattern():
  33.     # Generate a list of overlapping patterns using a sliding window approach
  34.     patterns = []
  35.     for i in range(0, len(full_digits) - training_length + 1, window_size):
  36.         pattern_digits = full_digits[i:i+training_length]
  37.         pattern_fft = fourier_transform(pattern_digits)
  38.         patterns.append(pattern_fft)
  39.  
  40.     predicted_digits = full_digits[:test_length]
  41.     for i in range(test_length, len(full_digits)):
  42.         # Generate the current pattern using the last training_length digits
  43.         current_digits = predicted_digits[i-training_length:i]
  44.         current_fft = fourier_transform(current_digits)
  45.        
  46.         # Find the best pattern match based on the Fourier transform
  47.         best_match = None
  48.         best_distance = float("inf")
  49.         for pattern in patterns:
  50.             distance = sqrt(sum(abs(current_fft[j]-pattern[j])**2 for j in range(training_length)))
  51.             if distance < best_distance:
  52.                 best_distance = distance
  53.                 best_match = pattern
  54.            
  55.         # Use the best match to predict the next digit
  56.         prediction = sum([best_match[j] * j for j in range(training_length)])
  57.         predicted_digit = int(round(prediction.real)) % 10
  58.         predicted_digits.append(predicted_digit)
  59.  
  60.     # Calculate the accuracy
  61.     correct_predictions = sum([1 for i in range(training_length, len(full_digits)) if full_digits[i] == predicted_digits[i]])
  62.     accuracy = correct_predictions / test_length * 100
  63.  
  64.     print(f"Percentage of correctly predicted digits: {accuracy:.2f}%")
  65.    
  66. fft_predict_pattern()
  67.  
  68. # Percentage of correctly predicted digits: 90.04% <<< I'm surprised that it's even over 20% given the odds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement