This post https://stackoverflow.com/a/5541452/6394617
suggests a way to make a Numpy array immutable, using .flags.writeable = False
However, when I test this:
arr = np.arange(20).reshape((4,5))
arr.flags.writeable = False
arr
for i in range(5):
np.random.shuffle(arr[:,i])
arr
The array is shuffled in place, without even a warning.
QUESTION: Is there a way to make the array immutable?
BACKGROUND:
For context, I'm doing machine learning, and I have feature arrays, X, which are floats, and label arrays, y, which are ints.
I'm new to Scikit-learn, but from what I've read, it seems like the fit methods shuffle the arrays in place. That said, when I created two arrays, fit a model to the data, and inspected the arrays afterwards, they were in the original order. So I'm just not familiar with how Scikit-learn shuffles, and haven't been able to find an easy explanation to that online yet.
I'm using many different models, and doing some preprocessing in between, and I'm worried that at some point my two arrays may get shuffled so that the rows no longer correspond appropriately.
It would give me piece of mind if I could make the arrays immutable. I'm sure I could switch to tuples instead of Numpy arrays, but I suspect that would be more complicated to code and slower.
arr[:, i]returns something like a "view" of the data, not the array itself.np.random.shuffle(x)will throw an errorfitshouldn't shuffle the columns. If it shuffles anything, it should do the whole row.X.flags.writeable = Falsebeforeclf.fit(X,y)and did not cause any errors, since it seemed to me likefitwas going to try to shuffle the data in place, but should not have been able to. So I'm not sure how the scikit-learn library shuffles the data. I haven't dug through every line of source code, and don't really have time to, which is why I was hoping there was some way to just lock the array, in a way that prevented any changes to it.arr[:, i]is a view, but that it is a one-dimensional array. It looks like theshufflemethod does not respect thewriteableflag when the input is a 1-d array. E.g.x = np.arange(5); x.flags.writeable = False; np.random.shuffle(x)succeeds. This might be a bug in theshufflemethod.