Trouble using add_row with prettytable - python

I am trying to format an array using the prettytable library. Here is my code:
from prettytable import PrettyTable
arrayHR = [1,2,3,4,5,6,7,8,9,10]
print ("arrayHR:", arrayHR)
x = PrettyTable(["Heart Rate"])
for row in arrayHR:
x.add_row(row)
This results in the following error:
arrayHR: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Traceback (most recent call last):
File "C:\Users\aag\Documents\Python\test.py", line 7, in <module>
x.add_row(row)
File "C:\Python33\lib\site-packages\prettytable.py", line 817, in add_row
if self._field_names and len(row) != len(self._field_names):
TypeError: object of type 'int' has no len()
What am I doing wrong?

According to the documentation, add_row is expecting a list, not an int, as an argument. Assuming that you want the values in arrayHR to be the first value in each row, you could do:
x = PrettyTable(["Heart Rate"])
for row in arrayHR:
x.add_row([row])
or adopt the add_column example, also from the documentation:
x = PrettyTable()
x.add_column("Heart Rate", arrayHR)

Related

Errors while importing Operator (Python)

I am a little confused after a couple attempts while importing Operator and receiving errors. Along with a couple of examples, I've shared a python doc link for reference below.
What I'm expecting to happen below is that operator will run the product and multiply 3 * 4 in the data list which the answer will start [3, 12....] then multiply 12 by the next element '6' to give, [3, 12, 72...]. However importing Operator here isn't working as expected?
The Output I'm expecting for this problem is:
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
Running the below code in PythonTutor.com gives me an Error:
ImportError: cannot import name 'operator'
from itertools import operator
data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
list(accumulate(data, operator.mul))
I've gotten the same type of error running this in Jupyter notebook:
ImportError Traceback (most recent call last)
<ipython-input-1-bc61652bebb8> in <module>
----> 1 from itertools import operator
2
3 data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
4 list(accumulate(data, operator.mul))
ImportError: cannot import name 'operator' from 'itertools' (unknown location)
I've spelled check about 100 times and I've ran these on both PythonTutor and Jupyter NB, and both are giving me errors - can this be an issue with itertools?
Below is from The Python Docs. I'm using the first case:
operator.mul(a, b)
I'll share for your reference: Here
----> operator.mul(a, b)
operator.__mul__(a, b)
Return a * b, for a and b numbers.
Why isn't this working, and how can I fix it?
operator is its own module, not part of itertools:
import itertools
import operator
Note that itertools.accumulate doesn't modify the iterable it is given. It returns a new object which you are not using above. Consider assigning it to a new variable:
data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
accumulated_list = list(itertools.accumulate(data, operator.mul))

comm.bcast not working properly

I am trying to test a simple mpi code on python with the following code :
from scipy.sparse import csr_matrix
from mpi4py import MPI
comm=MPI.COMM_WORLD
rank=comm.Get_rank()
size=comm.Get_size()
if rank == 0:
data = [1, 2, 3, 4, 5]
indices = [1, 3, 2, 1, 0]
indptr = [0, 2, 3, 4, 5]
#A=csr_matrix((data,indices,indptr),shape=(4,4))
data=comm.bcast(data, root=0)
indices=comm.bcast(indices, root=0)
indptr=comm.bcast(indptr, root=0)
print rank,data,indices,indptr
which returns the following error:
Traceback (most recent call last):
File "test.py", line 14, in <module>
data=comm.bcast(data, root=0)
NameError: name 'data' is not defined
Traceback (most recent call last):
File "test.py", line 14, in <module>
data=comm.bcast(data, root=0)
NameError: name 'data' is not defined
Traceback (most recent call last):
File "test.py", line 14, in <module>
data=comm.bcast(data, root=0)
NameError: name 'data' is not defined
0 [1, 2, 3, 4, 5] [1, 3, 2, 1, 0] [0, 2, 3, 4, 5]
-------------------------------------------------------
Primary job terminated normally, but 1 process returned
a non-zero exit code.. Per user-direction, the job has been aborted.
-------------------------------------------------------
--------------------------------------------------------------------------
mpirun detected that one or more processes exited with non-zero status, thus causing
the job to be terminated. The first process to do so was:
Process name: [[10263,1],1]
Exit code: 1
It seems like the error is due to me not using comm.bcast properly, but that is exactly how its used in the docs.
you are defining data in the if block. What happens when the if block is false? the variable data is not defined.
from scipy.sparse import csr_matrix
from mpi4py import MPI
comm=MPI.COMM_WORLD
rank=comm.Get_rank()
size=comm.Get_size()
data = []
indices = []
indptr = []
if rank == 0:
data = [1, 2, 3, 4, 5]
indices = [1, 3, 2, 1, 0]
indptr = [0, 2, 3, 4, 5]
#A=csr_matrix((data,indices,indptr),shape=(4,4))
data=comm.bcast(data, root=0)
indices=comm.bcast(indices, root=0)
indptr=comm.bcast(indptr, root=0)
print rank,data,indices,indptr
This should now work.

Piping a pipe-delimited flat file into python for use in Pandas and Stats

I have searched a lot, but haven't found an answer to this.
I am trying to pipe in a flat file with data and put into something python read and that I can do analysis with (for instance, perform a t-test).
First, I created a simple pipe delimited flat file:
1|2
3|4
4|5
1|6
2|7
3|8
8|9
and saved it as "simpledata".
Then I created a bash script in nano as
#!/usr/bin/env python
import sys
from scipy import stats
A = sys.stdin.read()
print A
paired_sample = stats.ttest_rel(A[:,0],A[:,1])
print "The t-statistic is %.3f and the p-value is %.3f." % paired_sample
Then I save the script as pairedttest.sh and run it as
cat simpledata | pairedttest.sh
The error I get is
TypeError: string indices must be integers, not tuple
Thanks for your help in advance
Are you trying to call this?:
paired_sample = stats.ttest_rel([1,3,4,1,2,3,8], [2,4,5,6,7,8,9])
If so, you can't do it the way you're trying. A is just a string when you read it from stdin, so you can't index it the way you're trying. You need to build the two lists from the string. The most obvious way is like this:
left = []
right = []
for line in A.splitlines():
l, r = line.split("|")
left.append(int(l))
right.append(int(r))
print left
print right
This will output:
[1, 3, 4, 1, 2, 3, 8]
[2, 4, 5, 6, 7, 8, 9]
So you can call stats.ttest_rel(left, right)
Or to be really clever and make a (nearly impossible to read) one-liner out of it:
z = zip(*[map(int, line.split("|")) for line in A.splitlines()])
This will output:
[(1, 3, 4, 1, 2, 3, 8), (2, 4, 5, 6, 7, 8, 9)]
So you can call stats.ttest_rel(*z)

TypeError while representing arbitrary element type in multiprocessing.Array

>>> from multiprocessing import Array, Value
>>> import numpy as np
>>> a = [(i,[]) for i in range(3)]
>>> a
[(0, []), (1, []), (2, [])]
>>> a[0][1].extend(np.array([1,2,3]))
>>> a[1][1].extend(np.array([4,5]))
>>> a[2][1].extend(np.array([6,7,8]))
>>> a
[(0, [1, 2, 3]), (1, [4, 5]), (2, [6, 7, 8])]
Following the python multiprocessing example: def test_sharedvalues(): I am trying to create a shared Proxy object using the below code:
shared_a = [multiprocessing.Array(id, e) for id, e in a]
but it is giving me an error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/multiprocessing/__init__.py", line 255, in Array
return Array(typecode_or_type, size_or_initializer, **kwds)
File "/usr/lib64/python2.6/multiprocessing/sharedctypes.py", line 87, in Array
obj = RawArray(typecode_or_type, size_or_initializer)
File "/usr/lib64/python2.6/multiprocessing/sharedctypes.py", line 60, in RawArray
result = _new_value(type_)
File "/usr/lib64/python2.6/multiprocessing/sharedctypes.py", line 36, in _new_value
size = ctypes.sizeof(type_)
TypeError: this type has no size
Ok. The problem is solved
I changed
>>> a = [(i,[]) for i in range(3)]
to
>>> a = [('i',[]) for i in range(3)]
and this solved the TypeError.
Actually, I also found out that I did not necessarily had to use the i as count within range(3) (since Array automatically allows indexing), The 'i' is for c_int typecode under multiprocessing.sharedctypes
Hope this helps.

How do I select components from a particular list of indices of a Python vector?

So here I have x1vals:
>>> x1Vals
[-0.33042515829906227, -0.1085082739900165, 0.93708611747433213, -0.19289496973017362, -0.94365384912207761, 0.43385903975568652, -0.46061140566051262, 0.82767432358782367, -0.24257307936591843, -0.1182761514447952, -0.29794617763330011, -0.87410892638408, -0.34732294121174467, 0.40646145339571249, -0.64082861589870865, -0.45680189916940073, 0.4688889876175073, -0.89399689430691298, 0.53549621114138612]
And here is the list of x1Vals indices that I want to select
>>> np.where(np.dot(XValsOnly,newweights) > 0)
>>>(array([ 1, 2, 4, 5, 6, 8, 9, 13, 15, 16]),)
But when I try to get the values of x1Vals the Matlab way, I get this error:
>>> x1Vals[np.where(np.dot(XValsOnly,newweights) > 0)]
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
x1Vals[np.where(np.dot(XValsOnly,newweights) > 0)]
TypeError: list indices must be integers, not tuple
>>> np.where(np.dot(XValsOnly,newweights) > 0)
Is there a way around this?
The problem is that your x1Vals is a list object, which does not support fancy indexing. You just have to build an array out of it:
x1Vals = np.array(x1Vals)
and your approach will work.
A faster approach would be to use np.take:
np.take(x1Vals, np.where(np.dot(XValsOnly,newweights) > 0))

Categories

Resources