# AlgorithmStar 特征处理与转换 计算组件 *AlgorithmStar* 特征处理,将一些计算机无法用来进行计算的事物转换为可计算的一个步骤,例如 字符串 转 向量 ## 目录 [TOC]  ## 特征提取组件 示例 ### 词频向量矩阵提取示例 在 AS 机器学习库中,经过此计算组件处理之后的字符串会返回出来一个矩阵对象,矩阵对象的排列顺序如下所示! | 句子1 | 句子2 | | | :------------: | :------------: | :------------: | | `句子1`中`单词1`出现的次数 | `句子2`中`单词1`出现的次数 | 单词1 | | `句子1`中`单词2`出现的次数 | `句子2`中`单词2`出现的次数 | 单词2 | | `句子1`中`单词3`出现的次数 | `句子2`中`单词3`出现的次数 | 单词3 | #### 直接将一些句子进行特征提取 ```java package top.lingyuzhao; import io.github.beardedManZhao.algorithmStar.algorithm.featureExtraction.WordFrequency; import io.github.beardedManZhao.algorithmStar.core.AlgorithmStar; import io.github.beardedManZhao.algorithmStar.operands.matrix.ColumnIntegerMatrix; /** * @author zhao */ public final class MAIN { public static void main(String[] args) { // 准备几句话 final String[] sentences = new String[]{ "I love you", "I love you too", "I love you too much", "I love you too much too", "I love you too much too much", "I love you too much too much too much", "I love you too much too much too much too much", "I love you too much too much too much too much too much" }; // 获取到特征处理计算组件 这里使用的是 词频向量特征提取组件 final WordFrequency wordFrequency = WordFrequency.getInstance("WordFrequency"); // 构建一个 AS 库计算对象 // TODO 需要注意是 在这里我们需要设置一下第二个泛型的类型 // 第二个泛型代表的就是 词频处理之后返回的操作数的类型 ColumnIntegerMatrix 是 wordFrequency 对应的类型 final AlgorithmStar<?, ColumnIntegerMatrix> instance = AlgorithmStar.getInstance(); // 将组件和需要被处理的数据提供给计算对象 TODO 这里获取到的类型就是 ColumnIntegerMatrix final ColumnIntegerMatrix extract = instance.extract(wordFrequency, sentences); // 打印出计算结果 System.out.println(extract); } } ``` 下面就是计算结果 ``` ------------IntegerMatrixStart----------- I love you I love you too I love you too much I love you too much too I love you too much too much I love you too much too much too much I love you too much too much too much too much I love you too much too much too much too much too much rowColName [1, 1, 1, 1, 1, 1, 1, 1] love [0, 1, 1, 2, 2, 3, 4, 5] too [1, 1, 1, 1, 1, 1, 1, 1] I [1, 1, 1, 1, 1, 1, 1, 1] you [0, 0, 1, 1, 2, 3, 4, 5] much ------------IntegerMatrixEnd------------ ``` #### 搭配着矩阵打印出详细信息 ```java package top.lingyuzhao; import io.github.beardedManZhao.algorithmStar.algorithm.featureExtraction.WordFrequency; import io.github.beardedManZhao.algorithmStar.core.AlgorithmStar; import io.github.beardedManZhao.algorithmStar.operands.matrix.ColumnIntegerMatrix; import java.util.Arrays; /** * @author zhao */ public final class MAIN { public static void main(String[] args) { // 准备几句话 final String[] sentences = new String[]{ "I love you", "I love you too", "I love you too much", "I love you too much too", "I love you too much too much", "I love you too much too much too much", "I love you too much too much too much too much", "I love you too much too much too much too much too much" }; // 获取到特征处理计算组件 这里使用的是 词频向量特征提取组件 final WordFrequency wordFrequency = WordFrequency.getInstance("WordFrequency"); // 构建一个 AS 库计算对象 // TODO 需要注意是 在这里我们需要设置一下第二个泛型的类型 // 第二个泛型代表的就是 词频处理之后返回的操作数的类型 ColumnIntegerMatrix 是 wordFrequency 对应的类型 final AlgorithmStar<?, ColumnIntegerMatrix> instance = AlgorithmStar.getInstance(); // 将组件和需要被处理的数据提供给计算对象 TODO 这里获取到的类型就是 ColumnIntegerMatrix final ColumnIntegerMatrix extract = instance.extract(wordFrequency, sentences); // 矩阵中所包含的就是 词频向量的矩阵数据 其中 行代表的就是每个单词的计数结果,列代表的就是不同的句子 // 例如在这我们获取到第 4 (索引为 3 )个句子对应的词频向量(在这里我们是通过句子获取到的) final int[] arrayByColName = extract.getArrayByColName(sentences[3]); // 我们可以直接通过打印所有行名 来获取到所有的单词 final String[] rowFieldNames = extract.getRowFieldNames(); System.out.println("被统计的单词有:" + Arrays.toString(rowFieldNames)); System.out.println("统计出来的向量:" + Arrays.toString(arrayByColName)); // 直接打印结果 for (int i = 0; i < rowFieldNames.length; i++) { System.out.println("单词:" + rowFieldNames[i] + ",在 " + sentences[3] + " 中出现的次数为:" + arrayByColName[i]); } } } ``` 下面就是计算结果 ``` 被统计的单词有:[love, too, I, you, much] 统计出来的向量:[1, 2, 1, 1, 1] 单词:love,在 I love you too much too 中出现的次数为:1 单词:too,在 I love you too much too 中出现的次数为:2 单词:I,在 I love you too much too 中出现的次数为:1 单词:you,在 I love you too much too 中出现的次数为:1 单词:much,在 I love you too much too 中出现的次数为:1 ``` ### 词频字典矩阵提取 > 值得注意的是 此组件在小于1.41 的版本中会出现越界问题,因此请尽量使用 1.41 版本来进行操作哦! ```java import io.github.beardedManZhao.algorithmStar.algorithm.featureExtraction.DictFeatureExtraction; import io.github.beardedManZhao.algorithmStar.core.AlgorithmStar; import io.github.beardedManZhao.algorithmStar.operands.matrix.ColumnIntegerMatrix; /** * 示例代码文件 */ public class MAIN1 { public static void main(String[] args) { // 准备几个所谓的类别 final String[] sentences = new String[]{ "zhao", "zhao", "asdasd", "zhao1", "zhao123", "zhao4" }; // 获取到特征处理计算组件 这里使用的是 词频向量特征提取组件 final DictFeatureExtraction dictFeatureExtraction = DictFeatureExtraction.getInstance("DictFeatureExtraction"); // 构建一个 AS 库计算对象 // TODO 需要注意是 在这里我们需要设置一下第二个泛型的类型 // 第二个泛型代表的就是 词频处理之后返回的操作数的类型 ColumnIntegerMatrix 是 dictFeatureExtraction 对应的类型 final AlgorithmStar<?, ColumnIntegerMatrix> instance = AlgorithmStar.getInstance(); // 将组件和需要被处理的数据提供给计算对象 TODO 这里获取到的类型就是 ColumnIntegerMatrix final ColumnIntegerMatrix extract = instance.extract(dictFeatureExtraction, sentences); System.out.println(extract); } } ``` 下面就是计算结果 ``` ------------IntegerMatrixStart----------- zhao4 zhao123 zhao1 asdasd zhao [0, 0, 0, 0, 1] [0, 0, 0, 0, 1] [0, 0, 0, 1, 0] [0, 0, 1, 0, 0] [0, 1, 0, 0, 0] [1, 0, 0, 0, 0] ------------IntegerMatrixEnd------------ ``` ------ ***操作记录*** 作者:[algorithmStar](https://www.lingyuzhao.top//index.html?search=23 "algorithmStar") 操作时间:2024-07-17 23:34:45 星期三 事件描述备注:保存/发布 中国 天津 [](如果不需要此记录可以手动删除,每次保存都会自动的追加记录)