toad icon indicating copy to clipboard operation
toad copied to clipboard

导入numpy后,numpy.str报错

Open wugeer opened this issue 5 months ago • 0 comments

python 3.11安装toad后,环境和版本如下

(py311) [root@rock9 ~]# pip list | grep -P 'toad|numpy'
numpy           1.26.4
toad            0.1.0
(py311) [root@rock9 ~]# python -V
Python 3.11.9

test.py内容如下

import pandas as pd
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from toad.transform import Combiner

# 创建示例数据
data = pd.DataFrame({
    'numerical_feature1': [1, 2, 3, 4, 5],
    'numerical_feature2': [5, 4, 3, 2, 1],
    'categorical_feature': ['A', 'B', 'A', 'B', 'C']
})

# 定义变换器
num_features = ['numerical_feature1', 'numerical_feature2']
cat_features = ['categorical_feature']

# 数值特征变换器
num_transformer = StandardScaler()

# 类别特征变换器
cat_transformer = OneHotEncoder()

# 创建 Combiner 对象
combiner = Combiner()

# 配置变换器
combiner.fit_transform(num_transformer, num_features)
combiner.fit_transform(cat_transformer, cat_features)

# 训练变换器
combiner.fit(data)

# 应用变换
transformed_data = combiner.transform(data)

print("原数据:")
print(data)
print("\n转换后的数据:")
print(transformed_data)

执行结果

(py311) [root@rock9 ~]# python test.py 
/opt/anaconda3/envs/py311/lib/python3.11/site-packages/toad/utils/func.py:109: FutureWarning: In the future `np.str` will be defined as the corresponding NumPy scalar.
  arr = arr.astype(np.str)
Traceback (most recent call last):
  File "/root/1.py", line 27, in <module>
    combiner.fit_transform(num_transformer, num_features)
  File "/opt/anaconda3/envs/py311/lib/python3.11/site-packages/sklearn/utils/_set_output.py", line 316, in wrapped
    data_to_wrap = f(self, X, *args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/envs/py311/lib/python3.11/site-packages/sklearn/base.py", line 1101, in fit_transform
    return self.fit(X, y, **fit_params).transform(X)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/envs/py311/lib/python3.11/site-packages/toad/utils/decorator.py", line 55, in func
    return self.__call__(instance, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/envs/py311/lib/python3.11/site-packages/toad/utils/decorator.py", line 46, in __call__
    return self.wrapper(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/envs/py311/lib/python3.11/site-packages/toad/utils/decorator.py", line 89, in wrapper
    return self.call(X, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/envs/py311/lib/python3.11/site-packages/toad/utils/decorator.py", line 75, in call
    return self.fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/envs/py311/lib/python3.11/site-packages/toad/utils/decorator.py", line 46, in __call__
    return self.wrapper(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/envs/py311/lib/python3.11/site-packages/toad/utils/decorator.py", line 100, in wrapper
    return self.call(X, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/envs/py311/lib/python3.11/site-packages/toad/utils/decorator.py", line 75, in call
    return self.fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/envs/py311/lib/python3.11/site-packages/toad/transform.py", line 43, in fit
    rules[name] = self.fit_(X, *args, **kwargs)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/envs/py311/lib/python3.11/site-packages/toad/transform.py", line 193, in fit_
    X = to_ndarray(X)
        ^^^^^^^^^^^^^
  File "/opt/anaconda3/envs/py311/lib/python3.11/site-packages/toad/utils/func.py", line 109, in to_ndarray
    arr = arr.astype(np.str)
                     ^^^^^^
  File "/opt/anaconda3/envs/py311/lib/python3.11/site-packages/numpy/__init__.py", line 324, in __getattr__
    raise AttributeError(__former_attrs__[attr])
AttributeError: module 'numpy' has no attribute 'str'.
`np.str` was a deprecated alias for the builtin `str`. To avoid this error in existing code, 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.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations. Did you mean: 'std'?

image

wugeer avatar Sep 13 '24 03:09 wugeer