microsoftml.concat:将多个列串联为一个向量
使用情况
microsoftml.concat(cols: [dict, list], **kargs)
说明
将多个列合并为一个向量值列。
详细信息
concat
从多个列创建单个向量值列。 在训练模型之前,可以对数据执行此方法。 当列数高达数百到数千列时,串联会显著提高数据处理速度。
参数
cols
要转换的字符 dict 或变量名称列表。 如果是 dict
,则键表示要创建的新变量的名称。
请注意,所有输入变量的类型必须相同。 可以使用串联转换生成多个输出列。 在这种情况下,需要使用向量列表来定义输入和输出变量之间的一对一映射。
例如,若要将 InNameA 和 InNameB 列连接到 OutName1 列,并将 InNameC 和 InNameD 列连接到 OutName2 列,请使用 dict(OutName1 = [InNameA, InNameB], outName2 = [InNameC, InNameD])
kargs
发送到计算引擎的其他参数。
返回
一个定义串联转换的对象。
请参阅
示例
'''
Example on logistic regression and concat.
'''
import numpy
import pandas
import sklearn
from microsoftml import rx_logistic_regression, concat, rx_predict
from microsoftml.datasets.datasets import get_dataset
iris = get_dataset("iris")
if sklearn.__version__ < "0.18":
from sklearn.cross_validation import train_test_split
else:
from sklearn.model_selection import train_test_split
# We use iris dataset.
irisdf = iris.as_df()
# The training features.
features = ["Sepal_Length", "Sepal_Width", "Petal_Length", "Petal_Width"]
# The label.
label = "Label"
# microsoftml needs a single dataframe with features and label.
cols = features + [label]
# We split into train/test. y_train, y_test are not used.
data_train, data_test, y_train, y_test = train_test_split(irisdf[cols], irisdf[label])
# We train a logistic regression.
# A concat transform is added to group features in a single vector column.
multi_logit_out = rx_logistic_regression(
formula="Label ~ Features",
method="multiClass",
data=data_train,
ml_transforms=[concat(cols={'Features': features})])
# We show the coefficients.
print(multi_logit_out.coef_)
# We predict.
prediction = rx_predict(multi_logit_out, data=data_test)
print(prediction.head())
输出:
Automatically adding a MinMax normalization transform, use 'norm=Warn' or 'norm=No' to turn this behavior off.
Beginning processing data.
Rows Read: 112, Read Time: 0, Transform Time: 0
Beginning processing data.
Beginning processing data.
Rows Read: 112, Read Time: 0, Transform Time: 0
Beginning processing data.
Beginning processing data.
Rows Read: 112, Read Time: 0.001, Transform Time: 0
Beginning processing data.
LBFGS multi-threading will attempt to load dataset into memory. In case of out-of-memory issues, turn off multi-threading by setting trainThreads to 1.
Beginning optimization
num vars: 15
improvement criterion: Mean Improvement
L1 regularization selected 9 of 15 weights.
Not training a calibrator because it is not needed.
Elapsed time: 00:00:00.2348578
Elapsed time: 00:00:00.0197433
OrderedDict([('0+(Bias)', 1.943994402885437), ('1+(Bias)', 0.6346845030784607), ('2+(Bias)', -2.57867693901062), ('0+Petal_Width', -2.7277402877807617), ('0+Petal_Length', -2.5394322872161865), ('0+Sepal_Width', 0.4810805320739746), ('1+Sepal_Width', -0.5790582299232483), ('2+Petal_Width', 2.547518491744995), ('2+Petal_Length', 1.6753791570663452)])
Beginning processing data.
Rows Read: 38, Read Time: 0, Transform Time: 0
Beginning processing data.
Elapsed time: 00:00:00.0662932
Finished writing 38 rows.
Writing completed.
Score.0 Score.1 Score.2
0 0.320061 0.504115 0.175825
1 0.761624 0.216213 0.022163
2 0.754765 0.215548 0.029687
3 0.182810 0.517855 0.299335
4 0.018770 0.290014 0.691216