如何减少CSV文件的尺寸?

问题描述 投票:0回答:1

假设我有一个尺寸为 m×n 的 CSV 文件,表示 m 行和 n 列。我想通过替换相应子矩阵的平均值来减少其维度。


Example 1:

Given we have 6×6 matrix (CSV file):
 
col1,col2,col3,col4,col5,col6

a1,b1,c1,d1,e1, f1

a2,b2,c2,d2,e2, f2

a3,b3,c3,d3,e3, f3

a4,b4,c4,d4,e4, f4

a5,b5,c5,d5,e5, f5

a6,b6,c6,d6,e6, f6

If we want 2×2 matrix, then resultant CSV file should be below:

col1, col2
a',  d'
a", d"

Where a'=(a1+a2+a3+b1+b2+b3+c1+c2+c3)/9
a"=(a4+a5+a6+b4+b5+b6+c4+c5+c6)/9
d'=(d1+d2+d3+e1+e2+e3+f1+f2+f3)/9
d"=(d4+d5+d6+e4+e5+e6+f4+f5+f6)/9

Example:2

Given we have 5×6 matrix (CSV file):

col1,col2,col3,col4,col5,col6

a1,b1,c1,d1,e1, f1

a2,b2,c2,d2,e2, f2

a3,b3,c3,d3,e3, f3

a4,b4,c4,d4,e4, f4

a5,b5,c5,d5,e5, f5



If we want 2×2 matrix, then resultant CSV file should be below:

col1, col2
a',  d'
a", d"

Where a'=(a1+a2+a3+b1+b2+b3+c1+c2+c3)/9

a"=(a4+a5+b4+b5+c4+c5)/6

d'=(d1+d2+d3+e1+e2+e3+f1+f2+f3)/9

d"=(d4+d5+e4+e5+f4+f5)/6

Example 3:

Given we have 6×5 matrix (CSV file):

col1,col2,col3,col4,col5,col6

a1,b1,c1,d1,e1

a2,b2,c2,d2,e2

a3,b3,c3,d3,e3

a4,b4,c4,d4,e4

a5,b5,c5,d5,e5

a6,b6,c6,d6,e6

If we want 2×2 matrix, then resultant CSV file should be below:

col1, col2
a',  d'
a", d"

Where a'=(a1+a2+a3+b1+b2+b3+c1+c2+c3)/9

a"=(a4+a5+a6+b4+b5+b6+c4+c5+c6)/9

d'=(d1+d2+d3+e1+e2+e3)/6

d"=(d4+d5+d6+e4+e5+e6)/6

我想要Python代码,它可以通过将所有子矩阵之和的平均值来减少维度。例如,在示例1中,我们给定了6×6矩阵,我们想要2×2矩阵,因此我们考虑(6÷2)×(6÷2)=3×3子矩阵并计算3×3矩阵的9个元素的平均值,它是合成的 2×2 矩阵的一个元素,依此类推。

在示例2中,如果给定维度不是结果维度的倍数,我们使用上限函数。

python-3.x pandas dataframe
1个回答
0
投票

要解决这个问题,您需要通过计算子矩阵值的平均值来减小给定矩阵的大小。这是一个 Python 解决方案,它将从 CSV 文件中获取任何 m×n 矩阵,将其减小到指定大小,并计算子矩阵的平均值,必要时使用上限值函数。

您可以使用 numpy 和 pandas 来高效地处理矩阵运算。

这里是减少矩阵维数的Python代码:

import numpy as np

将 pandas 导入为 pd 导入数学

def reduce_matrix(矩阵, new_rows, new_cols): ”“” 通过对子矩阵进行平均,将矩阵的维度减少到 (new_rows, new_cols)。

Parameters:
matrix (2D numpy array): The original matrix to be reduced.
new_rows (int): The number of rows in the reduced matrix.
new_cols (int): The number of columns in the reduced matrix.

Returns:
2D numpy array: The reduced matrix.
"""
old_rows, old_cols = matrix.shape
# Determine the size of submatrices
row_block_size = math.ceil(old_rows / new_rows)
col_block_size = math.ceil(old_cols / new_cols)

reduced_matrix = np.zeros((new_rows, new_cols))

for i in range(new_rows):
    for j in range(new_cols):
        # Determine the range of rows and columns for the submatrix
        row_start = i * row_block_size
        row_end = min((i+1) * row_block_size, old_rows)
        col_start = j * col_block_size
        col_end = min((j+1) * col_block_size, old_cols)
        
        # Extract the submatrix
        sub_matrix = matrix[row_start:row_end, col_start:col_end]
        
        # Calculate the average and store it in the reduced matrix
        reduced_matrix[i, j] = np.mean(sub_matrix)

return reduced_matrix

def process_csv(input_csv,new_rows,new_cols,output_csv): ”“” 读取 CSV 文件,减少其矩阵维度,并将结果保存到另一个 CSV。

Parameters:
input_csv (str): The path to the input CSV file.
new_rows (int): The number of rows in the reduced matrix.
new_cols (int): The number of columns in the reduced matrix.
output_csv (str): The path to the output CSV file.
"""
# Read the CSV into a DataFrame
df = pd.read_csv(input_csv)

# Convert the DataFrame to a numpy array
matrix = df.values

# Reduce the matrix
reduced_matrix = reduce_matrix(matrix, new_rows, new_cols)

# Convert the reduced matrix back to a DataFrame
reduced_df = pd.DataFrame(reduced_matrix, columns=[f'col{i+1}' for i in range(new_cols)])

# Save the reduced DataFrame to the output CSV
reduced_df.to_csv(output_csv, index=False)

使用示例:

process_csv('输入.csv', 2, 2, '输出.csv')

说明: 减少矩阵(矩阵,新行,新列):

该函数以矩阵作为输入,通过将矩阵划分为子矩阵并计算每个子矩阵的平均值,将其减少到指定的维度。 我们使用上限函数 (math.ceil) 来处理矩阵维度不能被所需维度整除的情况。 process_csv(输入_csv,新_行,新_列,输出_csv):

此函数读取输入的 CSV 文件,将其转换为矩阵(使用 pandas),应用 reduce_matrix 函数,并将缩减后的矩阵写回新的 CSV 文件。 子矩阵计算:

根据新旧维度商的上限将原始矩阵划分为大小为(row_block_size,col_block_size)的块。 对于每个块,计算平均值并将其插入到简化矩阵中的相应位置。 例子: 对于 6x6 的矩阵并需要缩减为 2x2,此函数将:

将矩阵分解为 3x3 子矩阵并计算每个子矩阵的平均值。 如果矩阵不能完全整除,它将使用最后一行或最后一列中的剩余元素并相应地计算平均值。 用法: 要将 6x6 矩阵的 CSV 文件减少到 2x2,您可以调用:

process_csv('input.csv', 2, 2, 'output.csv')

这将使用简化后的矩阵创建一个新的output.csv 文件。

© www.soinside.com 2019 - 2024. All rights reserved.