Xarray¶
设置¶
In [ ]:
Copied!
pip install ydf xarray -U
pip install ydf xarray -U
In [1]:
Copied!
import ydf
import xarray as xr
import ydf
import xarray as xr
In [2]:
Copied!
dataset = xr.Dataset({
"feature_1": ("example", [0.1, 0.2, 0.3, 0.4]),
"feature_2": ("example", ["X", "X", "Y", "Y"]),
"feature_3": (("example", "pixel"), [[1, 2], [3, 4], [5, 6], [7, 8]]),
"label": ("example", ["A", "B", "A", "B"]),
})
dataset = xr.Dataset({
"feature_1": ("example", [0.1, 0.2, 0.3, 0.4]),
"feature_2": ("example", ["X", "X", "Y", "Y"]),
"feature_3": (("example", "pixel"), [[1, 2], [3, 4], [5, 6], [7, 8]]),
"label": ("example", ["A", "B", "A", "B"]),
})
此数据集包含4个示例。feature1
是一个一维的数值特征。feature_2
是一个分类特征。feature_3
是一个多维特征。最后,label
是标签。
我们可以在这个(非常简单的)数据集上训练一个模型。
In [4]:
Copied!
model = ydf.RandomForestLearner(label="label").train(dataset)
model = ydf.RandomForestLearner(label="label").train(dataset)
Train model on 4 examples Model trained in 0:00:00.006497
我们可以用模型进行预测。
In [5]:
Copied!
model.predict(dataset)
model.predict(dataset)
Out[5]:
array([0.2999998, 0.2999998, 0.2999998, 0.2999998], dtype=float32)