Java 类名:com.alibaba.alink.pipeline.classification.Softmax
Python 类名:Softmax
Softmax算法是Logistic回归算法的推广,Logistic回归主要是用来处理二分类问题,而Softmax回归则是处理多分类问题。
面对多分类问题,建立代价函数,然后通过优化方法迭代求解出最优的模型参数。具体原理可参考文献。
该算法经常应用到多分类问题中,类似情感分析、手写字识别等问题都可以使用Softmax算法,该算法支持稀疏和稠密两种输入样本。
[1] Brown, Peter F., et al. “Class-based n-gram models of natural language.” Computational linguistics 18.4 (1992): 467-480.
[2] Goodman, Joshua. “Classes for fast maximum entropy training.” 2001 IEEE International Conference on Acoustics, Speech, and Signal Processing. Proceedings (Cat. No. 01CH37221). Vol. 1. IEEE, 2001.
| 名称 | 中文名称 | 描述 | 类型 | 是否必须? | 取值范围 | 默认值 |
|---|---|---|---|---|---|---|
| labelCol | 标签列名 | 输入表中的标签列名 | String | ✓ | ||
| predictionCol | 预测结果列名 | 预测结果列名 | String | ✓ | ||
| epsilon | 收敛阈值 | 迭代方法的终止判断阈值,默认值为 1.0e-6 | Double | x >= 0.0 | 1.0E-6 | |
| featureCols | 特征列名数组 | 特征列名数组,默认全选 | String[] | null | ||
| l1 | L1 正则化系数 | L1 正则化系数,默认为0。 | Double | x >= 0.0 | 0.0 | |
| l2 | L2 正则化系数 | L2 正则化系数,默认为0。 | Double | x >= 0.0 | 0.0 | |
| learningRate | 学习率 | 优化算法的学习率,默认0.1。 | Double | 0.1 | ||
| maxIter | 最大迭代步数 | 最大迭代步数,默认为 100 | Integer | x >= 1 | 100 | |
| modelFilePath | 模型的文件路径 | 模型的文件路径 | String | null | ||
| optimMethod | 优化方法 | 优化问题求解时选择的优化方法 | String | “LBFGS”, “GD”, “Newton”, “SGD”, “OWLQN” | null | |
| overwriteSink | 是否覆写已有数据 | 是否覆写已有数据 | Boolean | false | ||
| predictionDetailCol | 预测详细信息列名 | 预测详细信息列名 | String | |||
| reservedCols | 算法保留列名 | 算法保留列 | String[] | null | ||
| standardization | 是否正则化 | 是否对训练数据做正则化,默认true | Boolean | true | ||
| vectorCol | 向量列名 | 向量列对应的列名,默认值是null | String | null | ||
| weightCol | 权重列名 | 权重列对应的列名 | String | 所选列类型为 [BIGDECIMAL, BIGINTEGER, BYTE, DOUBLE, FLOAT, INTEGER, LONG, SHORT] | null | |
| withIntercept | 是否有常数项 | 是否有常数项,默认true | Boolean | true | ||
| numThreads | 组件多线程线程个数 | 组件多线程线程个数 | Integer | 1 | ||
| modelStreamFilePath | 模型流的文件路径 | 模型流的文件路径 | String | null | ||
| modelStreamScanInterval | 扫描模型路径的时间间隔 | 描模型路径的时间间隔,单位秒 | Integer | 10 | ||
| modelStreamStartTime | 模型流的起始时间 | 模型流的起始时间。默认从当前时刻开始读。使用yyyy-mm-dd hh:mm:ss.fffffffff格式,详见Timestamp.valueOf(String s) | String | null |
from pyalink.alink import *
import pandas as pd
useLocalEnv(1)
df_data = pd.DataFrame([
[2, 1, 1],
[3, 2, 1],
[4, 3, 2],
[2, 4, 1],
[2, 2, 1],
[4, 3, 2],
[1, 2, 1],
[5, 3, 3]
])
batchData = BatchOperator.fromDataframe(df_data, schemaStr='f0 int, f1 int, label int')
dataTest = batchData
colnames = ["f0","f1"]
softmax = Softmax().setFeatureCols(colnames).setLabelCol("label").setPredictionCol("pred")
model = softmax.fit(batchData)
model.transform(dataTest).print()
import org.apache.flink.types.Row;
import com.alibaba.alink.operator.batch.BatchOperator;
import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
import com.alibaba.alink.pipeline.classification.Softmax;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
public class SoftmaxTest {
@Test
public void testSoftmax() throws Exception {
List <Row> df_data = Arrays.asList(
Row.of(2, 1, 1),
Row.of(3, 2, 1),
Row.of(4, 3, 2),
Row.of(2, 4, 1),
Row.of(2, 2, 1),
Row.of(4, 3, 2),
Row.of(1, 2, 1),
Row.of(5, 3, 3)
);
BatchOperator <?> batchData = new MemSourceBatchOp(df_data, "f0 int, f1 int, label int");
Softmax softmax = new Softmax().setFeatureCols("f0", "f1").setLabelCol("label").setPredictionCol("pred");
softmax.fit(batchData).transform(batchData).print();
}
}
| f0 | f1 | label | pred |
|---|---|---|---|
| 2 | 1 | 1 | 1 |
| 3 | 2 | 1 | 1 |
| 4 | 3 | 2 | 2 |
| 2 | 4 | 1 | 1 |
| 2 | 2 | 1 | 1 |
| 4 | 3 | 2 | 2 |
| 1 | 2 | 1 | 1 |
| 5 | 3 | 3 | 3 |