Monday 15 April 2013

python - How to make NumPy create a matrix with Strings and floats -


OK, I've done a lot of research on this subject, and I know that NumPy only supports identical matrix .

I am working with Python with NLTK package to deal with some fundamental linguistic data, and just want to create a matrix with different strings as 'column names' and actual data values (Floats) as the rest of the matrix.

So far, I have made two matrix, one with strings and one floats, and used vstack to keep on top of each other, until I am the 'matrix' of this new vertical matrix Trying to use the savetxt () method of NumPy with all was fine and dandy, but because matrix 'like matrix' it is not homogeneous that is not in it, it will not write csv file. FML

I really want to be able to use NumPy for all the awesome methods to deal with real data points, but I can not find a weird array of strings, one at the top of the matrix. Any idea to convert to Csv?

Import OS .path import nltk.probability Import from nltk.corpus.reader Import ConditionalFreqDist import CHILDESCorpusReader from numpy import * sys import nltk, freqDist n_rows = 12 n_cols = 19 init_row = 0 init_col = 0 neg_words = ["age", "mlu" "all tokens", "no", "no", "no", "no", "will not", "is not", "no "," No "," should not be matrix_headers = array ('t'), "could not be", "no", "no", "not", "not", "no", "no"] Range (lane (neg_words), dtype = 'A12') Matrix_values ​​= Zero (n_rows * n_cols) .reshape ((n_rows, n_cols)) #the matrix with data points (for entering the range Literacy) (lane (neg_words)): Matrix_headers [enter] = neg_words [sign] P = neg_words q = Matrix_values ​​matrix = vstack ([p, q]) out_name = "/ Users / nicholasmoores / Documents / Research / neg_table. Csv "savetxt (out_name, matrix, fmt = '% .3e', delimiter =" \ t ") raw_input (" \ n \ nEnter the key to exit the form. ")

you

example:

  & gt; & Gt; & Gt; Ym = np.zeros (lane (neg_words), dtype = [('head', 'a14'), ('vals', 'f4', (n_rows,))] array ([(, '', [0.0] , 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]), ('', [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)), ('', [0.0, 0.0, 0.0, 0.0, 0.0] 0.0, 0.0, 0.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'), (' Wall ',' & lt; F4 ', (12,))]   

To set the header value:

  ym [' head '] = neg_words  < / Pre> 

Headers to access:

  & gt; & Gt; & Gt; Ym [ 'head'] Array ([ 'age', 'Melu', 'all the tokens', 'no', 'no', 'no', 'no', 'no', "" no ""   

Similarly, the values ​​of "no", "will not", "should not be", "no", "no", "no", "no", "no" To use

  ym ['wal']    

No comments:

Post a Comment