简体   繁体   English

我可以使 Numpy 阵列不可变吗?

[英]Can I make a Numpy array immutable?

This post https://stackoverflow.com/a/5541452/6394617这篇文章https://stackoverflow.com/a/5541452/6394617

suggests a way to make a Numpy array immutable, using .flags.writeable = False建议使用.flags.writeable = False使 Numpy 数组不可变的方法

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.对于上下文,我正在做机器学习,我有特征 arrays, X,它们是浮点数,和 label arrays,y,它们是整数。

I'm new to Scikit-learn, but from what I've read, it seems like the fit methods shuffle the arrays in place.我是 Scikit-learn 的新手,但从我读过的内容来看,似乎拟合方法将 arrays 洗牌到位。 That said, when I created two arrays, fit a model to the data, and inspected the arrays afterwards, they were in the original order.也就是说,当我创建两个 arrays,将 model 拟合到数据中,然后检查 arrays 时,它们是原始顺序。 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.所以我只是不熟悉 Scikit-learn 是如何洗牌的,而且还没有在网上找到一个简单的解释。

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.我正在使用许多不同的模型,并在两者之间进行一些预处理,我担心在某些时候我的两个 arrays 可能会被打乱,因此行不再适当地对应。

It would give me piece of mind if I could make the arrays immutable.如果我能让 arrays 不可变,我会放心。 I'm sure I could switch to tuples instead of Numpy arrays, but I suspect that would be more complicated to code and slower.我确信我可以切换到元组而不是 Numpy arrays,但我怀疑编码会更复杂且速度更慢。

This is a bug in numpy.random.shuffle in numpy versions 1.22 and earlier.这是 numpy 版本 1.22 及更早版本中numpy.random.shuffle中的一个错误。 The function does not respect the writeable flag of the input array when the array is one-dimensional.当数组是一维时,function 不尊重输入数组的writeable标志。

numpy.random.Generator.shuffle has the same issue, and numpy.random.Generator.permuted fails to respect the writeable flag for arrays of any dimension. numpy.random.Generator.shuffle有同样的问题,并且numpy.random.Generator.permuted无法尊重任何维度的 arrays 的writeable标志。

This has been fixed in the main development branch of NumPy, so NumPy versions 1.23.0 and later will not have this bug.此问题已在 Z3B7F949B2343F9E5390 E29F6EF5E1778Z的主开发分支中修复,因此 NumPy 版本 1.23.0 及更高版本不会出现此错误。 Note that NumPy 1.22.0 has not been released yet, but is available as a release candidate.请注意,NumPy 1.22.0 尚未发布,但可作为候选发布。 The fix occurred after the branching of 1.22, so the fix will not be in 1.22.0.修复发生在 1.22 分支之后,因此修复不会在 1.22.0 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM