How to use Wildcard in Numpy array operations? - python

For instance, define a numpy array with numpy.str_ format and doing the replace operation supported by numpy.char:
import numpy as np
a = np.array(['a-b-c-d','e-f-g-h'],np.str_)
print (np.char.replace(a,'*-','i-'))
The returned result would be ['a-b-c-d', 'e-f-g-h'] but ['i-i-i-d', 'i-i-i-h'] is expected.

Is there any reason to use numpy arrays? You can't use wildcards with numpy.char.replace.
I would suggest to use python lists here and the re module:
l = ['a-b-c-d', 'e-f-g-h']
import re
out = [re.sub('.-', 'i-', i) for i in l]
Output: ['i-i-i-d', 'i-i-i-h']

Related

return a 2d array without brackets

I'm trying to create a function that returns a 2d array without the brackets on the sides of the array, And I can't use print since I'm using this function for a discord bot and want to return the array instead of printing it.
Here's some of my code:
import numpy as np
example_array = np.array([["⚪", "⚪", "⚪"], ["⚪", "⚪", "⚪"], ["⚪", "⚪", "⚪"]])
def get_array():
for row in example_array:
return "".join(map(str,row))
X = print_array()
Here's the output if I print/send X:
⚪⚪⚪
How can I use a function to return the full array?
I think what you want to do is something like this
"".join(["".join(i) for i in example_array])
would give us
'⚪⚪⚪⚪⚪⚪⚪⚪⚪'

Import Variable Substitution Matrices based on string input Python

I am trying to import a substitution matrix to implement the Needleman-Wunsch algorithm in Python based on a given input.
If I want to select one Matrix I can do it like this:
from Bio.SubsMat import MatrixInfo as matlist
scoring_mat = matlist.blosum62
What would I have to do If I wanted to import any matrix based on an input? I have this for the moment:
def blosum(name):
index = str(name)
x= "blosum"+index
return x
a= blosum(62)
scoring_mat = matlist.a
Unfortunately, it is giving me the following error:
AttributeError: module 'Bio.SubsMat.MatrixInfo' has no attribute 'a'
What would I have to do to make it work?
Thanks in advance!
Try scoring_mat = getattr(matlist,a) instead. It worked for me.

Pickling a Function and Include the Imports

My idea is this, so I want to allow users to send code to a web endpoint.
I would like to grab the modules/imports from the method.
Is that possible?
import pickle
import inspect
def add(x,y):
return x+y
def stringify_method(func):
""""""
return pickle.dumps(func)
print(pickle.loads(stringify_method(add))(1,2))
3
So it returns 3, which is expected.
Now let's say I have something more complicated:
import sys
import pickle
import inspect
import arcpy
import pandas as pd
import numpy as np
def create_array(array):
return np.array(array)
def unpickle_method(func):
""""""
return pickle.dumps(func)
print(pickle.loads(stringify_method(create_array))([1,2,3,4]))
I get the method just fine, but the modules do not follow. How do I get my import numpy as np and pandas, etc..?
Not exactly sure what you are trying to do, but is this helpful?
>>> import sys
>>> import numpy as np
>>> import inspect
>>>
>>> [x[0] for x in inspect.getmembers(sys.modules[__name__], inspect.ismodule)]
['__builtins__', 'inspect', 'np', 'sys']
this gets me 1/2 way there, I need to be able to then grab the import statements from the method: import numpy as np
You could probably reconstruct the statements from this:
>>> [(x, y.__name__) for x,y in inspect.getmembers(sys.modules[__name__], inspect.ismodule)]
[('__builtins__', 'builtins'), ('inspect', 'inspect'), ('np', 'numpy'), ('sys', 'sys')]
(note the ('np', 'numpy') element which tells you import numpy as np)

python np.c_ error"CClass object is not callabel"

I'm using ipython 4.0.1 and python 3.5.1, when I call np.c_(), it shows an error
CClass object is not callable.
This is my code:
import numpy as np
rows = []
with open('ntumlone.dat') as f:
rows = [list(map(float, L.split())) for L in f]
arr = np.array(rows)
date = np.c_(np.ones(len(arr)), arr)
What's wrong with me?
Try
date = np.c_[np.ones(len(arr)), arr]
Check its docs. You 'call' it with square brackets, as though you are indexing, not with (). If the distinction is too confusing stick with concatenate or one of the stack functions. I think in this use it is the same as vstack.

Signal filtering in Python

Trying to create a filter for a signal i made, but can't seem to get. I keep getting error about '/' whenever i call my filter function.
import numpy as np
from numpy import sqrt
import matplotlib.pyplot as plt
import scipy as sp
import math
def sinyal(N,c,f,p):
y=np.zeros(N)
t=np.linspace(0,2*np.pi,N)
Nf=len(c)
for i in range(Nf):
y+=c[i]*np.sin(f[i]*t)
return y;
def filter (w,wn):
yf=1.0/sqrt(1.0+(w/wn)**(2.0*n))
return yf;
c=[2,5,10]
f=[50, 150, 300]
p=[0,0]
N=2000
w=[50,150,300]
x=np.linspace(0,2.0*math.pi,N)
y=sinyal(N,c,f,p)
yf=filter(w,200)
plt.plot(x[:100],y[:100])
plt.show()
w is a list, wn is an int, so w/wn raises
TypeError: unsupported operand type(s) for /: 'list' and 'int'
You could fix the error by making w a NumPy array:
w = np.array([50, 150, 300])
You'll also need to define n.
filter is called but the result is never used.
Tip: note that filter is the name of a Python builtin. It's
best not to define a function or variable the same name as a builtin,
since it makes it harder to access the builtin and may be confusing
to others.
Tip: Python does not need semicolons at the end of statements.

Categories

Resources