共用方式為


microsoftml.concat:將多個資料行串連成單一向量

使用方式

microsoftml.concat(cols: [dict, list], **kargs)

Description

將數個資料行結合成單一向量值資料行。

詳細資料

concat 會從多個資料行建立單一向量值資料行。 您可以在定型模型之前,先對資料執行。 當資料行數目大到數百個至數千個時,串連可以大幅加快資料處理速度。

引數

cols

要轉換的字元 dict 或變數名稱清單。 如果是 dict,則索引鍵代表要建立的新變數名稱。 請注意,所有輸入變數都必須是相同的類型。 您可以使用串連轉換產生多個輸出資料行。 在此情況下,您必須使用向量清單來定義輸入和輸出變數之間的一對一對應。 例如,若要將資料行 InNameA 和 InNameB 串連到資料行 OutName1,同時也要將資料行 InNameC 和 InNameD 串連到資料行 OutName2,請使用 dict:dict(OutName1 = [InNameA, InNameB], outName2 = [InNameC, InNameD])

kargs

傳送至計算引擎的其他引數。

傳回

定義串連轉換的物件。

另請參閱

drop_columns, select_columns.

範例

'''
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