I was trying to convert a piece of code in Matlab to python. There I could find a Standard deviation filter function "stdfilt()" for which I could not find any equivalent API in python.
The Matlab code is given below,
Ig_med = medfilt2(Input_Image);
h_gauss = fspecial('gaussian',11,2);
h_avg = fspecial('average',121);
I = imfilter(Ig_med,h_gauss,'corr','replicate');
P = stdfilt(I,ones(121));
P = P.^2;
Q = imfilter(P,h_avg,'corr','replicate');
Can anyone help me out in implementing the above piece of code in Python ?
Thanks in Advance
I found something on
https://nickc1.github.io/python,/matlab/2016/05/17/Standard-Deviation-(Filters)-in-Matlab-and-Python.html
from scipy.ndimage.filters import generic_filter
import numpy as np
I_filt = generic_filter(I, np.std, size=3)
maybe it can help!
(not you I guess, given the 3 years delay, but someone on the net)
Related
I'm trying to compose two functions and I get a bizzare result
'''
#!/usr/bin/python
from sympy import *
init_printing(use_unicode=True)
x= symbols('x')
f = x/(x+1);
g = x/(x+2);
print(compose(f,g))
This shows : x/((x + 1)*(x + 2))
Should be x/(2x+2)
I don't get it. Does anyone has an idea?
Thanks
Despite being available in the top-level sympy namespace under the plain name compose, sympy.compose doesn't actually do general function composition.
sympy.compose is actually sympy.polys.polytools.compose. It's actually a function for polynomial composition. When you try to compose x/(x+1) and x/(x+2), it ends up interpreting these inputs as multivariate polynomials in 3 variables, x, 1/(x+1), and 1/(x+2), and the results are total nonsense.
Using Binance Futures API I am trying to get a proper form of my position regarding cryptocurrencies.
Using the code
from binance_f import RequestClient
request_client = RequestClient(api_key= my_key, secret_key=my_secet_key)
result = request_client.get_position()
I get the following result
[{"symbol":"BTCUSDT","positionAmt":"0.000","entryPrice":"0.00000","markPrice":"5455.13008723","unRealizedProfit":"0.00000000","liquidationPrice":"0","leverage":"20","maxNotionalValue":"5000000","marginType":"cross","isolatedMargin":"0.00000000","isAutoAddMargin":"false"}]
The type command indicates it is a list, however adding at the end of the code print(result) yields:
[<binance_f.model.position.Position object at 0x1135cb670>]
Which is baffling because it seems not to be the list (in fact, debugging it indicates object of type Position). Using PrintMix.print_data(result) yields:
data number 0 :
entryPrice:0.0
isAutoAddMargin:True
isolatedMargin:0.0
json_parse:<function Position.json_parse at 0x1165af820>
leverage:20.0
liquidationPrice:0.0
marginType:cross
markPrice:5442.28502271
maxNotionalValue:5000000.0
positionAmt:0.0
symbol:BTCUSDT
unrealizedProfit:0.0
Now it seems like a JSON format... But it is a list. I am confused - any ideas how I can convert result to a proper DataFrame? So that columns are Symbol, PositionAmt, entryPrice, etc.
Thanks!
Your main question remains as you wrote on the header you should not be confused. In your case you have a list of Position object, you can see the structure of Position in the GitHub of this library
Anyway to answer the question please use the following:
df = pd.DataFrame([t.__dict__ for t in result])
For more options and information please read the great answers on this question
Good Luck!
you can use that
df = pd.DataFrame([t.__dict__ for t in result])
klines=df.values.tolist()
open = [float(entry[1]) for entry in klines]
high = [float(entry[2]) for entry in klines]
low = [float(entry[3]) for entry in klines]
close = [float(entry[4]) for entry in klines]
I haven't seen this specific scenario in my research for this error in Numba. This is my first time using the package so it might be something obvious.
I have a function that calculates engineered features in a data set by adding, multiplying and/or dividing each column in a dataframe called data and I wanted to test whether numba would speed it up
#jit
def engineer_features(engineer_type,features,joined):
#choose which features to engineer (must be > 1)
engineered = features
if len(engineered) > 1:
if 'Square' in engineer_type:
sq = data[features].apply(np.square)
sq.columns = map(lambda s:s + '_^2',features)
for c1,c2 in combinations(engineered,2):
if 'Add' in engineer_type:
data['{0}+{1}'.format(c1,c2)] = data[c1] + data[c2]
if 'Multiply' in engineer_type:
data['{0}*{1}'.format(c1,c2)] = data[c1] * data[c2]
if 'Divide' in engineer_type:
data['{0}/{1}'.format(c1,c2)] = data[c1] / data[c2]
if 'Square' in engineer_type and len(sq) > 0:
data= pd.merge(data,sq,left_index=True,right_index=True)
return data
When I call it with lists of features, engineer_type and the dataset:
engineer_type = ['Square','Add','Multiply','Divide']
df = engineer_features(engineer_type,features,joined)
I get the error: Failed at object (analyzing bytecode)
'DataFlowAnalysis' object has no attribute 'op_MAKE_FUNCTION'
Same question here. I think the problem might be the lambda function since numba does not support function creation.
I had this same error. Numba doesnt support pandas. I converted important columns from my pandas df into bunch of arrays and it worked successfully under #JIT.
Also arrays are much faster then pandas df, incase you need it for processing large data.
I want print a polynomial equation for the given data. I am using this line of code, however I am not sure this the correct way to get the polynomial equation for the given data
fit2 = numpy.polyfit(temp[0][0],temp[0][1],deg=2)
y1=numpy.poly1d(fit2)
Please advise
Sympy has some nice features do that simply:
from sympy import Symbol,expand
fit2 = numpy.polyfit(temp[0][0],temp[0][1],deg=2)
y1=numpy.poly1d(fit2)
x=Symbol('x')
print(expand(y1(x)))
for:
-2.6666666666668*x**3 + 41.0000000000018*x**2 - 195.333333333342*x + 323.000000000013
BELOW is the code for the problem MAXCOUNT from codechef.com. The code on submission shows NZEC error.
The code takes first input t as no. of test cases, then for each test case takes input n a integer and the next line consists of space separated n integers Basically I need to return the maximum occuring integer and its count as output.
import numpy as np
import sys
t = int(raw_input())
for i in xrange(t):
n = raw_input()
n = int(n)
a = []
a = map(int, raw_input().split())
print a
count = np.bincount(a)
print np.argmax(count),np.max(count)
sys.exit(0)
Someone please help me out with this error.
The answer to your question is the use of the numpy module, which is not part of the standard library used on CodeChef. If you need to check numpy or another module in an online programming judge, a good way is to use a code sample that you know works and then add the import statement to the top before resubmitting.
For CodeChef in particular, try the basic test with the following code, with and without the import statement:
#Test for modules
import numpy
number_in = int(raw_input())
while number_in != 42:
print number_in
number_in = int(raw_input())
As a suggestion, the Counter() function from the collections module will work on CodeChef, you may want to try that instead of numpy. I find however that for many of the problems on that site without numpy or using PyPy it can be quite difficult to meet the timing requirements for solutions.