Using numpy to apply logical xor on a list - python-2.7

I want to XOR the values in array using numpy. When using:
numpy.logical_xor([1,0,0,1,0,1,0,1])
I get:
Traceback (most recent call last):
File "test.py", line 44, in <module>
print np.logical_xor([1,0,0,1,0,1,0,1])
ValueError: invalid number of arguments
It seems numpy does not accept more than 2 elements in an array. Is there a workaround?

It seems you are looking for the reduction method. So, use -
np.logical_xor.reduce([1,0,0,1,0,1,0,1])

Related

Series regex extract producing a dataframe

I am working through a regex task on Dataquest. The following code snippet runs correctly
inside of the Dataquest IDE:
titles = hn["title"]
pattern = r'\[(\w+)\]'
tag_matches = titles.str.extract(pattern)
tag_freq = tag_matches.value_counts()
print(tag_freq, '\n')
However, on my PC running pandas 0.25.3 this exact same code block yields an error:
Traceback (most recent call last):
File "C:/Users/Mark/PycharmProjects/main/main.py", line 63, in <module>
tag_freq = tag_matches.value_counts()
File "C:\Users\Mark\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\generic.py", line 5179, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'value_counts'
Why is tag_matches coming back as a dataframe? I am running an extract against the series 'titles'.
From the docs:
Pandas.Series.str.Extract
A pattern with one group will return a Series if expand=False.
>>> s.str.extract(r'[ab](\d)', expand=False)
0 1
1 2
2 NaN
dtype: object
So perhaps you must be explicit and set expand=False to get a series object?

why does string format statement in python 2.x doesn't produce error when int or float value is given in substitution for string

when non string value is give in substitution for string place holder it doesn't produce error while it does when given non int or float value.
>>> 'In %d years I have spotted %g %s.' % (3.1, 0, 123456)
'In 3 years I have spotted 0 123456.'
>>> 'In %d years I have spotted %g %s.' % ('3.1', 0, '123')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: %d format: a number is required, not str
>>> 'In %d years I have spotted %g %s.' % (3.1, "0", '123')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: float argument required, not str
According to the docs:
's' String (converts any Python object using str()).
str() converts objects by calling their __str__ method. This is how it works for int: int has implemented __str__ method which converts it to a string.
If an object has no __str__ method implemented, its __repr__ method is called. All objects in Python inherit default implementation of this method, which returns object class name and memory address. So this means that you can pass pretty much every object to %s and this will not produce an error, but print some kind of its string representation.

Numpy stack list of ndarray

I have a list of images that have a shape of (3, 64, 64), read them and store them in a List images.
Then i applied stack to the List :
images = np.stack(images)
i got this error :
File "/usr/local/lib/python2.7/dist-packages/numpy/core/shape_base.py", line 350, in stack
raise ValueError('need at least one array to stack')
ValueError: need at least one array to stack'
I will be grateful to have anyone's idea about this.
I can reproduce your error with:
In [94]: images=[]
In [95]: np.stack(images)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-95-adab3e1812bc> in <module>()
----> 1 np.stack(images)
/usr/local/lib/python3.5/dist-packages/numpy/core/shape_base.py in stack(arrays, axis, out)
347 arrays = [asanyarray(arr) for arr in arrays]
348 if not arrays:
--> 349 raise ValueError('need at least one array to stack')
350
351 shapes = set(arr.shape for arr in arrays)
ValueError: need at least one array to stack
It raises the error because this is True:
In [97]: not images
Out[97]: True

else statement does not return to loop

I have a code that opens a file, calculates the median value and writes that value to a separate file. Some of the files maybe empty so I wrote the following loop to check it the file is empty and if so skip it, increment the count and go back to the loop. It does what is expected for the first empty file it finds ,but not the second. The loop is below
t = 15.2
while t>=11.4:
if os.stat(r'C:\Users\Khary\Documents\bin%.2f.txt'%t ).st_size > 0:
print("All good")
F= r'C:\Users\Documents\bin%.2f.txt'%t
print(t)
F= np.loadtxt(F,skiprows=0)
LogMass = F[:,0]
LogRed = F[:,1]
value = np.median(LogMass)
filesave(*find_nearest(LogMass,LogRed))
t -=0.2
else:
t -=0.2
print("empty file")
The output is as follows
All good
15.2
All good
15.0
All good
14.8
All good
14.600000000000001
All good
14.400000000000002
All good
14.200000000000003
All good
14.000000000000004
All good
13.800000000000004
All good
13.600000000000005
All good
13.400000000000006
empty file
All good
13.000000000000007
Traceback (most recent call last):
File "C:\Users\Documents\Codes\Calculate Bin Median.py", line 35, in <module>
LogMass = F[:,0]
IndexError: too many indices
A second issue is that t somehow goes from one decimal place to 15 and the last place seems to incrementing whats with that?
Thanks for any and all help
EDIT
The error IndexError: too many indices only seems to apply to files with only one line example...
12.9982324 0.004321374
If I add a second line I no longer get the error can someone explain why this is? Thanks
EDIT
I tried a little experiment and it seems numpy does not like extracting a column if the array only has one row.
In [8]: x = np.array([1,3])
In [9]: y=x[:,0]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-9-50e27cf81d21> in <module>()
----> 1 y=x[:,0]
IndexError: too many indices
In [10]: y=x[:,0].shape
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-10-e8108cf30e9a> in <module>()
----> 1 y=x[:,0].shape
IndexError: too many indices
In [11]:
You should be using try/except blocks. Something like:
t = 15.2
while t >= 11.4:
F= r'C:\Users\Documents\bin%.2f.txt'%t
try:
F = np.loadtxt(F,skiprows=0)
LogMass = F[:,0]
LogRed = F[:,1]
value = np.median(LogMass)
filesave(*find_nearest(LogMass,LogRed))
except IndexError:
print("bad file: {}".format(F))
else:
print("file worked!")
finally:
t -=0.2
Please refer to the official tutorial for more details about exception handling.
The issue with the last digit is due to how floats work they can not represent base10 numbers exactly. This can lead to fun things like:
In [13]: .3 * 3 - .9
Out[13]: -1.1102230246251565e-16
To deal with the one line file case, add the ndmin parameter to np.loadtxt (review its doc):
np.loadtxt('test.npy',ndmin=2)
# array([[ 1., 2.]])
With the help of a user named ajcr, found the problem was that ndim=2 should have been used in numpy.loadtxt() to insure that the array always 2 has dimensions.
Python uses indentation to define if while and for blocks.
It doesn't look like your if else statement is fully indented from the while.
I usually use a full 'tab' keyboard key to indent instead of 'spaces'

int object not callable error (sum of multiples)

I am writing a program to print the sum of multiple of 3 or 5 less than 1000. I am using an arithmetic progression to do it. My code is:
def multiple(x,y):
a=(1000-(1000%x) - x)/x +1
b=(995-y)/y +1
c=(1000-(1000%x*y)-x*y)/x*y +1
Sa=int(a/2(2*x+(a-1)*a))
Sb=b/2(2*y+(b-1)*b)
Sc=c/2(2*x*y+(c-1)*x*y)
Sd=Sa+Sb-Sc
print Sd
When I call the function I get the error:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python27\swampy-2.1.7\MULTIPLE.py", line 23, in multiple
Sa=int(a/2(2*x+(a-1)*a))
TypeError: 'int' object is not callable
Please point out the mistake in my code. Thanks.
P.S. Please forgive my "art" of question asking. I am new to Python and StackOverflow, so please bear with me. Thanks!
In Sa=int(a/2(2*x+(a-1)*a)) you forgot a * to multiply between a/2 and (2*x+(a-1)*a) You should have Sa=int(a/2*(2*x+(a-1)*a)).
Also, the same on Sb and Sc.