我有一个动态多维数组,每次可以有不同数量的列。要求用户选择从具有 N 列的文件中提取哪些列,并根据此数字创建多维数组“ARRAY_VALUES”。
import numpy as num
DIRECTORY = '/Users/user/Desktop/'
DATA_DIC_FILE = "%sOUTPUT_DIC/OUTPUT_DICTIONARIES.txt" %(DIRECTORY)
Choice = str( raw_input( 'Which columns do you want to use (separated by a comma):\t' ) ).split(',')
# Input something like this: 1,2,3,4
String_choice = []
PCA_INDEX = []
Columns = len(Choice)
PCA_INDEX = {}
# PCA_INDEX is a dictionary that the key is a string whose value is a float number.
PCA_INDEX['any_string'] = float_number # The dictionary has about 50 entries.
ARRAY_VALUES = [ [] for x in xrange( Columns) ]
""" Creating the N-dimensional array that will contain the data from the file """
""" This list has the form ARRAY_VALUES = [ [], [], [], ... ] for n-repetitions. """
ARRAY_VALUES2 = ARRAY_VALUES
lines = open( DATA_DIC_FILE ).readlines() #Read lines from the file
for i in range( 0, len(ARRAY_VALUES) ):
ARRAY_VALUES[i] = num.loadtxt( fname = DATA_DIC_FILE, comments= '#', delimiter=',', usecols = [ int( PCA_INDEX[i] ) ], unpack = True )
""" This saves the lists from the file to the matrix 'ARRAY_VALUES' """
现在我已经有了用于 n 列的 ARRAY_VALUES = [[], [], ...] 形式的多维数组。
如果任何值是'inf',我想从每一列中消除相应的行。我尝试使用以下代码,但我不知道如何使其动态列数:
for j in range(0, len(ARRAY_VALUES)):
for i in range(0, len(ARRAY_VALUES[0])):
if num.isinf( ARRAY_VALUES[j][i] ) or num.isinf( ARRAY_VALUES[]): # This is where the problem is.
# if num.isinf( ARRAY_VALUES[0][i] ) or num.isinf(ARRAY_VALUES[1][i] or ... num.isinf(ARRAY_VALUES[last_column][i]:
continue
else:
ARRAY_VALUES2[j].append( ARRAY_VALUES[j][i] ) #Save the values into ARRAY_VALUES2.
谁能帮助我并告诉我如何做这部分:
# if num.isinf( ARRAY_VALUES[0][i] ) or num.isinf(ARRAY_VALUES[1][i] or ... num.isinf(ARRAY_VALUES[last_column][i]:
对于具有 n 列的多维数组,因此输出如下所示:
ARRAY_VALUES = [ [8, 2, 3 , inf, 5],
[1, 9, inf, 4 , 5],
[7, 2, inf, inf, 6] ]
ARRAY_VALUES2 = [ [8, 2, 5],
[1, 9, 5],
[7, 2, 6] ]
- 谢谢!