MLRun特征集导入时出现"_fn0键值为None"错误的解决咨询
问题
在使用MLRun向FeatureSet导入数据时出现以下错误:
错误日志
2023-05-20 13:11:40,669 [info] loaded project my-project7xx from ./ and saved in MLRun DB 2023-05-20 13:11:53,640 [error] For {'_1': 374, '_2': 886, '_3': 989, '_4': 191, '_5': 49, '_6': 658, '_7': 994, '_8': 857, '_9': 217, '_10': 220} value of key _fn0 is None 2023-05-20 13:11:53,640 [error] For {'_1': 642, '_2': 688, '_3': 438, '_4': 599, '_5': 176, '_6': 562, '_7': 708, '_8': 444, '_9': 525, '_10': 54} value of key _fn0 is None
调用的代码片段
import mlrun import mlrun.feature_store as fs ... project = mlrun.get_or_create_project(project_name, context='./', user_project=False) feature_set = fstore.FeatureSet(feature_name, entities=[fstore.Entity("_fn0", value_type=mlrun.data_types.data_types.ValueType.INT32, description='fn0 description'), fstore.Entity("_fn1", value_type=mlrun.data_types.data_types.ValueType.INT32, description='fn1 description')], engine="storey") feature_set.save() ... df = pandas.DataFrame(numpy.random.randint(low=0, high=1000, size=(100, 10)), # rows, columns columns=[f"_fn{i}" for i in range(10)]) fs.ingest(feature_set, df)
解决方案
1. 对齐数据列名与实体定义
错误核心是MLRun找不到实体_fn0对应的数据列,日志显示传入的数据行字段是_1、_2这类,和代码定义的_fn0、_fn1完全不匹配。
- 在
fs.ingest执行前添加代码验证DataFrame列名:
确认输出为print(df.columns.tolist())['_fn0', '_fn1', '_fn2', ..., '_fn9'],若不符则修正列名生成逻辑,确保列名前缀为_fn。
2. 修正导入与调用的不一致问题
代码中导入了mlrun.feature_store as fs,但创建FeatureSet时用了未定义的fstore,这会导致运行异常。将FeatureSet创建代码修正为:
feature_set = fs.FeatureSet(feature_name, entities=[ fs.Entity("_fn0", value_type=mlrun.data_types.data_types.ValueType.INT32, description='fn0 description'), fs.Entity("_fn1", value_type=mlrun.data_types.data_types.ValueType.INT32, description='fn1 description')], engine="storey")
3. 确保实体字段无缺失值
实体字段作为FeatureSet的标识字段,不允许为None。验证_fn0列是否存在缺失值:
print(df['_fn0'].isna().sum())
若输出大于0,需补充缺失数据或过滤掉缺失行后再执行导入操作。
内容的提问来源于stack exchange,提问作者JIST
相关产品推荐
相关产品推荐

