Errors while importing Operator (Python) - 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))

Related

How to (log) transform *args arguments without losing structure

I am attempting to apply statistical tests to some datasets with variable numbers of groups. This causes a problem when I try to perform a log transformation for said groups while maintaining the ability to perform the test function (in this case scipy's kruskal()), which takes a variable number of arguments, one for each group of data.
The code below is an idea of what I want. Naturally stats.kruskal([np.log(i) for i in args]) does not work, as kruskal() does not expect a list of arrays, but one argument for each array. How do I perform log transformation (or any kind of alteration, really), while still being able to use the function?
import scipy.stats as stats
import numpy as np
def t(*args):
test = stats.kruskal([np.log(i) for i in args])
return test
a = [11, 12, 4, 42, 12, 1, 21, 12, 6]
b = [1, 12, 4, 3, 14, 8, 8, 6]
c = [2, 2, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8]
print(t(a, b, c))
IIUC, * in front of the list you are forming while calling kruskal should do the trick:
test = stats.kruskal(*[np.log(i) for i in args])
Asterisk unpacks the list and passes each entry of the list as arguments to the function being called i.e. kruskal here.

Jupyter iPython Notebook and Command Line yield different results

I have the following Python 2.7 code:
def average_rows2(mat):
'''
INPUT: 2 dimensional list of integers (matrix)
OUTPUT: list of floats
Use map to take the average of each row in the matrix and
return it as a list.
Example:
>>> average_rows2([[4, 5, 2, 8], [3, 9, 6, 7]])
[4.75, 6.25]
'''
return map(lambda x: sum(x)/float(len(x)), mat)
When I run it in my browser using iPython notebook, I get the following output:
[4.75, 6.25]
However, when I run the code's file on Command Line (Windows), I get the following error:
>python -m doctest Delete.py
**********************************************************************
File "C:\Delete.py", line 10, in Delete.average_rows2
Failed example:
average_rows2([[4, 5, 2, 8], [3, 9, 6, 7]])
Expected:
[4.75, 6.25]
Got:
<map object at 0x00000228FE78A898>
**********************************************************************
Why does the command line toss an error? Is there a better way to structure my function?
It seems like your command line is running Python 3. The builtin map returns a list in Python 2, but an iterator (a map object) in Python 3. To turn the latter into a list, apply the list constructor to it:
# Python 2
average_rows2([[4, 5, 2, 8], [3, 9, 6, 7]]) == [4.75, 6.25]
# => True
# Python 3
list(average_rows2([[4, 5, 2, 8], [3, 9, 6, 7]])) == [4.75, 6.25]
# => True

Python: Recursion Limit Reached while Importing

When ever I am trying to import a file named "tttnums.py" I always get this error:
Traceback (most recent call last):
File "C:/Users/Marcsegal/Dropbox/Programs/ttt finished.py", line 1, in <module>
import tttnums
RuntimeError: maximum recursion depth exceeded during compilation
This is the contents of tttnums.py:
tttSets = [
[7, 1, 4, 0, 3, 2, 8, 6, 5, 'L']
[0, 6, 5, 4, 2, 8, 1, 3, 7, 'W']
[2, 8, 0, 5, 6, 7, 4, 3, 1, 'W']
(continued with 40317 more lists)
]
I assume the reason I got this error is because I have so many lists in the file (40320 to be exact). How do I fix this error?
If the whole content of tttnums.py is just that data structure, it makes much more sense to store it in a plain text or .json file and just read it than to import it as a .py file.

Unsized object error with numpy.random.permutation?

I have some code right now that is getting stuck on one line:
perm = numpy.random.permutation(128)
To which it give the following error: "TypeError: len() of unsized object." I can't figure out what the issue is since 128 is just an integer. I see that this is a problem that has probably been resolved before here: http://mail.scipy.org/pipermail/numpy-discussion/2007-January/025592.html but their solution isn't helpful to me since it is about floats.
Can anyone see what's going wrong here?
In Sage, the input is preparsed by the Sage preparser.
I'll use 12 instead of 128 so the examples fit in one line.
When you input the following:
sage: import numpy
sage: perm = numpy.random.permutation(12)
The error message you get looks like:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-38b6a5e3e889> in <module>()
----> 1 perm = numpy.random.permutation(Integer(12))
/opt/sage/local/lib/python2.7/site-packages/numpy/random/mtrand.so in mtrand.RandomState.permutation (numpy/random/mtrand/mtrand.c:21297)()
/opt/sage/local/lib/python2.7/site-packages/numpy/random/mtrand.so in mtrand.RandomState.shuffle (numpy/random/mtrand/mtrand.c:20965)()
TypeError: len() of unsized object
where you see in particular the line:
----> 1 perm = numpy.random.permutation(Integer(12))
telling you that your input
perm = numpy.random.permutation(12)
was preparsed to
perm = numpy.random.permutation(Integer(12))
However numpy is not so happy being fed a Sage Integer,
it would prefer a Python int.
The simplest way to input a raw Python integer is to append r to it:
sage: perm = numpy.random.permutation(12r)
This will work for you:
sage: perm = numpy.random.permutation(12r)
sage: perm # random
array([ 9, 0, 11, 4, 2, 10, 3, 5, 7, 6, 1, 8])
Another way is to let Sage transform the Python int to a Sage Integer but then force it to convert it back to a Python integer:
sage: perm = numpy.random.permutation(int(12))
sage: perm # random
array([ 5, 9, 1, 7, 0, 2, 10, 6, 3, 8, 4, 11])
Another thing you could do is to turn off the Sage preparser.
sage: preparser(False)
sage: perm = numpy.random.permutation(12)
sage: perm # random
array([ 0, 2, 7, 5, 8, 11, 1, 6, 9, 10, 3, 4])

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)

Categories

Resources