math.prod() is not working in google colab notebook - python

I have executed this code in Gooogle ColabNotebook...As per the link given below...
prod() is available under math module. But why prod() is not working..its giving me the error?
https://www.w3schools.com/python/ref_math_prod.asp
import math
print(math.prod([1,2,3,4,5,6,7]))
Output
AttributeError: module 'math' has no attribute 'prod'

math.prod() is a new function available in Python versions 3.8 and later. Google Colab's kernel, as of this writing, runs Python 3.6.9. As such, math.prod() won't be available for your use.
You can try to install a Python 3.8 kernel in Colab, but it seems some folks have some mixed results, and the method in the accepted answer is a bit hacky, but might work for your purposes.

Google Colab uses Python 3.6. 9 as of 2021. and math.prod() is in python 3.8 version
you can use this
from functools import reduce
import operator
print(reduce(operator.mul, [1,2,3,4,5,6,7], 1))

Related

Why is kociemba python module not working correctly?

I'm trying to build a rubik's cube solver and I'm using kociemba module. I had some problems with installation so I downloaded it manually from GitHub - https://github.com/muodov/kociemba. Now I'm testing it but I'm getting an error that I don't understand. Answer if you can help, thanks!
This is the test code:
import kociemba
kociemba.solve('DRLUUBFBRBLURRLRUBLRDDFDLFUFUFFDBRDUBRUFLLFDDBFLUBLRBD')
And I'm getting this output:
No module named 'kociemba.ckociembawrapper'
C:\Users\Paweł\AppData\Local\Programs\Python\Python39\lib\site-
packages\kociemba\__init__.py:21: SlowContextWarning: Native version of the package
is not available. We have to fallback to pure-Python implementation of the
algorithm, which is usually many times slower. If performance is important to you,
check official project page for a native implementation:
https://github.com/muodov/kociemba
warnings.warn("Native version of the package is not available. "

How to solve incompatibility between DeepPavlov, Tensorflow and Numpy in a Python script

I am trying to run a script in Python 3.7 that makes use of DeepPavlov and Tensorflow==2.4.1.
It seems that there is an incompatibility, because:
DeepPavlov can work only with Numpy==1.18
Tensorflow==2.4.1 needs at least Numpy==1.19.2
If I downgrade Tensorflow, some of the instructions in the script fail, in particular I can not import "transpose_shape".
I may try to change the script, and use different functions.
However, I know for sure that this script has been successfully used by the person who gave it to me.
Is there a way around this apparent incompatibility?

Using Matlab functions in Python [duplicate]

Is it possible to run MATLAB functions from within Python?
I search the internet, I could only find PyMat. The bad thing is the compiled version only supports Python2.2 and I am using 2.6. So I tried to download the source code, so I can compile it for myself. But I cannot compile it, VC++ express seems not to have the necessary functionalities to compile it. Does anyone have the compile version for PC?
or any substitutes for PyMat?
Thanks
I know this is an old question and has been answered. But I was looking for the same thing (for the Mac) and found that there are quite a few options with different methods of interacting with matlab and different levels of maturity. Here's what I found:
pymat
A low level interface to Matlab using the matlab engine (libeng) for communication (basically a library that comes with matlab). The module has to be compiled and linked with libeng.
http://pymat.sourceforge.net
Last updated: 2003
pymat2
A somewhat short lived continuation of the pymat development. Seems to work on windows (including 64bit), linux and mac (with some changes).
https://code.google.com/p/pymat2/
Last updated: 2012
mlabwrap
A high level interface that also comes as a module which needs compilation and linking against libeng. It exposes Matlab functions to python so you can do fun stuff like
mlab.plot(x, y, 'o')
http://mlabwrap.sourceforge.net
Last updated: 2009
mlab
A repackaging effort of mlabwrap. Basically it replaces the c++ code that links against 'libeng' in mlabwrap with a python module (matlabpipe) that communicates with matlab through a pipe. The main advantage of this is that it doesn't need compilation of any kind.
Unfortunately the package currently has a couple of bugs and doesn't seem to work on the mac at all. I reported a few of them but gave up eventually. Also, be prepared for lots of trickery and a bunch of pretty ugly hacks if you have to go into the source code ;-) If this becomes more mature it could be one of the best options.
https://github.com/ewiger/mlab
last update: 2013
pymatlab
A newer package (2010) that also interacts with Matlab through libeng. Unlike the other packages this one loads the engine library through ctypes thus no compilation required. Its not without flaws but still being maintained and the (64bit Mac specific) issues I found should be easy enough to fix.
(edit 2014-05-20: it seems those issues have already been fixed in the source so things should be fine with 0.2.4)
http://pymatlab.sourceforge.net
last update: 2014
python-matlab-bridge
Also a newer package that is still actively maintained. Communicates with Matlab through some sort of socket. Unfortunately the exposed functions are a bit limited. I couldn't figure out how to invoke a function that takes structs as parameters. Requires zmq, pyzmq and IPython which are easy enough to install.
http://arokem.github.io/python-matlab-bridge
last update: 2014
Another option is Mlabwrap:
Mlabwrap is a high-level python to Matlab® bridge that lets Matlab look like a normal python library.
It works well with numpy arrays. An example from the home page:
>>> from mlabwrap import mlab; from numpy import *
>>> xx = arange(-2*pi, 2*pi, 0.2)
>>> mlab.surf(subtract.outer(sin(xx),cos(xx)))
PyMat looks like it's been abandoned.
I'm assuming you are on windows so you could always do the simplest approach and use Matlab's COM interface:
>>> import win32com.client
>>> h = win32com.client.Dispatch('matlab.application')
>>> h.Execute ("plot([0 18], [7 23])")
>>> h.Execute ("1+1")
u'\nans =\n\n 2\n\n'
More info here
There is a python-matlab bridge which is unique in the sense that Matlab runs in the background so you don't have the startup cost each time you call a Matlab function.
https://github.com/jaderberg/python-matlab-bridge
it's as easy as downloading and the following code:
from pymatbridge import Matlab
mlab = Matlab(matlab='/Applications/MATLAB_R2011a.app/bin/matlab')
mlab.start()
res = mlab.run('path/to/yourfunc.m', {'arg1': 3, 'arg2': 5})
print res['result']
where the contents of yourfunc.m would be something like this:
%% MATLAB
function lol = yourfunc(args)
arg1 = args.arg1;
arg2 = args.arg2;
lol = arg1 + arg2;
end
see this page: An Open-Source MATLAB®-to-Python® Compiler
I would like to add one more option to the excellent summary by Lukas:
matlab_wrapper
The advantage of matlab_wrapper is that it is pure Python library and you will not need to compile anything. Works in GNU/Linux, Windows and OSX.
https://github.com/mrkrd/matlab_wrapper
Disclaimer: I'm the author of matlab_wrapper
You can use the official matlab engine by installing Matlab, then building python engine from its extern files. You can check the guide website below:
---Thanks for the advice in the first comment of this answer ---
the essential step in brief are (On Windows platform, other can checked in the url below):
1. download and then install matlab, the version must be R2014 or later.
2. open a PowerShell window under admin, then:
cd "matlabroot\extern\engines\python"
3. use command-line below to install:
python setup.py install
The admin is essential, or you'll fail to build it.
For more information, you can click the official start sheet below:
http://cn.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html
newer versions of matlab seem to provide a module that allows you to call matlab functions from within python. see here and here.
2 more options for you to consider:
Follow the official MATLAB docs:
Create a Python Application with MATLAB Code. This will create a Python library that includes MATLAB runtime which you can call from within your Python code.
Run your MATLAB code in GNU Octave then call it from Python using Oct2Py
This is the solution from Mathworks.
In your current folder, create a MATLAB script in a file named triarea.m.
function a = triarea(b,h)
a = 0.5*(b.* h);
Meanwhile, you run the python code as follows,
import matlab.engine
eng = matlab.engine.start_matlab()
eng.addpath('your/code/folders/')
ret = eng.triarea(1.0,5.0)
print(ret)
>>> 2.5
Matlab already provides the python module of the engine, to install that you can do the following,
cd matlab_root_folder/extern/engines/python
python setup.py install
You are all done!
Tips: you need to be careful about the data type, the engine is not friendly with numpy. You need to convert the data first.
mat_array = matlab.double(list(my_numpy_array))
eng.my_matlabe_function(mat_array )

How can I import PCL into Python, on Ubuntu?

So my situation is as follows: I am on an Ubuntu 14.04, and I am very simply, trying to use PCL (point cloud library) in Python 2.7x.
I followed the instructions here,(http://pointclouds.org/downloads/linux.html), however in Python if I now do
import pcl
I still get the error:
ImportError: No module named pcl
I am not sure what else to do - there do not seem to be any more leads I can follow... thanks.
You can try python-pcl.
It is a python binding and supports operations on PointXYZ.

python UDF version with Jython/Pig

When I do Python UDF with Pig, how do we know which version of Python it is using? Is it possible to use a specific version of Python?
Specifically my problem is in my UDF, I need to use a function in math module math.erf() which is newly introduced in Python version 2.7. I have Python 2.7 installed on my machine and standalone Python program runs fine but when I run it in Pig as Python UDF, I got this:
AttributeError: type object 'org.python.modules.math' has no attribute 'erf'
My guess is Jython is using some pre-2.7 version of Python?
Thanks for your help!
To get the version you are using you can do this:
myUDFS.py
#!/usr/bin/python
import sys
#outputSchema('bar: chararray')
def my_func(foo):
print sys.version
return foo
If you run the script locally then the version will be printed directly to stdout. To see the output of sys.version when you run it remotely you'll have to check the logs on the job tracker.
However, you are right about Jython being pre-2.7 (kind of). The current stable version of Jython right now is 2.5.3, so this is the version that Pig is using. There is a beta version of 2.7.

Categories