Memory-mapped arrays for large data in NumPy - Mini Project: Build & Apply
Create a numpy array called large_array with values from 0 to 9999 using np.arange(10000). Then save this array to a file named 'large_data.dat' using large_array.tofile('large_data.dat').
Use np.arange(10000) to create numbers from 0 to 9999. Use tofile method to save the array to a file.
Create a variable called slice_size and set it to 100. This variable will control how many elements we read from the memory-mapped array.
Just create a variable named slice_size and assign it the value 100.
Create a memory-mapped array called mmap_array from the file 'large_data.dat' using np.memmap with dtype np.int64 and mode 'r'. Then create a variable called data_slice that contains the first slice_size elements of mmap_array using slicing.
Use np.memmap with the file name, dtype np.int64, and mode 'r' to create the memory-mapped array. Use slicing [:slice_size] to get the first part.
Print the variable data_slice to display the first 100 elements read from the memory-mapped array.
Use print(data_slice) to show the first 100 elements.