AlgorithmStar(AS机器学习与科学计算库) 实现 矩阵数据类型的计算函数汇总

AlgorithmStar 实现 矩阵 计算

AlgorithmStar
本文中将会演示通过 AS 机器学习库 实现 矩阵计算

目录


在这里插入图片描述

矩阵创建

通过数组创建

package com.zhao;

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatixFactory;
import zhao.algorithmMagic.operands.matrix.DoubleMatrix;

public class MAIN {

    public static void main(String[] args) {
        // 创建矩阵对象
        final MatixFactory matixFactory = AlgorithmStar.matixFactory();
        final DoubleMatrix matrix1 = matixFactory.parseMatrix(
                new double[]{1, 2, 3},
                new double[]{4, 5, 6},
                new double[]{7, 8, 9}
        );
        final DoubleMatrix matrix2 = matixFactory.parseMatrix(
                new double[]{1, 20, 3},
                new double[]{4, 50, 6},
                new double[]{7, 80, 9}
        );

        System.out.println(matrix1);
        System.out.println(matrix2);
    }

}

下面就是打印结果

------------MatrixStart-----------
[1.0, 2.0, 3.0]
[4.0, 5.0, 6.0]
[7.0, 8.0, 9.0]
------------MatrixEnd------------

------------MatrixStart-----------
[1.0, 20.0, 3.0]
[4.0, 50.0, 6.0]
[7.0, 80.0, 9.0]
------------MatrixEnd------------


进程已结束,退出代码0

通过稀疏矩阵创建

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.DoubleMatrix;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 以及稀疏矩阵的方式创建两个矩阵
        final IntegerMatrix integerMatrix = matrixFactory.sparseMatrix(
                // 在坐标 (2,3) 的位置创建一个元素 1
                new int[]{1, 2, 3},
                // 在坐标 (1,2) 的位置创建一个元素 2
                new int[]{2, 1, 2}
        );
        final DoubleMatrix doubleMatrix = matrixFactory.sparseMatrix(
                // 在坐标 (2,3) 的位置创建一个元素 1
                new double[]{1, 2, 3},
                // 在坐标 (1,2) 的位置创建一个元素 2
                new double[]{2, 1, 2}
        );
        System.out.println(integerMatrix);
        System.out.println(doubleMatrix);
    }
}


下面就是代码的计算结果

------------MatrixStart-----------
[0, 0, 0, 0]
[0, 0, 2, 0]
[0, 0, 0, 1]
------------MatrixEnd------------

------------MatrixStart-----------
[0.0, 0.0, 0.0, 0.0]
[0.0, 0.0, 2.0, 0.0]
[0.0, 0.0, 0.0, 1.0]
------------MatrixEnd------------


进程已结束,退出代码0

通过填充创建矩阵

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.DoubleMatrix;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN1
    public static void main(String[] args) {
       // 获取到矩阵工厂
        MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 填充一个 3 行 4 列的 矩阵 其中的元素为 1024
        IntegerMatrix fill1 = matrixFactory.fill(1024, 3, 4);
        // 如果数值是 double 类型,则矩阵也是 double 类型
        DoubleMatrix fill2 = matrixFactory.fill(1024.5, 3, 4);
        // 打印矩阵
        System.out.println(fill1);
        System.out.println(fill2);
    }
}

下面就是代码的执行结果

------------MatrixStart-----------
[1024, 1024, 1024, 1024]
[1024, 1024, 1024, 1024]
[1024, 1024, 1024, 1024]
------------MatrixEnd------------

------------MatrixStart-----------
[1024.5, 1024.5, 1024.5, 1024.5]
[1024.5, 1024.5, 1024.5, 1024.5]
[1024.5, 1024.5, 1024.5, 1024.5]
------------MatrixEnd------------


进程已结束,退出代码0

通过随机的方式创建矩阵

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.DoubleMatrix;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN1 {
    public static void main(String[] args) {
       // 获取到矩阵工厂
        MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 创建 3 行 4 列的 矩阵 其中的元素 随机创建 在这里设置的随机种子是 22
        IntegerMatrix integerMatrix = matrixFactory.randomGetInt(3, 4, 22);
        DoubleMatrix doubleMatrix = matrixFactory.randomGetDouble(3, 4, 22);
        // 打印矩阵
        System.out.println(integerMatrix);
        System.out.println(doubleMatrix);
    }
}


下面就是代码的运行结果

------------MatrixStart-----------
[-1150098092, 279129721, -571596271]
[1679422763, -1489264737, 551745089]
[-1724358601, -2014521070, 1780678643]
[-976397893, -375228091, -1488911514]
------------MatrixEnd------------

------------MatrixStart-----------
[0.7322219172863654, 0.8669148741814655, 0.6532535274097795]
[0.5985164721452856, 0.4145965542296267, 0.9126354106267657]
[0.2859704488730964, 0.6145075557422693, 0.9270089804071319]
[0.999587711996269, 0.563868878760328, 0.06820469035033427]
------------MatrixEnd------------

矩阵计算

矩阵数据类型在 AS 机器学习库中的使用率最高,且其提供的计算函数也是最多的。

矩阵的基本运算

AS 库中所有的操作数对象都支持的基本运算,矩阵也是支持的哦!

矩阵的加法计算
package top.lingyuzhao;

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.DoubleMatrix;

public class MAIN {

    public static void main(String[] args) {
        // 准备两个矩阵
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        final DoubleMatrix doubles = matrixFactory.parseMatrix(
                new double[]{1, 2, 3},
                new double[]{4, 5, 6},
                new double[]{7, 8, 9}
        );
        // 复制出新矩阵
        final DoubleMatrix doubles2 = matrixFactory.parseMatrix(doubles.copyToNewArrays());
        // 矩阵与整数计算
        System.out.println(doubles.add(2));
        // 矩阵与矩阵计算
        System.out.println(doubles.add(doubles2));
    }
}

下面就是计算结果

------------MatrixStart-----------
[3.0, 4.0, 5.0]
[6.0, 7.0, 8.0]
[9.0, 10.0, 11.0]
------------MatrixEnd------------

------------MatrixStart-----------
[2.0, 4.0, 6.0]
[8.0, 10.0, 12.0]
[14.0, 16.0, 18.0]
------------MatrixEnd------------
矩阵的减法计算
package top.lingyuzhao;

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.DoubleMatrix;

public class MAIN {

    public static void main(String[] args) {
        // 准备两个矩阵
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        final DoubleMatrix doubles = matrixFactory.parseMatrix(
                new double[]{1, 2, 3},
                new double[]{4, 5, 6},
                new double[]{7, 8, 9}
        );
        // 复制出新矩阵
        final DoubleMatrix doubles2 = matrixFactory.parseMatrix(doubles.copyToNewArrays());
        // 矩阵与整数计算
        System.out.println(doubles.diff(2));
        // 矩阵与矩阵计算
        System.out.println(doubles.diff(doubles2));
    }
}

下面就是计算结果

------------MatrixStart-----------
[-1.0, 0.0, 1.0]
[2.0, 3.0, 4.0]
[5.0, 6.0, 7.0]
------------MatrixEnd------------

------------MatrixStart-----------
[0.0, 0.0, 0.0]
[0.0, 0.0, 0.0]
[0.0, 0.0, 0.0]
------------MatrixEnd------------
矩阵的乘法计算
package top.lingyuzhao;

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.DoubleMatrix;

public class MAIN {

    public static void main(String[] args) {
        // 准备两个矩阵
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        final DoubleMatrix doubles = matrixFactory.parseMatrix(
                new double[]{1, 2, 3},
                new double[]{4, 5, 6},
                new double[]{7, 8, 9}
        );
        // 复制出新矩阵
        final DoubleMatrix doubles2 = matrixFactory.parseMatrix(doubles.copyToNewArrays());
        // 矩阵与矩阵计算
        System.out.println(doubles.multiply(doubles2));
    }
}

下面就是计算结果

------------MatrixStart-----------
[2.0, 3.0, 2.0, 6.0, 3.0, 6.0]
[20.0, 24.0, 20.0, 30.0, 24.0, 30.0]
[56.0, 63.0, 56.0, 72.0, 63.0, 72.0]
------------MatrixEnd------------
矩阵的内积计算
package top.lingyuzhao;

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.DoubleMatrix;

public class MAIN {

    public static void main(String[] args) {
        // 准备两个矩阵
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        final DoubleMatrix doubles = matrixFactory.parseMatrix(
                new double[]{1, 2, 3},
                new double[]{4, 5, 6},
                new double[]{7, 8, 9}
        );
        // 复制出新矩阵
        final DoubleMatrix doubles2 = matrixFactory.parseMatrix(doubles.copyToNewArrays());
        // 矩阵与矩阵计算
        System.out.println(doubles.innerProduct(doubles2));
    }
}

下面就是集散结果

285.0

矩阵的转化计算

矩阵对象,在AS库中数据非常重要的一部分,所以矩阵操作数对象的转换计算操作是非常丰富的,在这里您将可以学习到诸多的知识!

矩阵的维度重设

针对一个矩阵对象的维度,我们可以直接通过 reShape 函数将其中的矩阵维度进行重新设置,实现有效的矩阵变换操作,例如将一个 2行8列的矩阵 变成 4行4列 的矩阵,下面是一个示例。

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个 2x8 的 矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(2, 8, 22);
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里维度重设为了 4 行 4 列
        System.out.println(integerMatrix.reShape(4, 4));
    }
}


下面就是计算结果

------------MatrixStart-----------
[-1150098092, 279129721]
[-571596271, 1679422763]
[-1489264737, 551745089]
[-1724358601, -2014521070]
[1780678643, -976397893]
[-375228091, -1488911514]
[1228233692, -165598556]
[-1655677467, -63220304]
------------MatrixEnd------------

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
------------MatrixEnd------------


进程已结束,退出代码0
矩阵的子矩阵提取

在一个矩阵中,如果我们需要提取出其中的某些元素,并将这些元素勾成一个新的矩阵,就需要使用到这里的子矩阵提取操作,这样的提取效果会很棒,接下来我们就要开始进行一个演示。

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个 4x8 的 矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 8, 22);
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里 我们提取出来了(2,4) 到 (4, 8) 坐标之间的子矩阵
        // 需要注意的是,这里的坐标从 0 开始,所以每个轴都需要减一
        System.out.println(integerMatrix.extractMat(1, 3, 3, 7));
    }
}

下面就是计算结果

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
[353159284, 1767746472, 1107247833, -350820282]
[-1814378561, -1766358679, -1375246549, 1190957425]
------------MatrixEnd------------

------------MatrixStart-----------
[-165598556, -1655677467, -63220304]
[-1748391512, -1770763, -771252503]
[-435684294, 292936915, 1240741745]
[1767746472, 1107247833, -350820282]
[-1766358679, -1375246549, 1190957425]
------------MatrixEnd------------


进程已结束,退出代码0
矩阵的源子矩阵提取

此操作也是可以实现矩阵中某些元素提取的操作,值得注意的是,这样提取出来的矩阵中的修改操作将会直接作用到源矩阵中,例如从 A 中提取的 B子矩阵,B被反转之后,A也会变化,,而且此操作只可以进行提取的高度的设置,宽度无法设置,您可以参考下面的表格来查询其不同。

对照项目 非源子矩阵 源子矩阵
提取速度 较慢 较块
支持的坐标 宽,高
修改会修改父矩阵 是,修改操作会作用到源矩阵对象中
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个 4x8 的 矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 8, 22);
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里 我们提取出来了(最大宽度,4) 到 (最大宽度, 8) 坐标之间的子矩阵
        IntegerMatrix integerMatrix1 = integerMatrix.extractSrcMat(4, 8);
        System.out.println(integerMatrix1);
        // 对子矩阵进行修改 在这里是 使用不拷贝的方式 左右反转 相当于直接修改 子矩阵
        integerMatrix1.reverseLR(false);
        // 查询被修改之后的父矩阵是否有了变化
        System.out.println(integerMatrix);
    }
}

下面就是操作结果,可以看到,父矩阵虽然没有调用修改的函数,但是由于源子矩阵修改了,所以父矩阵爷就被修改了。

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
[353159284, 1767746472, 1107247833, -350820282]
[-1814378561, -1766358679, -1375246549, 1190957425]
------------MatrixEnd------------

------------MatrixStart-----------
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
[353159284, 1767746472, 1107247833, -350820282]
[-1814378561, -1766358679, -1375246549, 1190957425]
------------MatrixEnd------------

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-771252503, -1770763, -1748391512, -313494065]
[1240741745, 292936915, -435684294, -1873168937]
[-350820282, 1107247833, 1767746472, 353159284]
[1190957425, -1375246549, -1766358679, -1814378561]
------------MatrixEnd------------


进程已结束,退出代码0

矩阵的元素提取
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;


public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个 4x8 的 矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 8, 22);
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里 我们提取出来了 第1行 第3个元素
        System.out.println(integerMatrix.get(0, 2));
    }
}

下面就是打印之后的结果

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
[353159284, 1767746472, 1107247833, -350820282]
[-1814378561, -1766358679, -1375246549, 1190957425]
------------MatrixEnd------------

-571596271

进程已结束,退出代码0
矩阵到数组的转化
全矩阵源数组提取

此操作会将矩阵对象中所引用的二维数组原样获取到,这样的操作获取是非常快速的一种方式,但是这样获取的矩阵不建议修改,因为修改之后会影响源矩阵对象中的数据,因为两者指向同一个内存地址。

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

import java.util.Arrays;


public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个 4x8 的 矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 8, 22);
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里 我们提取出来了 矩阵对应的数组
        System.out.println(Arrays.deepToString(integerMatrix.toArrays()));
    }
}

下面就是运行的代码

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
[353159284, 1767746472, 1107247833, -350820282]
[-1814378561, -1766358679, -1375246549, 1190957425]
------------MatrixEnd------------

[[-1150098092, 279129721, -571596271, 1679422763], [-1489264737, 551745089, -1724358601, -2014521070], [1780678643, -976397893, -375228091, -1488911514], [1228233692, -165598556, -1655677467, -63220304], [-313494065, -1748391512, -1770763, -771252503], [-1873168937, -435684294, 292936915, 1240741745], [353159284, 1767746472, 1107247833, -350820282], [-1814378561, -1766358679, -1375246549, 1190957425]]

进程已结束,退出代码0
全矩阵非源数组提取

这样的提取结果与 源数组 提取是一样的,唯一不同的就是这样提取的数组,与源矩阵之间毫无关系,因此在这个函数获取到的数组是可以进行直接修改的,下面就是一个基本的示例。

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

import java.util.Arrays;


public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个 4x8 的 矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 8, 22);
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里 我们提取出来了 矩阵对应的数组
        System.out.println(Arrays.deepToString(integerMatrix.copyToNewArrays()));
    }
}

下面就是计算结果

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
[353159284, 1767746472, 1107247833, -350820282]
[-1814378561, -1766358679, -1375246549, 1190957425]
------------MatrixEnd------------

[[-1150098092, 279129721, -571596271, 1679422763], [-1489264737, 551745089, -1724358601, -2014521070], [1780678643, -976397893, -375228091, -1488911514], [1228233692, -165598556, -1655677467, -63220304], [-313494065, -1748391512, -1770763, -771252503], [-1873168937, -435684294, 292936915, 1240741745], [353159284, 1767746472, 1107247833, -350820282], [-1814378561, -1766358679, -1375246549, 1190957425]]

进程已结束,退出代码0
矩阵的行或列提取
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

import java.util.Arrays;

public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个 4x8 的 矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 8, 22);
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里 我们提取出来了 列索引为 0 的列,并且转换为了数组
        System.out.println(Arrays.toString(integerMatrix.getArrayByColIndex(0)));
        // 当然 也可以直接提取出 行 在这里我们还提取了行索引为 0 的行 并转换为了数组
        System.out.println(Arrays.toString(integerMatrix.getArrayByRowIndex(0)));
    }
}

下面就是代码计算之后的结果

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
[353159284, 1767746472, 1107247833, -350820282]
[-1814378561, -1766358679, -1375246549, 1190957425]
------------MatrixEnd------------

[-1150098092, -1489264737, 1780678643, 1228233692, -313494065, -1873168937, 353159284, -1814378561]
[-1150098092, 279129721, -571596271, 1679422763]

进程已结束,退出代码0
相关性维度的删除

在一些案例中,我们希望去除一些维度(例如机器学习的分类之前,需要进行数据清洗,有些不需要的维度需要被删除掉),我们通过这里的相关性维度的删除可以实现将不需要的维度,以及所有可能与其相关的维度删除。

import zhao.algorithmMagic.operands.matrix.ColumnIntegerMatrix;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;


public class MAIN1 {
    public static void main(String[] args) {
        int[][] ints = {
                new int[]{1, 2, 1, 1, 1, 1, 1, 1, 1},
                new int[]{1, 2, 1, 40, 1, 1, 60, 1, 1},
                new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9},
                new int[]{10, 20, 30, 40, 50, 60, 70, 80, 90}
        };
        // 准备一个矩阵 其中存储的是鸟的数据样本
        IntegerMatrix parse1 = ColumnIntegerMatrix.parse(
                new String[]{"1d", "2d", "3d", "4d", "5d", "6d", "7d", "8d", "9d"}, // 样本来源地区编号
                new String[]{"羽毛", "字段占位", "羽毛的颜色", "种族"}, // 样本统计的三种维度
                ints
        );
        System.out.println(parse1);
        // 开始进行特征清洗 去除掉与其中第4行 正相关系数区间达到 [0.8, 1] 的维度
        parse1 = parse1.deleteRelatedDimensions(3, 0.8, 1);
        System.out.println(parse1);
    }
}

下面就是一个计算结果

------------IntegerMatrixStart-----------
1d	2d	3d	4d	5d	6d	7d	8d	9d	rowColName
[1, 2, 1, 1, 1, 1, 1, 1, 1]	羽毛
[1, 2, 1, 40, 1, 1, 60, 1, 1]	字段占位
[1, 2, 3, 4, 5, 6, 7, 8, 9]	羽毛的颜色
[10, 20, 30, 40, 50, 60, 70, 80, 90]	种族
------------IntegerMatrixEnd------------

------------IntegerMatrixStart-----------
1d	2d	3d	4d	5d	6d	7d	8d	9d	rowColName
[1, 2, 1, 1, 1, 1, 1, 1, 1]	羽毛
[1, 2, 1, 40, 1, 1, 60, 1, 1]	字段占位
------------IntegerMatrixEnd------------


进程已结束,退出代码0
冗余维度行去除
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;


public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.parseMatrix(
                new int[]{1, 2, 3, 4},
                new int[]{5, 6, 7, 8},
                new int[]{10, 9, 8, 7},
                new int[]{10, 9, 8, 70}
        );
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里 我们清理了其中的所有 与第一行(索引为0的行) 相关的其他行
        System.out.println(integerMatrix.featureSelection(
                // 这里设置了需要去除的比例 我们要去除掉其中的 50% 会按照离散值算法
                // 将最没有特点的行去除,最后返回的一定是原本的 50% 因为我们去除掉了 百分之50
                0.5
        ));
    }
}

下面就是计算出来的结果

------------MatrixStart-----------
[1, 2, 3, 4]
[5, 6, 7, 8]
[10, 9, 8, 7]
[10, 9, 8, 70]
------------MatrixEnd------------

------------MatrixStart-----------
[10, 9, 8, 70]
[10, 9, 8, 7]
------------MatrixEnd------------


进程已结束,退出代码0
矩阵扁平化

此操作能够将一个矩阵压扁成只有一行的数组,在下面就是一个具体的示例。

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

import java.util.Arrays;


public class MAIN1 {
    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        // 通过 工厂类 创建一个 矩阵对象
        final IntegerMatrix integerMatrix = matrixFactory.parseMatrix(
                new int[]{1, 2, 3, 4},
                new int[]{5, 6, 7, 8},
                new int[]{10, 9, 8, 7},
                new int[]{10, 9, 8, 70}
        );
        // 打印出矩阵对象
        System.out.println(integerMatrix);
        // 打印出转化后的矩阵 在这里 我们将矩阵扁平化为了一个数组
        System.out.println(Arrays.toString(integerMatrix.flatten()));
    }
}

下面就是扁平化之后的结果

------------MatrixStart-----------
[1, 2, 3, 4]
[5, 6, 7, 8]
[10, 9, 8, 7]
[10, 9, 8, 70]
------------MatrixEnd------------

[1, 2, 3, 4, 5, 6, 7, 8, 10, 9, 8, 7, 10, 9, 8, 70]

进程已结束,退出代码0
矩阵元素镜像反转
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN {

    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(2, 3, 22);
        // 先打印一下矩阵
        System.out.println(integerMatrix);
        // 打印一下矩阵反转之后的结果 
        // 这个反转函数具有一个拷贝参数,您如果想要在源矩阵对象进行修改的话,需要传入 false 代表不需要进行拷贝
        // 左右反转
        System.out.println(integerMatrix.reverseLR(true));
        // 上下反转
        System.out.println(integerMatrix.reverseBT(true));
    }

}

下面就是代码的运行结果

------------MatrixStart-----------
[-1150098092, 279129721]
[-571596271, 1679422763]
[-1489264737, 551745089]
------------MatrixEnd------------

------------MatrixStart-----------
[279129721, -1150098092]
[1679422763, -571596271]
[551745089, -1489264737]
------------MatrixEnd------------

------------MatrixStart-----------
[-1150098092, 279129721]
[-571596271, 1679422763]
[-1489264737, 551745089]
------------MatrixEnd------------


进程已结束,退出代码0

矩阵元素位移

矩阵中包含很多的元素,我们可以让每行的元素统一的向左或向右移动,下面是一个实际演示示例。

package com.zhao;

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN {

    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 6, 22);
        // 先打印一下矩阵
        System.out.println(integerMatrix);
        // 打印一下矩阵左移2位的结果 
        // 第二个参数代表的就是 是否需要拷贝 如果不需要 则转换操作将会直接作用在原矩阵
        System.out.println(integerMatrix.leftShift(2, true));
        // 再打印一下矩阵右移2位的结果
        System.out.println(integerMatrix.rightShift(2, true));
    }

}

下面就是代码的运行结果

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
------------MatrixEnd------------

------------MatrixStart-----------
[-571596271, 1679422763, 0, 0]
[-1724358601, -2014521070, 0, 0]
[-375228091, -1488911514, 0, 0]
[-1655677467, -63220304, 0, 0]
[-1770763, -771252503, 0, 0]
[292936915, 1240741745, 0, 0]
------------MatrixEnd------------

------------MatrixStart-----------
[0, 0, -1150098092, 279129721]
[0, 0, -1489264737, 551745089]
[0, 0, 1780678643, -976397893]
[0, 0, 1228233692, -165598556]
[0, 0, -313494065, -1748391512]
[0, 0, -1873168937, -435684294]
------------MatrixEnd------------


进程已结束,退出代码0

洗牌打乱矩阵
package com.zhao;

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

public class MAIN {

    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 6, 22);
        // 先打印一下矩阵
        System.out.println(integerMatrix);
        // 打印一下洗牌之后的矩阵 在这里设置打乱算法随机种子为 22
        System.out.println(integerMatrix.shuffle(22));
    }

}

下面就是代码的运行结果

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
------------MatrixEnd------------

------------MatrixStart-----------
[-1489264737, 551745089, -1724358601, -2014521070]
[1228233692, -165598556, -1655677467, -63220304]
[-1150098092, 279129721, -571596271, 1679422763]
[1780678643, -976397893, -375228091, -1488911514]
[-1873168937, -435684294, 292936915, 1240741745]
[-313494065, -1748391512, -1770763, -771252503]
------------MatrixEnd------------


进程已结束,退出代码0

矩阵的行迭代

矩阵实现了 Java 中的迭代器接口,可以直接使用Java中的增强 for 来进行迭代,增强for的语法为for (类型 参数名字 : 被迭代的对象) 这个样子,接下来就是实际演示。

import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.MatrixFactory;
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;

import java.util.Arrays;

public class MAIN {

    public static void main(String[] args) {
        final MatrixFactory matrixFactory = AlgorithmStar.matrixFactory();
        final IntegerMatrix integerMatrix = matrixFactory.randomGetInt(4, 6, 22);
        // 先打印一下矩阵
        System.out.println(integerMatrix);
        // 开始迭代矩阵
        for (int[] matrix : integerMatrix) {
            System.out.println(Arrays.toString(matrix));
        }
    }

}

下面就是代码的运行结果

------------MatrixStart-----------
[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]
------------MatrixEnd------------

[-1150098092, 279129721, -571596271, 1679422763]
[-1489264737, 551745089, -1724358601, -2014521070]
[1780678643, -976397893, -375228091, -1488911514]
[1228233692, -165598556, -1655677467, -63220304]
[-313494065, -1748391512, -1770763, -771252503]
[-1873168937, -435684294, 292936915, 1240741745]

进程已结束,退出代码0

最近更新

  1. TCP协议是安全的吗?

    2024-04-04 05:50:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-04 05:50:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-04 05:50:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-04 05:50:02       20 阅读

热门阅读

  1. 【图像处理小知识】PIL Image 中的P和L模式

    2024-04-04 05:50:02       19 阅读
  2. Ubuntu终端多窗口分屏Terminator优化

    2024-04-04 05:50:02       12 阅读
  3. Centos7、ubuntu22.04.3安装php7.4,mysql8.0

    2024-04-04 05:50:02       26 阅读
  4. 中文bert预训练

    2024-04-04 05:50:02       13 阅读
  5. 【C++】编程规范之表达式原则

    2024-04-04 05:50:02       17 阅读
  6. 算法练习----力扣每日一题------6

    2024-04-04 05:50:02       14 阅读