could u please help me to Find correlation for these two lists importing stats-model in python.
a=[1.0,2.0,3.0,2.0]
b=[789.0,786.0,788.0,785.0]
using some built-in functions
>>> import numpy as np
>>> a = np.array([1.0,2.0,3.0,2.0])
>>> b = np.array([789.0,786.0,788.0,785.0])
>>> np.corrcoef(a,b)
array([[ 1. , -0.2236068],
[-0.2236068, 1. ]])
Just use indexing to extract the right one:
np.corrcoef(a,b)[0,1]
Related
I have a simple equation, trying to solve for using symbolic, however the code gets stuck and I do not get an error for me to debug. How can I do this correctly?
from sympy import *
from sympy import init_printing
init_printing(use_latex = True)
import sympy as sp
from numpy import random
import numpy as np
import math
from decimal import *
timeS = symbols("t")
eq1 = Eq( (-4221.4125*exp(-2750.0*timeS)*sin(6514.4071*timeS) + 10000.0*exp(-2750.0*timeS)*cos(6514.4071*timeS)),8000)
T_off = solve(eq1, timeS )[0]
display("T_off = ",T_off)
Your equation looks like one that doesn't have an analytic solution so you should use nsolve instead. There is a root between -0.001 and 0 so the bisect method gives:
In [59]: nsolve(eq1, timeS, [-0.001, 0])
Out[59]: 3.46762014687136e-5
This is an ill-posed equation. Rescale by dividing both sides by 10**4 and replace timeS with Symbol(x)/10**4 and use nsolve
>>> eq2 = eq1.func(eq1.lhs/10**4, eq1.rhs/10**4).subs(timeS, symbols('x')/10**4)
>>> from sympy import nsolve
>>> nsolve(eq2,0)
0.346762014687135
So timeS ~= 0.35e-4. The sqrt function automatically scales sums so sqrt(eq1.rewrite(Add)) will reduce coefficients of the terms to something less than 1 but such simplification is not part of SymPy simplification of general epxressions at present.
this might be a silly question. But I am desperate. I am a math teacher and I try to generate Math tests. I tried Python for this and I get some things done. However, I am not a professional programmer, so I get lost with MathMl, prettyprint() and whatsoever.
Is there anybody who can supply me a complete example that I can execute? It may just contain one small silly equation, that does not matter. I just want to see how I can get it into a Word document. After that, I can use that as a basis. I work on a Mac.
I hope anyone can help me out. Thanks in advance!
Best regards, Johan
This works for me:
from sympy import *
from docx import Document
from lxml import etree
# create expression
x, y = symbols('x y')
expr1 = (x+y)**2
# create MathML structure
expr1xml = mathml(expr1, printer = 'presentation')
tree = etree.fromstring('<math xmlns="http://www.w3.org/1998/Math/MathML">'+expr1xml+'</math>')
# convert to MS Office structure
xslt = etree.parse('C:/MML2OMML.XSL')
transform = etree.XSLT(xslt)
new_dom = transform(tree)
# write to docx
document = Document()
p = document.add_paragraph()
p._element.append(new_dom.getroot())
document.save("simpleEq.docx")
How about the following. The capture captures whatever is printed. In this case I use pprint to print the expression that I want written to file. There are lots of options you can use with pprint (including wrapping which you might want to set to False). The quality of output will depend on the fonts you use. I don't do this at all so I don't have a lot of hints for that.
from pprint import pprint
from sympy.utilities.iterables import capture
from sympy.abc import x
from sympy import Integral
with open('out.doc','w',encoding='utf-8') as f:
f.write(capture(lambda:pprint(Integral(x**2, (x, 1, 3)))))
When I double click (in Windows) on the out.doc file, a word equation with the integral appears.
Here is the actual IPython session:
IPython console for SymPy 1.6.dev (Python 3.7.3-32-bit) (ground types: python)
These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)
>>> init_printing()
Documentation can be found at https://docs.sympy.org/dev
In [1]: pprint(Integral(x**2, (x, 1, 3)))
3
(
? 2
? x dx
)
1
In [2]: from pprint import pprint
...: from sympy.utilities.iterables import capture
...: from sympy.abc import x
...: from sympy import Integral
...: with open('out.doc','w',encoding='utf-8') as f:
...: f.write(capture(lambda:pprint(Integral(x**2, (x, 1, 3)))))
...:
{problems pasting the unicode here, but it shows up as an integral symbol in console}
I have a number stored in mongo as 15000.245263 with 6 numbers after decimal point but when I use pymongo to get this number I got 15000.24. Is the pymongo reduced the precision of float?
I can't reproduce this. In Python 2.7.13 on my Mac:
>>> from pymongo import MongoClient
>>> c = MongoClient().my_db.my_collection
>>> c.delete_many({}) # Delete all documents
>>> c.insert_one({'x': 15000.245263})
>>> c.find_one()
{u'x': 15000.245263, u'_id': ObjectId('59525d32a08bff0800cc72bd')}
The retrieved value of "x" is printed the same as it was when I entered it.
This could happen if you trying to print out a long float value, and i think it is not related to mongodb.
>>> print 1111.1111
1111.1111
>>> print 1111111111.111
1111111111.11
>>> print 1111111.11111111111
1111111.11111
# for a timestamp
>>> import time
>>> now = time.time()
>>> print now
1527160240.06
For python2.7.10 it will just display 13 character(for my machine), if you want to display the whole value, use a format instead, like this:
>>> print '%.6f' % 111111111.111111
111111111.111111
And this is just a display problem, the value of the variable will not be affected.
>>> test = 111111111.111111 * 2
>>> test
222222222.222222
>>> print test
222222222.222
I have an app that want to serialize only classes/functions which are:
no Python primitive data type
no Numpy data type
no pandas data type.
So, is it possible to filter object to be saved in dill ?
(filter by loop on the type)
Thanks,
While this is not a complete solution (i.e. you'd probably want to include more of the modules with pandas data types, numpy data types… and also you might want to be more selective for the built-in types by filtering by type instead of module)… I think it sort of gets you what you want.
>>> import dill
>>> import numpy
>>> import pandas
>>>
>>> target = numpy.array([1,2,3])
>>> dill.dumps(target) if not dill.source.getmodule(type(target)) in [numpy, pandas.core.series, dill.source.getmodule(int)] else None
>>>
>>> target = [1,2,3]
>>> dill.dumps(target) if not dill.source.getmodule(type(target)) in [numpy, pandas.core.series, dill.source.getmodule(int)] else None
>>>
>>> target = lambda x:x
>>> dill.dumps(target) if not dill.source.getmodule(type(target)) in [numpy, pandas.core.series, dill.source.getmodule(int)] else None
>>>
>>> class Foo(object):
... pass
...
>>> target = Foo()
>>> dill.dumps(target) if not dill.source.getmodule(type(target)) in [numpy, pandas.core.series, dill.source.getmodule(int)] else None
'\x80\x02cdill.dill\n_create_type\nq\x00(cdill.dill\n_load_type\nq\x01U\x08TypeTypeq\x02\x85q\x03Rq\x04U\x03Fooq\x05h\x01U\nObjectTypeq\x06\x85q\x07Rq\x08\x85q\t}q\n(U\r__slotnames__q\x0b]q\x0cU\n__module__q\rU\x08__main__q\x0eU\x07__doc__q\x0fNutq\x10Rq\x11)\x81q\x12}q\x13b.'
>>>
However, if you are asking if dill has such a filtering method, then the answer is no.
I'm trying to solve the simple example found in https://en.wikipedia.org/wiki/Integer_programming#Example using the CVXOPT library on Python 2.7 ; The optimal answers are either (1,2) or (2,2). I'm getting (0.0 , 0.0). What am I doing wrong in the code below ? Thanks !
import numpy as np
import cvxopt
from cvxopt import glpk
c=cvxopt.matrix([0,-1]) #-1 since we're maximising the 2nd variable
G=cvxopt.matrix([[-1,1],[3,2],[2,3],[-1,0],[0,-1]],tc='d')
h=cvxopt.matrix([1,12,12,0,0],tc='d')
(status, x)=glpk.ilp(c,G.T,h,B=set([0,1]))
print status
print x[0],x[1] #should be (1,2) or (2,2)
print sum(c.T*x)
Your code is basically correct but needs two minor modifications:
The c-vector must be a double, too.
The variables x[0] and x[1] are supposed to be integer, not binary.
Then, a working solution is given by:
import numpy as np
import cvxopt
c=cvxopt.matrix([0,-1],tc='d')
G=cvxopt.matrix([[-1,1],[3,2],[2,3],[-1,0],[0,-1]],tc='d')
h=cvxopt.matrix([1,12,12,0,0],tc='d')
(status, x)=cvxopt.glpk.ilp(c,G.T,h,I=set([0,1]))
print status
print x[0],x[1]
print sum(c.T*x)
Update for Python 3.8.8
from cvxopt.glpk import ilp
import numpy as np
from cvxopt import matrix
c=matrix([0,-1],tc='d')
G=matrix([[-1,1],[3,2],[2,3],[-1,0],[0,-1]],tc='d')
h=matrix([1,12,12,0,0],tc='d')
(status, x)=ilp(c,G.T,h,I=set([0,1]))
print (status)
print (x[0],x[1])
print (sum(c.T*x))
Calling:
cvxopt.glpk.ilp(c,G.T,h,I=set([0,1]))
returns:
module 'cvxopt' has no attribute 'glpk'