How to resolve numpy deprecation issue: AttributeError: module 'numpy' has no attribute 'str'. Did you mean: 'std'?
最近準備 Model Training Code 的時候, 發現幾個禮拜前的 Code 居然 Failed. 才發現一直沒仔細看 Warninig message.
FutureWarning: In the future np.str
will be defined as the corresponding NumPy scalar. (This may have returned Python scalars in past versions.
之前的版本
$ pip show numpy Name: numpy Version: 1.23.5 Summary: NumPy is the fundamental package for array computing with Python. Home-page: https://www.numpy.org Author: Travis E. Oliphant et al. Author-email: License: BSD Location: /home/jimmyliao/venvs/mlops/lib/python3.10/site-packages Requires: Required-by: contourpy, matplotlib, mlflow, numba, pandas, pyarrow, scikit-learn, scipy, shap
原來, 一直都有提示即將被 deprecated.
$ python train_iris.py train_iris.py:28: DeprecationWarning: `np.str` is a deprecated alias for the builtin `str`. To silence this warning, use `str` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.str_` here. Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations run.log('Kernel type', np.str(args.kernel)) Attempted to log scalar metric Kernel type: linear /home/jimmyliao/workspace/lab/train_iris.py:29: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here. Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations run.log('Penalty', np.float(args.penalty)) Attempted to log scalar metric Penalty: 1.0
升級到目前的 release:
1.24.1
$ pip install -U numpy Successfully installed numpy-1.24.1 (mlops) jimmyliao@JimmyLiaoSL:~/workspace/lab$ pip show numpy Name: numpy Version: 1.24.1 Summary: Fundamental package for array computing in Python Home-page: https://www.numpy.org Author: Travis E. Oliphant et al. Author-email: License: BSD-3-Clause Location: /home/jimmyliao/venvs/mlops/lib/python3.10/site-packages Requires: Required-by: contourpy, matplotlib, mlflow, numba, pandas, pyarrow, scikit-learn, scipy, shap
執行就直接噴 Error
train_iris.py:28: FutureWarning: In the future `np.str` will be defined as the corresponding NumPy scalar. (This may have returned Python scalars in past versions. run.log('Kernel type', np.str(args.kernel)) Traceback (most recent call last): File "/home/jimmyliao/workspace/lab/train_iris.py", line 60, in <module> main() File "/home/jimmyliao/workspace/lab/train_iris.py", line 28, in main run.log('Kernel type', np.str(args.kernel)) File "/home/jimmyliao/venvs/mlops/lib/python3.10/site-packages/numpy/__init__.py", line 284, in __getattr__ raise AttributeError("module {!r} has no attribute " AttributeError: module 'numpy' has no attribute 'str'. Did you mean: 'std'?
回頭找一下 Numpy 官網, 早就在 1.20 的文件就有說明.
https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
Using the aliases of builtin types like np.int is deprecated
For a long time, np.int has been an alias of the builtin int. This is repeatedly a cause of confusion for newcomers, and existed mainly for historic reasons.
白話的意思就是
np.int
通常被拿來當作內建int
的 alias, 在之前的版本其實都有 FutureWarning 只是都被忽略.現在要正式 deprecated.解決方法就照上面的文件說明, 取代成對應的. 像是:
run.log('Kernel type', np.str_(args.kernel)) run.log('Penalty', np.double(args.penalty))
$ python train_iris.py Attempted to log scalar metric Kernel type: linear Attempted to log scalar metric Penalty: 1.0 Accuracy of SVM classifier on test set: 0.97 Attempted to log scalar metric Accuracy: 0.9736842105263158 [[13 0 0] [ 0 15 1] [ 0 0 9]]
收工.