python library to generate all factors of an integer? - python-3.x

I tried sympy.factorint() but it returns a dictionary of prime factors with count.
I need all factors in a list in ascending order.
import sympy
sympy.factorint(567)
output
{3: 4, 7: 1}
I am looking for output like this
[1, 3, 7, 9, 21, 27, 63, 81, 189, 567]

Found solution I should use sympy.divisors() instead
import sympy
sympy.divisors(n)

Related

new value is the sum of old values

I have 2 list A and B, list A contain values I want list B to be the sum of value from list a
A = [3,5,7,8,9,12,13,20]
#Wanted result
#B = [3, 8, 15, 23,...77]
#so the new value will be the sum of the old value
# [x1, x2+x1, x3+x2+x1,... xn+xn+xn]
what methods I could use to get the answer, thank you.
The easiest way IMO would be to use numpy.cumsum, to get the cumulative sum of your list:
>>> import numpy as np
>>> np.cumsum(A)
array([ 3, 8, 15, 23, 32, 44, 57, 77])
But you also could do it in a list comprehension like this:
>>> [sum(A[0:x]) for x in range(1, len(A)+1)]
[3, 8, 15, 23, 32, 44, 57, 77]
Another fun way is to use itertools.accumulate, which gives accumulated sums by default:
>>> from itertools import accumulate
>>> list(accumulate(A))
[3, 8, 15, 23, 32, 44, 57, 77]

what is the format of sklearn cluster labels?

I'm using sklearn to cluster some lines of text, but trying to understand the format of the returned cluster labels. It looks like this:
km_model.labels_
array([ 5, 35, 1, 29, 49, 2, 6, 28, 5, 4, 4, 19, 40, 52, 6, 20, 4,\n 40, 40, 7, 10, 13, 14, 4, 10, 29, 14, 22, 24, 13, 24, 5, 4, 21,\n ...
So it's kind of like an array but there are elements of \n to separate clusters?
Is that really the format?
Is this some type of shortcut method for packing matrices in SKLearn? Why don't they return a 2D array of labels, eg one list of labels per cluster?
After that what is the best way to iterate through this type of data and group the labels per cluster?
Your clusters are the number values, the index of each label corresponds to the index of the samples you passed into your model. I suspect the \n is resulting from whatever IDE you're using read this output.

How do I turn a large multi dimentional numpy ndarray into a string without truncating and save it to .txt in python3? [duplicate]

When I print a numpy array, I get a truncated representation, but I want the full array.
>>> numpy.arange(10000)
array([ 0, 1, 2, ..., 9997, 9998, 9999])
>>> numpy.arange(10000).reshape(250,40)
array([[ 0, 1, 2, ..., 37, 38, 39],
[ 40, 41, 42, ..., 77, 78, 79],
[ 80, 81, 82, ..., 117, 118, 119],
...,
[9880, 9881, 9882, ..., 9917, 9918, 9919],
[9920, 9921, 9922, ..., 9957, 9958, 9959],
[9960, 9961, 9962, ..., 9997, 9998, 9999]])
Use numpy.set_printoptions:
import sys
import numpy
numpy.set_printoptions(threshold=sys.maxsize)
import numpy as np
np.set_printoptions(threshold=np.inf)
I suggest using np.inf instead of np.nan which is suggested by others. They both work for your purpose, but by setting the threshold to "infinity" it is obvious to everybody reading your code what you mean. Having a threshold of "not a number" seems a little vague to me.
Temporary setting
You can use the printoptions context manager:
with numpy.printoptions(threshold=numpy.inf):
print(arr)
(of course, replace numpy by np if that's how you imported numpy)
The use of a context manager (the with-block) ensures that after the context manager is finished, the print options will revert to whatever they were before the block started. It ensures the setting is temporary, and only applied to code within the block.
See numpy.printoptions documentation for details on the context manager and what other arguments it supports. It was introduced in NumPy 1.15 (released 2018-07-23).
The previous answers are the correct ones, but as a weaker alternative you can transform into a list:
>>> numpy.arange(100).reshape(25,4).tolist()
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19], [20, 21,
22, 23], [24, 25, 26, 27], [28, 29, 30, 31], [32, 33, 34, 35], [36, 37, 38, 39], [40, 41,
42, 43], [44, 45, 46, 47], [48, 49, 50, 51], [52, 53, 54, 55], [56, 57, 58, 59], [60, 61,
62, 63], [64, 65, 66, 67], [68, 69, 70, 71], [72, 73, 74, 75], [76, 77, 78, 79], [80, 81,
82, 83], [84, 85, 86, 87], [88, 89, 90, 91], [92, 93, 94, 95], [96, 97, 98, 99]]
Here is a one-off way to do this, which is useful if you don't want to change your default settings:
def fullprint(*args, **kwargs):
from pprint import pprint
import numpy
opt = numpy.get_printoptions()
numpy.set_printoptions(threshold=numpy.inf)
pprint(*args, **kwargs)
numpy.set_printoptions(**opt)
This sounds like you're using numpy.
If that's the case, you can add:
import numpy as np
np.set_printoptions(threshold=np.nan)
That will disable the corner printing. For more information, see this NumPy Tutorial.
Using a context manager as Paul Price sugggested
import numpy as np
class fullprint:
'context manager for printing full numpy arrays'
def __init__(self, **kwargs):
kwargs.setdefault('threshold', np.inf)
self.opt = kwargs
def __enter__(self):
self._opt = np.get_printoptions()
np.set_printoptions(**self.opt)
def __exit__(self, type, value, traceback):
np.set_printoptions(**self._opt)
if __name__ == '__main__':
a = np.arange(1001)
with fullprint():
print(a)
print(a)
with fullprint(threshold=None, edgeitems=10):
print(a)
numpy.savetxt
numpy.savetxt(sys.stdout, numpy.arange(10000))
or if you need a string:
import StringIO
sio = StringIO.StringIO()
numpy.savetxt(sio, numpy.arange(10000))
s = sio.getvalue()
print s
The default output format is:
0.000000000000000000e+00
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00
...
and it can be configured with further arguments.
Note in particular how this also not shows the square brackets, and allows for a lot of customization, as mentioned at: How to print a Numpy array without brackets?
Tested on Python 2.7.12, numpy 1.11.1.
This is a slight modification (removed the option to pass additional arguments to set_printoptions)of neoks answer.
It shows how you can use contextlib.contextmanager to easily create such a contextmanager with fewer lines of code:
import numpy as np
from contextlib import contextmanager
#contextmanager
def show_complete_array():
oldoptions = np.get_printoptions()
np.set_printoptions(threshold=np.inf)
try:
yield
finally:
np.set_printoptions(**oldoptions)
In your code it can be used like this:
a = np.arange(1001)
print(a) # shows the truncated array
with show_complete_array():
print(a) # shows the complete array
print(a) # shows the truncated array (again)
with np.printoptions(edgeitems=50):
print(x)
Change 50 to how many lines you wanna see
Source: here
A slight modification: (since you are going to print a huge list)
import numpy as np
np.set_printoptions(threshold=np.inf, linewidth=200)
x = np.arange(1000)
print(x)
This will increase the number of characters per line (default linewidth of 75). Use any value you like for the linewidth which suits your coding environment. This will save you from having to go through huge number of output lines by adding more characters per line.
Complementary to this answer from the maximum number of columns (fixed with numpy.set_printoptions(threshold=numpy.nan)), there is also a limit of characters to be displayed. In some environments like when calling python from bash (rather than the interactive session), this can be fixed by setting the parameter linewidth as following.
import numpy as np
np.set_printoptions(linewidth=2000) # default = 75
Mat = np.arange(20000,20150).reshape(2,75) # 150 elements (75 columns)
print(Mat)
In this case, your window should limit the number of characters to wrap the line.
For those out there using sublime text and wanting to see results within the output window, you should add the build option "word_wrap": false to the sublime-build file [source] .
To turn it off and return to the normal mode
np.set_printoptions(threshold=False)
Since NumPy version 1.16, for more details see GitHub ticket 12251.
from sys import maxsize
from numpy import set_printoptions
set_printoptions(threshold=maxsize)
Suppose you have a numpy array
arr = numpy.arange(10000).reshape(250,40)
If you want to print the full array in a one-off way (without toggling np.set_printoptions), but want something simpler (less code) than the context manager, just do
for row in arr:
print row
If you're using a jupyter notebook, I found this to be the simplest solution for one off cases. Basically convert the numpy array to a list and then to a string and then print. This has the benefit of keeping the comma separators in the array, whereas using numpyp.printoptions(threshold=np.inf) does not:
import numpy as np
print(str(np.arange(10000).reshape(250,40).tolist()))
You won't always want all items printed, especially for large arrays.
A simple way to show more items:
In [349]: ar
Out[349]: array([1, 1, 1, ..., 0, 0, 0])
In [350]: ar[:100]
Out[350]:
array([1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1,
1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1])
It works fine when sliced array < 1000 by default.
If you are using Jupyter, try the variable inspector extension. You can click each variable to see the entire array.
This is the hackiest solution it even prints it nicely as numpy does:
import numpy as np
a = np.arange(10000).reshape(250,40)
b = [str(row) for row in a.tolist()]
print('\n'.join(b))
Out:
You can use the array2string function - docs.
a = numpy.arange(10000).reshape(250,40)
print(numpy.array2string(a, threshold=numpy.nan, max_line_width=numpy.nan))
# [Big output]
If you have pandas available,
numpy.arange(10000).reshape(250,40)
print(pandas.DataFrame(a).to_string(header=False, index=False))
avoids the side effect of requiring a reset of numpy.set_printoptions(threshold=sys.maxsize) and you don't get the numpy.array and brackets. I find this convenient for dumping a wide array into a log file
If an array is too large to be printed, NumPy automatically skips the central part of the array and only prints the corners:
To disable this behaviour and force NumPy to print the entire array, you can change the printing options using set_printoptions.
>>> np.set_printoptions(threshold='nan')
or
>>> np.set_printoptions(edgeitems=3,infstr='inf',
... linewidth=75, nanstr='nan', precision=8,
... suppress=False, threshold=1000, formatter=None)
You can also refer to the numpy documentation numpy documentation for "or part" for more help.

Pass argument to array of functions

I have a 2D numpy array of lambda functions. Each function has 2 arguments and returns a float.
What's the best way to pass the same 2 arguments to all of these functions and get a numpy array of answers out?
I've tried something like:
np.reshape(np.fromiter((fn(1,2) for fn in np.nditer(J,order='K',flags=["refs_ok"])),dtype = float),J.shape)
to evaluate each function in J with arguments (1,2) ( J contains the functions).
But it seems very round the houses, and also doesn't quite work...
Is there a good way to do this?
A = J(1,2)
doesn't work!
You can use list comprehensions:
A = np.asarray([[f(1,2) for f in row] for row in J])
This should work for both numpy arrays and list of lists.
I don't think there is a really clean way, but this is reasonably clean and works:
import operator
import numpy as np
# create array of lambdas
a = np.array([[lambda x, y, i=i, j=j: x**i + y**j for i in range(4)] for j in range(4)])
# apply arguments 2 and 3 to all of them
np.vectorize(operator.methodcaller('__call__', 2, 3))(a)
# array([[ 2, 3, 5, 9],
# [ 4, 5, 7, 11],
# [10, 11, 13, 17],
# [28, 29, 31, 35]])
Alternatively, and slightly more flexible:
from types import FunctionType
np.vectorize(FunctionType.__call__)(a, 2, 3)
# array([[ 2, 3, 5, 9],
# [ 4, 5, 7, 11],
# [10, 11, 13, 17],
# [28, 29, 31, 35]])

Python: set(sympy.primerange(a, b))

(Python 3.5.1)
I've been trying to use Sympy for some Project Euler problems, but I've come across something weird about how set(sympy.primerange(a, b)) and similar constructions work.
>>> import sympy
>>> PR = sympy.primerange(1, 20)
>>> set(PR)
{2, 3, 5, 7, 11, 13, 17, 19}
So far, so good. But:
>>> import sympy
>>> PR = sympy.primerange(1, 20)
>>> set(PR)
{2, 3, 5, 7, 11, 13, 17, 19}
>>> set(PR)
set()
Calling simply PR gives me <generator object primerange at 0x039C1720> after calling list(PR) once or twice. The same thing happens with for p in PR: print(p) and list(PR).
Why does this not work:
>>> import sympy, itertools
>>> sympy.sieve.extend(100)
>>> set(itertools.takewhile(lambda p: p<20, sympy.sieve))
set()
>>> sympy.sieve
<Sieve with 25 primes sieved: 2, 3, 5, ... 89, 97>
Why don't we get the set {2, 3, 5, 7, 11, 13, 17, 19}?
The first phenomenon is has do to with generators. sympy.primerange returns a generator, not a list. Generators let you iterate over their elements once, producing them on demand. The call to set() iterates over every element in the generator PR, consuming it.
itertools.takewhile requires an iterable for its second argument. sympy.sieve is not an iterable. It allows you to to request arbitrary primes by index, and maintains a dynamic internal sieve. Because sympy.sieve isn't an iterable, takewhile can't extract any elements from it. That's why you don't get your expected results.
Kudos to you for doing Project Euler.

Resources