GVKun编程网logo

用numpy将csv加载到二维矩阵中进行绘图(numpy csv)

24

本文将为您提供关于用numpy将csv加载到二维矩阵中进行绘图的详细介绍,我们还将为您解释numpycsv的相关知识,同时,我们还将为您提供关于01二维矩阵中最大全为1的正方形maxSquare——经

本文将为您提供关于用numpy将csv加载到二维矩阵中进行绘图的详细介绍,我们还将为您解释numpy csv的相关知识,同时,我们还将为您提供关于01 二维矩阵中最大全为 1 的正方形 maxSquare—— 经典 DP 问题(二维)、1d numpy 数组到矩阵中的块、matlab三维矩阵如何提取二维矩阵、MPI_GATHERV (Fortran) 从二维子矩阵创建一个新的二维矩阵的实用信息。

本文目录一览:

用numpy将csv加载到二维矩阵中进行绘图(numpy csv)

用numpy将csv加载到二维矩阵中进行绘图(numpy csv)

鉴于此CSV文件:

"A","B","C","D","E","F","timestamp"611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291111964948E12611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291113113366E12611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291120650486E12

我只是想将其加载为3行7列的矩阵/ ndarray。但是,由于某种原因,我能从numpy中脱颖而出的是一个具有3行(每行一个)且没有列的ndarray。

r = np.genfromtxt(fname,delimiter='','',dtype=None, names=True)print rprint r.shape[ (611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.22776999999996, 1291111964948.0) (611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.22776999999996, 1291113113366.0) (611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.22776999999996, 1291120650486.0)](3,)

我可以手动迭代并将其修改为所需的形状,但这似乎很愚蠢。我只想将其加载为适当的矩阵,以便可以像在matlab中一样将其切成不同的维度并进行绘制。

答案1

小编典典

纯麻木

numpy.loadtxt(open("test.csv", "rb"), delimiter=",", skiprows=1)

请查阅loadtxt文档。

您还可以使用python的csv模块:

import csvimport numpyreader = csv.reader(open("test.csv", "rb"), delimiter=",")x = list(reader)result = numpy.array(x).astype("float")

您将不得不将其转换为您喜欢的数字类型。我想您可以将全部内容写成一行:

结果= numpy.array(list(csv.reader(open(“ test.csv”,“ rb”),delimiter =“,”))))。astype(“ float”)

新增提示:

您还可以使用pandas.io.parsers.read_csv并获取关联的numpy数组,该数组可以更快。

01 二维矩阵中最大全为 1 的正方形 maxSquare—— 经典 DP 问题(二维)

01 二维矩阵中最大全为 1 的正方形 maxSquare—— 经典 DP 问题(二维)

在一个二维 01 矩阵中找到全为 1 的最大正方形

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0


以矩阵中每一个点作为正方形右下角点来处理,而以该点为右下角点的最大边长最多比以它的左方、上方和左上方为右下角的正方形边长多1,所以这时只能取另外三个正方形中最小的正方形边长+1。用d[i][j]表示以i,j坐标为右下角的正方形最大边。则有状态转移方程:dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1])) + 1,具体代码如下:

 public static int maxSquare(int[][] matrix) {
        if (matrix.length==0||matrix[0].length==0) {
            return 0;
        }
        int M = matrix.length, N = matrix[0].length, res = 0;
        int[][] dp = new int[M][N];
        for (int i=0; i<M; i++) {
            if (matrix[i][0] == 1) {
                dp[i][0] = 1;
                res = 1;
            }
        }

        for (int j=0; j<N; j++) {
            if (matrix[0][j] == 1) {
                dp[0][j] = 1;
                res = 1;
            }
        }

        for (int i=1; i<M; i++) {
            for (int j=1; j<N; j++) {
                if (matrix[i][j] == 1) {
                    dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1])) + 1;
                }
                res = max(res, dp[i][j]);
            }
        }
        return res;
    }

采用动态规划方法正向依次利用之前存储的状态计算出下一个状态值,从而避免了重复计算,大大提升了时间复杂度。

1d numpy 数组到矩阵中的块

1d numpy 数组到矩阵中的块

这样做:

In [327]: a = np.array([0,1,...:  0,0])
     ...: 
In [328]: a.reshape(3,3,2,2)
Out[328]: 
array([[[[0,0],[0,0]],[[0,0]]],[[[0,[[1,1],[1,1]],0]]]])

1s 的 (2,2) 块是可见的,但我需要切换中间的 2 个维度:

In [329]: a.reshape(3,2).transpose(0,3)
Out[329]: 
array([[[[0,0]]]])

现在回到二维:

In [330]: a.reshape(3,3).reshape(6,6)
Out[330]: 
array([[0,0]])

reshape、transpose、reshape的基本步骤是这类问题的通病。转置细节非常。

matlab三维矩阵如何提取二维矩阵

matlab三维矩阵如何提取二维矩阵

从三维矩阵中提取二维矩阵的方法是:使用索引运算符提取:提取整个二维矩阵:matrix_2d = matrix_3d(:, :, idx)提取特定行:matrix_2d = matrix_3d(row_idx, :, :)提取特定列:matrix_2d = matrix_3d(:, col_idx, :)使用squeeze函数:matrix_2d = squeeze(matrix_3d)

matlab三维矩阵如何提取二维矩阵

如何在 MATLAB 中从三维矩阵提取二维矩阵

问题: 如何从三维矩阵中提取二维矩阵?

回答: 从三维矩阵中提取二维矩阵,可以使用 squeeze 函数或索引运算符。

方法:

1. 索引运算符:

  • 提取整个二维矩阵: matrix_2d = matrix_3d(:, :, idx),其中 idx 是要提取的二维矩阵的索引。
  • 提取特定行: matrix_2d = matrix_3d(row_idx, :, :),其中 row_idx 是要提取的行索引。
  • 提取特定列: matrix_2d = matrix_3d(:, col_idx, :),其中 col_idx 是要提取的列索引。

2. squeeze 函数:

  • matrix_2d = squeeze(matrix_3d):将三维矩阵中的所有单维度维度删除,从而生成一个二维矩阵。

示例:

考虑三维矩阵 matrix_3d:

matrix_3d = [
    1  2  3
    4  5  6
    7  8  9
];
登录后复制
  • 要提取第一层,使用索引运算符:matrix_2d = matrix_3d(:, :, 1)。
  • 要提取第二列,使用索引运算符:matrix_2d = matrix_3d(:, 2, :)。
  • 要将三维矩阵转换为二维矩阵,使用 squeeze 函数:matrix_2d = squeeze(matrix_3d)。

以上就是matlab三维矩阵如何提取二维矩阵的详细内容,更多请关注php中文网其它相关文章!

MPI_GATHERV (Fortran) 从二维子矩阵创建一个新的二维矩阵

MPI_GATHERV (Fortran) 从二维子矩阵创建一个新的二维矩阵

如何解决MPI_GATHERV (Fortran) 从二维子矩阵创建一个新的二维矩阵?

我正在尝试将行数不同但列数相同的子二维数组收集到全局二维数组中。例如,假设使用 2 个 MPI 进程,第一个进程(即 rank == 0)有:

local = [11,12,13,14]

,第二个过程(即 rank == 1)有:

local = [21,22,23,24
         31,32,33,34]

然后,我想将这两个数组连接成一个二维数组:

global = [11,14
          21,24
          31,34]

由于每个“本地”数组都有不同的行数,我(可能)想使用 mpi_gatherv(或 mpi_allgatherv)。我在这里发现了相同的问题:Using Gatherv for 2d Arrays in Fortran 和 Using MPI_gatherv to create a new matrix from other smaller matrices,但我还是不太明白。所以,请教我。这是我的示例代码:

program main
use mpi
implicit none
   
integer :: i,j
integer :: rank,npro,ierr
integer,allocatable :: local(:,:)
integer,allocatable :: global(:,:),displs(:),counts(:)
integer :: loc_size(2),glob_size(2),starts(2) 
integer :: newtype,int_size,resizedtype
integer(kind=MPI_ADDRESS_KIND) :: extent,begin
! End of local variables ==================================================!

call MPI_Init(ierr)
call MPI_Comm_rank(MPI_COMM_WORLD,rank,ierr)
call MPI_Comm_size(MPI_COMM_WORLD,ierr)

! I will set local 2D arrays as: [1,4] for rank #0,and [2,4] for rank #1
! then,the global 2D array will be [3,4] (assuming I use 2 processes)
loc_size  = [rank+1,4]      ! [1,4],[2,4]
glob_size = [3,4]           ! I will use npro = 2

! allocate local and global arrays
allocate(local(loc_size(1),loc_size(2))) ! [1,4]
allocate(global(glob_size(1),glob_size(2)))! [3,4] ! if npro = 2

! set "local" array
!       rank = 0: [11,14]
!       rank = 1: [21,24
!                  31,34]
if(rank == 0) then
   do j=1,4
      local(1,j) = 10 + j ! [11,14]
   end do
else if(rank == 1) then
   do i=1,2
      do j=1,4
         local(i,j) = (i+1)*10 + j ! [21,24; 31,34]
      end do
   end do
end if


! create a 2D subarray and set as "newtype" 
starts    = [0,0]              ! array start location
call MPI_Type_create_subarray(2,glob_size,loc_size,starts,&
&                             MPI_ORDER_FORTRAN,MPI_INTEGER,&
&                             newtype,ierr)

! get MPI_INTEGER type size in byte
! I don''t quite understand the following processes...
! So,please comment on each step if possible...
call MPI_Type_size(MPI_INTEGER,ierr)
begin  =  0
extent =  (rank+1) * int_size ! rank 0 = 4 byte; rank 1 = 8 byte (am I doing correct here?)
call MPI_Type_create_resized(newtype,begin,extent,resizedtype,ierr) ! I dont'' quite understand this process
call MPI_Type_commit(resizedtype,ierr)

! allocate index for mpi_gatherv
allocate(displs(npro)) ! [2],index for mpi_gatherv
allocate(counts(npro)) ! [2],index for mpi_gatherv

counts = [1,1]
do i =  1,npro
   displs(i) = (i-1) ! [0,1]
end do

call MPI_Gatherv(local,1,&
&                global,counts,displs,&
&                0,MPI_COMM_WORLD,ierr)

if(rank == 0) then
   do i=1,3
      write(*,*) (global(i,j),j=1,4)
   end do
end if

call MPI_Finalize(ierr)
end program main

提前致谢。

解决方法

我认为如果您更改存储顺序会容易得多(即具有 rank "i" 初始化固定长度的 "i+1" 列),但以下代码似乎适用于您当前拥有的内容。我打开了调试输出,将列数更改为 4,在 3 个进程上运行(因此全局行数 = 1+2+3 = 6)并确保使用唯一数据初始化本地数组。

重要的一点是,您需要为每个等级使用不同的发送类型,因为步幅不同(因为本地数组的维度不同)。也许有更简单的方法来做到这一点(不改变存储顺序),但至少这似乎有效。

请注意,注释不再与实际代码相关!

program main
use mpi
implicit none
   
integer :: i,j
integer :: rank,npro,ierr
integer,allocatable :: local(:,:)
integer,allocatable :: global(:,:),displs(:),counts(:)
integer :: loc_size(2),glob_size(2),starts(2) 
integer :: newtype,int_size,stype,resizedstype,rtype,resizedrtype
integer(kind=MPI_ADDRESS_KIND) :: extent,begin
! End of local variables ==================================================!

call MPI_Init(ierr)
call MPI_Comm_rank(MPI_COMM_WORLD,rank,ierr)
call MPI_Comm_size(MPI_COMM_WORLD,ierr)

! I will set local 2D arrays as: [1,3] for rank #0,and [2,3] for rank #1
! then,the global 2D array will be [3,3] (assuming I use 2 processes)
loc_size  = [rank+1,4]      ! [1,3],[2,3]
glob_size = [6,4]           ! I will use npro = 3

! allocate local and global arrays
allocate(local(loc_size(1),loc_size(2))) ! [1,3]
allocate(global(glob_size(1),glob_size(2)))! [3,3] ! if npro = 2

! set "local" array
!       rank = 0: [0,0]
!       rank = 1: [1,1,1
!                  1,1]
do i=1,rank+1
   do j=1,4
      local(i,j) = 10*rank+4*(i-1)+j
   end do
end do

! check the local array 
 do i=1,rank+1
    write(*,*) ''rank = '',''local = '',(local(i,j),j=1,4)
 end do

! create a 2D subarray and set as send type stype 
loc_size= [1,4]
starts    = [0,0]              ! array start location
glob_size=[rank+1,4]
call MPI_Type_create_subarray(2,glob_size,loc_size,starts,&
&                             MPI_ORDER_FORTRAN,MPI_INTEGER,&
&                             stype,ierr)

! get MPI_INTEGER type size in byte
call MPI_Type_size(MPI_INTEGER,ierr)
begin  =  0
extent =  int_size

call MPI_Type_create_resized(stype,begin,extent,ierr)
call MPI_Type_commit(resizedstype,ierr)

 ! create a 2D subarray and set as receive type rtype 
loc_size=[1,0]              ! array start location
glob_size=[6,&
&                             rtype,ierr)

! get MPI_INTEGER type size in byte
! I don''t quite understand the following processes...
! So,please comment on each step if possible...
call MPI_Type_size(MPI_INTEGER,ierr)
begin  =  0
extent =  int_size

call MPI_Type_create_resized(rtype,resizedrtype,ierr)
call MPI_Type_commit(resizedrtype,ierr)

! allocate index for mpi_gatherv
allocate(displs(npro)) ! [2],index for mpi_gatherv
allocate(counts(npro)) ! [2],index for mpi_gatherv

counts = [1,2,3]
displs = [0,3]
call MPI_Gatherv(local,rank+1,&
&                global,counts,displs,&
&                0,MPI_COMM_WORLD,ierr)

if(rank == 0) then
   do i=1,6
      write(*,*) (global(i,4)
   end do
end if

call MPI_Finalize(ierr)
end program main

如果我运行 3 个进程,我会得到合理的结果:

 rank =            0 local =            1           2           3           4
 rank =            1 local =           11          12          13          14
 rank =            1 local =           15          16          17          18
 rank =            2 local =           21          22          23          24
 rank =            2 local =           25          26          27          28
 rank =            2 local =           29          30          31          32
           1           2           3           4
          11          12          13          14
          15          16          17          18
          21          22          23          24
          25          26          27          28
          29          30          31          32

关于用numpy将csv加载到二维矩阵中进行绘图numpy csv的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于01 二维矩阵中最大全为 1 的正方形 maxSquare—— 经典 DP 问题(二维)、1d numpy 数组到矩阵中的块、matlab三维矩阵如何提取二维矩阵、MPI_GATHERV (Fortran) 从二维子矩阵创建一个新的二维矩阵等相关知识的信息别忘了在本站进行查找喔。

本文标签: