getting nonetype after using numpy.fill - python

I want to use numpy array as the following:
room_1_class_value = numpy.empty(len(room_1_indices[0])).fill(1)
room_1_indices[0] is an array, and I can see the output of that array. But why I get "None" for "room_1_class_value"?
Thanks,

Because you're not assigning a numpy array to room_1_class_value. You're assigning to it the value of the fill(1) function call. This function doesn't return anything.
Try:
room_1_class_value = numpy.empty(len(room_1_indices[0]))
room_1_class_value.fill(1)

Related

How do I print name of array in Python?

I have few arrays, in my code. I wanna be able to change, which I am using in one place, and to be able to print name of it only changing one line (definition).
Example:
XYZ=my_array #definition of which array I am using now I am calling only XYZ
#some_code
print('the name of array I am using is my_array')
Now I want to have in print being to able call XYZ array not my_array. So I don't have to change it twice, but It will show the same output.
How do I that?
you can use a class to store the array and the name, then you can access with
.name o .array
class Foo():
def __init__(self, array, name):
self.array = array
self.name = name
my_array = [1,2,3,4]
XYZ=Foo(my_array, "name")
print(XYZ.array)
print(XYZ.name)
To print an array in Python, use the print() function. The print() is a built-in Python function that takes the name of the array containing the values and prints it. To create an array in Python, use the numpy library and create an array using the np.array() function, and then print that array in the console.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
There are many ways to answer this question.
This is one of the ways that i would do it.
You can store your list in a dict and assign a key(basically a unique name) to it and you can call it at your disposal when u want it.
_my_dict = {
"my_first_array" : [1,2,3],
"my_second_array" : [4,5,6],
}
# this is how to get all the names of the list
print(_my_dict.keys()) # ['my_first_array','my_second_array']
# this is how to access your list by name
print(_my_dict['my_first_array']) # [1,2,3]

TypeError: Cannot interpret '10000' as a data type

I am writing the following code for a deep learning program in python but it is repeatedly giving me errors.
import numpy as np
def vectorize_sequences(sequences,dimension=10000):
results=np.zeros((len(sequences)),dimension)
for i,sequence in enumerate(sequences):
results[i,sequence]=1
return results
error-TypeError: Cannot interpret '10000' as a data type
You need to change the line results=np.zeros((len(sequences)),dimension). Here dimension is being passed as the second argument, which is supposed to be the datatype that the zeros are stored as. Change it to:
results = np.zeros((len(sequences), dimension))

Calculating the Downside Deviation of an Array of Returns

I'm trying to calculate the downside deviation of an array of returns using the code below:
def downside_deviation(arr):
downside_returns = 0
arr.loc[arr < 0, 'downside_returns'] = arr
down_stdev = downside_returns**2
arraysize = downside_returns.count()
down_stdev = downside_returns.sum()/arraysize
down_stdev = np.sqrt(down_stdev)*np.sqrt(12)
return down_stdev
But I keep encountering the and AttributeError as below:
AttributeError: 'float' object has no attribute 'loc'
I'm wondering if anyone could me on this error as nothing I have tried has worked so far.
Thanks a million for the help in advance!
It seems like the arr variable should a Pandas DataFrame, but you passed the float object for the arr variable. So, it raises the AttributeError: 'float' object has no attribute 'loc'.
Additionally, I see this arr.loc[arr < 0, 'downside_returns'] = arr might raise the next error if your arr is actually a Pandas DataFrame. To use it correctly, you may need to read more in its documentation (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html) - for example, df.loc[df['shield'] > 6, ['max_speed']].
You're passing a float into the function, but it expects it to be a type that has a .loc attribute.
Python is "duck typed". You can pass anything you want as the argument, but when it comes time to use the members, if they are not present you will get an error like this.

"How to use append or extend function using def function in python?"

I am trying to append or extend values by using "def" function, but, I am getting an error numpy.float64 object is not iterable
Basically, I want to store different slopes values in the variable name "all_slope" by using extend or append function. I am passing four different values in call function that is a slope. Would, it possible to help me?
all_slope=[]
def slope(x1,x2,y1,y2):
x=x2-x1
y=y2-y1
slope_value=(y/x)
all_slope.extend(slope_value)
return all_slope
slope(3,2,4,2)
Use append instead of extend:
Why:
extend: Extends list by appending elements from the iterable.
append: Appends object at the end.
Read more..
Hence:
all_slope=[]
def slope(x1,x2,y1,y2):
x=x2-x1
y=y2-y1
slope_value=(y/x)
all_slope.append(slope_value)
return all_slope
print(slope(3,2,4,2))
OUTPUT:
[2.0]
EDIT:
Good catch by # mfitzp, Since all_slope is a global var, you could just call the function and then print the list without return:
all_slope=[]
def slope(x1,x2,y1,y2):
x=x2-x1
y=y2-y1
slope_value=(y/x)
all_slope.append(slope_value)
slope(3,2,4,2)
print(all_slope)
change all_slope.extend(slope_value) to all_slope.append(slope_value)

Reshape Numpy Array: 'list' object is not callable

I have a method, get_input_representation that returns a numpy array
np.array = input_stack + input_buffer
return np.array
In another a different part of the program, I call the above method, save its return value and reshape it.
state_rep = self.extractor.get_input_representation(words, pos, state)
reshaped_state = np.array(state_rep).reshape(-1,6)
However, I get:
reshaped_state = np.array(state_rep).reshape(-1,6) TypeError: 'list'
object is not callable
I have also tried:
reshaped_state = np.array(self.extractor.get_input_representation(words, pos, state)).reshape(-1,6)
But I get the same list object is not callable. Where is the error in my code and how can I go about fixing it?
I think you should not be assigning default Numpy methods to a variable (even if it is inside a function) ie instead of np.array = input_stack + input_buffer and then return np.array you should have return input_stack + input_buffer in your function
Would you need brackets instead of parens?
np.array[state_rep].reshape(-1,6)
I think you are trying to index into the np array right?

Categories