I am trying to translate some code from MATLAB to Python. Right now I am having trouble understanding the syntax of MATLAB (and in general reading this section of the code) and converting it to Python.
The following is the code:
if (verbose == 0) | (verbose == 1)
warning('off','all');
options = optimset('Display', 'off','Diagnostics','off','MaxIter',2000,'TolFun',1e-10,'TolX',1e-10 );
else
warning('on','all');
options = optimset('Display', 'final','Diagnostics','on','MaxIter',2000,'TolFun',1e-10,'TolX',1e-10);
end
I know that I can import the warnings class into Python. However I am wondering if the class is similar to the one used in MATLAB? Also, as a clarifying question, I am reading the warnings in MATLAB as they have described it in their documentation (warning(state,mode) controls whether MATLAB displays the stack trace or additional information about the warning). Is there an equivalent to that in Python?
As an edit, I also realized that optimset is also a MATLAB specific function and it changes the state of a lot of parameters. Are there any tips or other help as to how to do the same in Python?
Thank you all!
You can use
python -W ignore
It will ignore all python warning
Related
I just started switching from R to python, and may I ask about some very basic questions?
There are two nice properties that Rstudio has, which I haven't figure out how to do with python. For example, I'm trying to use the dot product function such as numpy.array([1,2]).dot(numpy.array([3,4])).
Problem 1: In R, if I move the cursor into a function, the possible arguments that I can potentially use will be shown, as demonstrated in the figure below. However, I don't see this in python (jupyter lab).
Problem 2: for the help document regarding the function, in R I would just type ?functionName(), but in python I don't really know how to get the help documents. help(numpy.array) returns me a full list of everything, and I was wondering if there is a way for it to return only the help document for the dot product function?
Problem 1: you can press Shift+Tab after placing cursor on the function in question for a snippet of documentation. Press Shift+Tab+Tab for a more detailed one.
Problem 2: you can run the same command as R but just drop the brackets.
e.g. ?numpy.dot
I'm trying to find the source code for torch.mean and am unable to find it in the pytorch github. It is under math operations but I can't find it at all.
I've looked everywhere and inspected most pages under pytorch/torch and am still unable to find it.
I even did ?? in a jupyter notebook but it just returned a useless docstring
Since operations components are written in C++, they are not callable with operations such as ?? or "__file__" or "getsourcefile" type of operations.
The files appear to be here, written in C++:
https://github.com/pytorch/pytorch/blob/master/caffe2/operators/mean_op.cc
https://github.com/pytorch/pytorch/blob/master/caffe2/operators/mean_op.h
Following this question other question I got to the next question.
I have 40 matlab files and I'm implementing a general solution for extracting that info, as each of them has a different internal organization. As soon as I have it I will post here.
But my question is: the solutions given by gg349 and mergen gets me some objects like this:
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f368cff3f90>,
And I don't know how to deal with those, could anyone help?
It is the equivalent of Matlab struct in python. You can access the elements by dot, like object.value. An example here. Complex Matlab struct mat file read by python
I'm new to Python and I'm needing to convert some Matlab code I have to Python. Here's the code I have:
save(myFile, 'list','config');
else
load(myfile, 'list');
end
I've been looking within Python and I can't seem to find an equivalent method for this. Any help would be greatly appreciated.
Thanks!
Checkout the numpy IO routines here, they can handle various formats.
This is a follow up question to the last question I'd ask, which you can read here:
Using Python's basic I/O to manipulate or create Python Files?
Basically I was asking for the best way to allow Python to edit other Python programs sources, for the purpose of screwing around/experimenting and seeing if I could hook them up to either a genetic algorithm or some sort of backprop network to get results.
Basically, one of the answers suggests making every python operator or code-bit, such as '=', 'if', etc, etc, into classes which can then be used by me/various other programs to piece together/edit other python files, utilizing Python's basic file i/o as a means of manipulating.
The classes would each, upon initialization, get their own unique ID, which would be stored, along with line number, type, etc, in an SQLite3 database for logging purposes, and would be operated upon by other classes/the basic file i/o system.
My question is: Is this a sane task? If not, what can I change or do differently? Am I undertaking something that will be worthwhile, or is it completely idiotic? If you need clarification please ask, I want to know if what I'm doing seems reasonable to an outside source, or if I should reconsider/scrap the whole deal...
Thanks in advance.
Don't generate code modify the AST for the code.
import ast
p = ast.parse("testcode")
Docs for AST: http://docs.python.org/library/ast.html
Example of modifying code: http://docs.python.org/library/ast.html#ast.NodeTransformer
Example: (modifying 3*3 to 3+3)
from ast import *
tree = parse("print(3*3)") #parse the code
tree.body[0].value.args[0].op = Add() #modify multiplication to plus
codeobj = compile(tree,"","exec") # compile the code
exec(codeobj) #run it - it should print 6 (not 9 which the original code would print)
BTW, I am interested in genetic algorithms.
If you start a project, I can help you.