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
'⚪⚪⚪⚪⚪⚪⚪⚪⚪'
Related
I was asking myself if it is possible to turn the output of a class into a np.array within the class itself.
I created the following class:
class stats:
def __init__( self, x ):
self.age = x[:,0]
self.education = x[:,1]
self.married = x[:,2]
self.nodegree = x[:,3]
self.RE75 = x[:,4]
self.RE78 = x[:,5]
def Vector( self ):
age = [np.mean(self.age), st.stdev(self.age)]
education = [np.mean(self.education), st.stdev(self.education)]
married = [np.mean(self.married), st.stdev(self.married)]
nodegree = [np.mean(self.nodegree), st.stdev(self.nodegree)]
RE75 = [np.mean(self.RE75), st.stdev(self.RE75)]
RE78 = [np.mean(self.RE78), st.stdev(self.RE78)]
return [age, education, married, nodegree, RE75, RE78]
results1 is a numpy.ndarray of shape 156x6.
I basically want to compute the mean as well as standard deviation for each column of results1 using a class. I use numpy to compute the mean and statistics for the std.
When I am printing the output I get the following:
results1_stats = stats(results1)
print(results1_stats.Vector())
Output:
[[25.98076923076923, 7.299554695959556], [10.314102564102564, 2.0597666237347005], [0.1858974358974359, 0.39027677820527085], [0.7243589743589743, 0.448275807219502], [1490.7220884615383, 3296.5535502409775], [6136.320646794872, 8143.4659725229685]]
Apparently, the class is working as wanted (although there is probablly a more efficent way to code this up).
The problem is, that I would lilke to get the output in a np.array of shape 6x2 (or transposed) directly from the class itself. However, since I just began using classes I don't know if that is even possible.
Any help is appreciated :)
Thank you!
You can construct an numpy array using np.array(your_list_sequence). Additionally, you can use list comprehension to convert list of lists to numpy array. More info here.
Try this:
def get_stats(results):
return np.array([np.array([np.mean(results[:, column]), st.stdev(results[:, column])]) for column in range(6)])
your_new_np_array = get_status(results)
Although, if you want only stats array, having a function for this instead of class would be better and simpler. But, you can easily include that method in your class and get back your expected result.
I have the following array:
a = np.random.rand(5,2)
a
array([[0.98736372, 0.07639041],
[0.45342928, 0.4932295 ],
[0.75789786, 0.48546238],
[0.85854235, 0.74868237],
[0.13534155, 0.79317482]])
and I want to resize it so that it is divided into 2 batches with three elements (adding zeros as required):
array([[[0.98736372, 0.07639041],
[0.45342928, 0.4932295 ],
[0.75789786, 0.48546238]],
[[0.85854235, 0.74868237],
[0.13534155, 0.79317482],
[0, 0]]])
I have tried this, but it returns None:
a = a.copy()
a.resize((2,3,2), refcheck = False)
I believe .reshape would not provide the solution, as it is not able to fill in with 0's to comply with the desired dimensions for the array.
Using numpy.resize, you have to use like this:
import numpy as np
a = np.random.rand(5,2)
b = np.resize(a, (2,3,2))
otherwise you can use the object method to get the same result, like this:
import numpy as np
a = np.random.rand(5,2)
a.np.resize(2,3,2)
b = a.copy()
note the first one return ndarray and the last one returns None because It changes the object itself. For more info, look the numpy.resize doc
I'm trying to write a function that will take an image as input and then return a function that will take an index and return the color of that index. This is what I got so far:
def generator(image):
def index2color(index1):
return image[index1]
return index2color
Lets try it:
generator2 = generator("flower.jpg")
print (generator2((3)))
>>> w
So this will return the index in the string but not the index for the picture. Any suggestions would be greatly appreciated!
You need to read the image in to an appropriate object first, then operate on the image object. Like this:
flower_img = cv2.imread('flower.jpg')
generator2 = generator(flower_img)
Consider working through this for a better understanding of what you can do: https://docs.opencv.org/3.1.0/d3/df2/tutorial_py_basic_ops.html
First post so I'll try to be specific as possible.
I'm trying to define a term ybar1 in Python. I'm intending for ybar1 to be the average of a list y1
where y1 = [1, 2, 3, 4, 5]
What I'm doing first is:
import numpy as np
then defining a new function
def funk(y1):
print np.sum(y1)/len(y1)
return;
So now when I compute funk(y1) I get a number, 3
Now is where it gets weird. I try to say:
ybar1 = funk(y1)
, which returns the value 3
but then when I type ybar1 alone, I get no output.
Thoughts?
Try this:
def funk(y1):
return np.sum(y1)/len(y1)
You were not actually returning a value from the function, only printing a result on the console. Also, there's a better way to compute the average using numpy:
def funk(y1):
return np.mean(y1)
This is an issue specific to the use of python scripts in booggie 2.
I want to return multiple strings to the sequence and store them there in variables.
The script should look like this:
def getConfiguration(config_id):
""" Signature: getConfiguration(int): string, string"""
return "string_1", "string_2"
In the sequence I wanna have this:
(param_1, param_2) = getConfiguration(1)
Please note: The booggie-project does not exist anymore but led to the development of Soley Studio which covers the same functionality.
Scripts in booggie 2 are restricted to a single return value.
But you can return an array which then contains your strings.
Sadly Python arrays are different from GrGen arrays so we need to convert them first.
So your example would look like this:
def getConfiguration(config_id):
""" Signature: getConfiguration(int): array<string>"""
#TypeHelper in booggie 2 contains conversion methods from Python to GrGen types
return TypeHelper.ToSeqArray(["string_1", "string_2"])
return a tuple
return ("string_1", "string_2")
See this example
In [124]: def f():
.....: return (1,2)
.....:
In [125]: a, b = f()
In [126]: a
Out[126]: 1
In [127]: b
Out[127]: 2
Still, it's not possible to return multiple values but a python list is now converted into a C#-array that works in the sequence.
The python script itself should look like this
def getConfiguration(config_id):
""" Signature: getConfiguration(int): array<string>"""
return ["feature_1", "feature_2"]
In the sequence, you can then use this list as if it was an array:
config_list:array<string> # initialize array of string
(config_list) = getConfigurationList(1) # assign script output to that array
{first_item = config_list[0]} # get the first string("feature_1")
{second_item = config_list[1]} # get the second string("feature_2")
For the example above I recommend using the following code to access the entries in the array (in the sequence):
config_list:array<string> # initialize array of string
(config_list) = getConfigurationList(1) # assign script output to that array
{first_item = config_list[0]} # get the first string("feature_1")
{second_item = config_list[1]} # get the second string("feature_2")