i'm having trouble with 3.4 using numpy. My question is to know how can i have a numpy matrix with plain string format instead byte-string.
def res(data):
M = np.zeros(data.shape).astype(dtype='|S20')
lines,columns = M.shape
for l in range(lines):
M[l][0] = data[l][1]
M[l][1] = data[l][2]
M[l][2] = data[l][3]
return M
**result python2.7**
[['Ann' '38.72' '-9.133']
['John' '55.68' '12.566']
['Richard' '52.52' '13.411']
['Alex' '40.42' '-3.703']]
**result python3.4**
[[b'Ann' b'38.72' b'-9.133']
[b'John' b'55.68' b'12.566']
[b'Richard' b'52.52' b'13.411']
[b'Alex' b'40.42' b'-3.703']]
In Python3.4 How can i have my Matrix in plain string like in example for python2.7 this is bad because i have functions that expect string values and not byte-strings.
Any help would be great. thanks
in my case the solution were simply to change dtype('|S20') to dtype(str)..I hope this help.
Related
Suppose I have a numpy array like -
A = ['83.56%' '2.74%' '2.74%' '4.11%' '4.11%' '19.18%' '76.71%' '20.55%'
'34.25%' '54.79%']
and I want to split this array as integers array only like -
B = ['83.56' '2.74' '2.74' '4.11' '4.11' '19.18' '76.71' '20.55'
'34.25' '54.79']
How should I do it using Python codes ?
Use-case for str.rstrip:
B = [item.rstrip('%') for item in A]
I need to convert a piece of MATLAB code to Python and I'm bad at both. The code in MATLAB uses fft and fftshift. I tried to use NumPy in Python. The code runs but when I compare the outcome they are not matching. I appreciate your help.
Here is the MATLAB code:
h(1,1:Modes_number) = -1i*S;
hfft = fft(h);
hft0 = fftshift(hfft);
and here is the Python code which I wrote:
h = np.zeros((1,self.cfg.Modes_number+1),dtype=complex)
for i in range(0, self.cfg.Modes_number+1):
h[0,i] = -1j*S;
hfft = np.fft.fft(h)
hft0 = np.fft.fftshift(hfft)
Here is the values for S and Modes_number:
S = 12.5022214424;
Modes_number = 200;
Here is also an example of the results I get in MATLAB and Python:
MATLAB:
hfft(1,1)
ans =
1.1857e-13 - 2.5129e+03i
Python:
hfft[0]
0. -2.52544873e+03j
Cheers.
The error in your Python code is that you define h to be of size Modes_number+1, which is one more than the size in the MATLAB code. The first value in hfft is the sum of all input values. In MATLAB this is -1j*S*200 = -2500.4j, and in your Python code this is -1j*S*201 = -2512.9j. These are the values that you are seeing.
This bit of Python code produces the same as your bit of MATLAB code, up to numerical precision (I see some values like -1.68388521e-15 +6.55829989e-15j in Python, which are forced to 0 by MATLAB's algorithms). I am creating h as a one-dimensional vector, rather than a 2D array with one dimension of size 1.
import numpy as np
S = 12.5022214424
Modes_number = 200
h = np.zeros(Modes_number,dtype=complex)
for i in range(0,Modes_number):
h[i] = -1j*S;
hfft = np.fft.fft(h)
hft0 = np.fft.fftshift(hfft)
Python:
>>> hfft[0]
-2500.4442884800001j
MATLAB:
>> hfft(1)
ans =
0.000000000000000e+00 - 2.500444288480000e+03i`
I have a matrix with 236 x 97 dimension. When I print the matrix in Python its output isn't complete, having ....... in the middle of matrix.
I tried to write the matrix to a test file, but the result is exactly same.
I can't post the screenshot because my reputation is not enough, and won't appear correctly if I choose another markup option.
Can anyone solve this?
def build(self):
self.keys = [k for k in self.wdict.keys() if len(self.wdict[k]) > 1]
self.keys.sort()
self.A = zeros([len(self.keys), self.dcount])
for i, k in enumerate(self.keys):
for d in self.wdict[k]:
self.A[i,d] += 1
def printA(self):
outprint = open('outputprint.txt','w')
print 'Here is the weighted matrix'
print self.A
outprint.write('%s' % self.A)
outprint.close()
print self.A.shape
Assuming your matrix is an numpy array you can use matrix.tofile(<options>) to write the array to a file as documented here:
#!/usr/bin/env python
# coding: utf-8
import numpy as np
# create a matrix of random numbers and desired dimension
a = np.random.rand(236, 97)
# write matrix to file
a.tofile('output.txt', sep = ' ')
The problem is that you're specifically saving the str representation to a file with this line:
outprint.write('%s' % self.A)
Which explicitly casts it to a string (%s) --- generating the abridged version you're seeing.
There are lots of ways to write the entire matrix to output, one easy option would be to use numpy.savetxt, for example:
import numpy
numpy.savetxt('outputprint.txt', self.A)
I am rather new to python programming so please be a big simple with your answer.
I have a .raw file which is 2b/2b complex short int format. Its actually a 2-D raster file. I want to read and seperate both real and complex parts. Lets say the raster is [MxN] size.
Please let me know if question is not clear.
Cheers
N
You could do it with the struct module. Here's a simple example based on the file formatting information you mentioned in a comment:
import struct
def read_complex_array(filename, M, N):
row_fmt = '={}h'.format(N) # "=" prefix means integers in native byte-order
row_len = struct.calcsize(row_fmt)
result = []
with open(filename, "rb" ) as input:
for col in xrange(M):
reals = struct.unpack(row_fmt, input.read(row_len))
imags = struct.unpack(row_fmt, input.read(row_len))
cmplx = [complex(r,i) for r,i in zip(reals, imags)]
result.append(cmplx)
return result
This will return a list of complex-number lists, as can be seen in this output from a trivial test I ran:
[
[ 0.0+ 1.0j 1.0+ 2.0j 2.0+ 3.0j 3.0+ 4.0j],
[256.0+257.0j 257.0+258.0j 258.0+259.0j 259.0+260.0j],
[512.0+513.0j 513.0+514.0j 514.0+515.0j 515.0+516.0j]
]
Both the real and imaginary parts of complex numbers in Python are usually represented as a pair of machine-level double precision floating point numbers.
You could also use the array module. Here's the same thing using it:
import array
def read_complex_array2(filename, M, N):
result = []
with open(filename, "rb" ) as input:
for col in xrange(M):
reals = array.array('h')
reals.fromfile(input, N)
# reals.byteswap() # if necessary
imags = array.array('h')
imags.fromfile(input, N)
# imags.byteswap() # if necessary
cmplx = [complex(r,i) for r,i in zip(reals, imags)]
result.append(cmplx)
return result
As you can see, they're very similar, so it's not clear there's a big advantage to using one over the other. I suspect the array based version might be faster, but that would have to be determined by actually timing it with some real data to be able to say with any certainty.
Take a look at Hachoir library. It's designed for this purposes, and does it's work really good.
Any way to export a python array into SVM light format?
There is one in scikit-learn:
http://scikit-learn.org/stable/modules/generated/sklearn.datasets.dump_svmlight_file.html
It's basic but it works both for numpy arrays and scipy.sparse matrices.
I wrote this totally un-optimized script a while ago, maybe it can help! Data and labels must be in two separate numpy arrays.
def save_svmlight_data(data, labels, data_filename, data_folder = ''):
file = open(data_folder+data_filename,'w')
for i,x in enumerate(data):
indexes = x.nonzero()[0]
values = x[indexes]
label = '%i'%(labels[i])
pairs = ['%i:%f'%(indexes[i]+1,values[i]) for i in xrange(len(indexes))]
sep_line = [label]
sep_line.extend(pairs)
sep_line.append('\n')
line = ' '.join(sep_line)
file.write(line)
The svmlight-loader module can load an svmlight file into a numpy array. I don't think anything exists for the other direction, but the module is probably a good starting point for extending its functionality.