Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # fft_predict_pattern.py
- from math import pi, cos, sin, sqrt
- # pi as string for the digits variable https://pastebin.com/JW4ii30e
- digits =
- # Convert to list of integers
- full_digits = list(map(int, digits))
- # Define the pattern length and sliding window size
- training_length = 500
- window_size = 20
- # Define the length of the test set
- test_length = len(digits) - training_length
- # Define the Fourier transform function
- def fourier_transform(digits):
- n = len(digits)
- fft = []
- for k in range(n):
- re = 0
- im = 0
- for j in range(n):
- a = 2 * pi * j * k / n
- re += digits[j] * cos(a)
- im -= digits[j] * sin(a)
- fft.append(re + im * 1j)
- return fft
- def fft_predict_pattern():
- # Generate a list of overlapping patterns using a sliding window approach
- patterns = []
- for i in range(0, len(full_digits) - training_length + 1, window_size):
- pattern_digits = full_digits[i:i+training_length]
- pattern_fft = fourier_transform(pattern_digits)
- patterns.append(pattern_fft)
- predicted_digits = full_digits[:test_length]
- for i in range(test_length, len(full_digits)):
- # Generate the current pattern using the last training_length digits
- current_digits = predicted_digits[i-training_length:i]
- current_fft = fourier_transform(current_digits)
- # Find the best pattern match based on the Fourier transform
- best_match = None
- best_distance = float("inf")
- for pattern in patterns:
- distance = sqrt(sum(abs(current_fft[j]-pattern[j])**2 for j in range(training_length)))
- if distance < best_distance:
- best_distance = distance
- best_match = pattern
- # Use the best match to predict the next digit
- prediction = sum([best_match[j] * j for j in range(training_length)])
- predicted_digit = int(round(prediction.real)) % 10
- predicted_digits.append(predicted_digit)
- # Calculate the accuracy
- correct_predictions = sum([1 for i in range(training_length, len(full_digits)) if full_digits[i] == predicted_digits[i]])
- accuracy = correct_predictions / test_length * 100
- print(f"Percentage of correctly predicted digits: {accuracy:.2f}%")
- fft_predict_pattern()
- # 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