Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing AttributeError & suggesting Alternative solution for Puzzle N53 #49

Open
Artur-Arstamyan opened this issue Oct 1, 2024 · 0 comments

Comments

@Artur-Arstamyan
Copy link

Artur-Arstamyan commented Oct 1, 2024

AttributeError

  • Code
from scipy.signal import convolve2d
mine_grid = df.pivot_table(columns='x', index='y', values='mine')
counts = convolve2d(mine_grid.astype(complex), np.ones((3, 3)), mode='same').real.astype(int)
df['adjacent'] = (counts - mine_grid).ravel('F')
df
  • Output
AttributeError: 'DataFrame' object has no attribute 'ravel'
  • Fixing using to_numpy()
df['adjacent'] = (counts - mine_grid).to_numpy().ravel('F')

Alternative solution

  • Code
from numpy.lib.stride_tricks import sliding_window_view
arr = df['mine'].values.reshape(5, 4)
df['adjacent'] = (sliding_window_view(np.pad(arr, 1), window_shape=(3, 3)).sum(axis=(-2, -1))-arr).ravel()
print(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant