更改矩阵内的数字

时间:2018-02-13 16:07:28

标签: python python-3.x numpy matrix

我随机生成一个矩阵。为简单起见,我们假设它采用以下形式import csv with open('file.csv') as csvDataFile: csvReader = csv.reader(csvDataFile) # This skips the first row of the CSV file. next(csvReader) for row in csvReader: y = ('([division].[' + row[3] + '], [revenue_type].[' + row[4] + '], [week].[' + row[2] + '],[quarter].[' + row[0] + 'q' + row[1] + '])') print(y) x = ('}') print (x) -Example of output that I get now would be: ([division].[26], [revenue_type].[T], [week].[4],[quarter].[2018q1]) ([division].[45], [revenue_type].[S], [week].[4],[quarter].[2017q2]) ([division].[26], [revenue_type].[T], [week].[4],[quarter].[2016q3]) } -Example of output that I am trying to get: ([division].[26], [revenue_type].[T], [week].[4],[quarter].[2018q1]), ([division].[45], [revenue_type].[S], [week].[4],[quarter].[2017q2]), ([division].[26], [revenue_type].[T], [week].[4],[quarter].[2016q3]) }

np.shape(A) = (2,4)

然后,我估计以下表达式:

import numpy as np
A:

matrix([[ 1,  2,  3,  4],
    [ 3,  4, 10,  8]])

问题是如何输入以下限制:如果矩阵import numpy as np K = 3 I = 4 C0 = np.sum(np.maximum(A[-1] - K, 0)) / I 中的任何数量的列小于或等于(< =)K(3),则更改最后一个数字该列为零?所以基本上,我的矩阵应该转变为:

A

1 个答案:

答案 0 :(得分:0)

这是一种方式。

A[-1][np.any(A <= 3, axis=0)] = 0

# matrix([[1, 2, 3, 4],
#         [0, 0, 0, 8]])

A[-1][np.any((A > 2) & (A <= 3), axis=0)] = 0

# matrix([[1, 2, 3, 4],
#         [0, 4, 0, 8]])