The error is reproducible if the array is of dtype=object:
import numpy as np
label0 = np.random.random((50, 3)).astype(object)
np.cov(label0, rowvar=False)
AttributeError: 'float' object has no attribute 'shape'
If possible you should convert it to a numeric type. For example:
np.cov(label0.astype(float), rowvar=False) # works
Note: object arrays are rarely useful (they are slow and not all NumPy functions deal gracefully with these - like in this case), so it could make sense to check where it came from and also fix that.
The error is reproducible if the array is of dtype=object:
import numpy as np
label0 = np.random.random((50, 3)).astype(object)
np.cov(label0, rowvar=False)
AttributeError: 'float' object has no attribute 'shape'
If possible you should convert it to a numeric type. For example:
np.cov(label0.astype(float), rowvar=False) # works
Note: object arrays are rarely useful (they are slow and not all NumPy functions deal gracefully with these - like in this case), so it could make sense to check where it came from and also fix that.
try
label0.astype(float32)
and then calculate your cov.
It might because your dtype is object.
fitting a single series causing torch to throw 'float' object has no attribute 'shape'
AttributeError: 'float' object has no attribute 'shape'
[BUG] 'float' object has no attribute 'shape' in Data pipelines
BUG: <Error in the Numpy 'cov()' function: ' prefix>
Videos
I got the same problem and was trying to find a solution but did not see the answer I was looking for. So I guess provide an answer here may help people like me.
The problem here is that the type of df.total_bill is object instead of float.
So the solution is to change it to float befor pass the dataframe to seaborn:
df.total_bill = df.total_bill.astype(float)
This is a rather unusual way of creating a dataframe. The resulting dataframe also has some very strange properties, e.g. it has a length of 50 but the last index is 88. I'm not going into debugging these nested loops. Instead, I would propose to create the dataframe from some numpy array, e.g. like
import numpy as np
import pandas as pd
time = ['day','night']
sex = ['female','male']
smoker = ['yes','no']
data = np.repeat(np.stack(np.meshgrid(time, sex, smoker), -1).reshape(-1,3), 10, axis=0)
df = pd.DataFrame(data, columns=["time","sex","smoker"])
df["total_bill"] = np.random.rand(len(df))*10
Then also plotting works fine:
g = sns.factorplot(x="sex", y="total_bill", hue="smoker", col="time", data=df,
kind="violin", size=4, aspect=.7)

Try this instead,
print(
"{:.3f}% {} ({} sentences)".format(pcent, gender, nsents)
)
Refer the latest docs for more examples and check the Py version!
You could also use {:.3%} instead of {:.3f}%.
It will transform the value into percentages automatically.
That means "{:.3%}".format(0.3) will print "30%" while you have to write "{:.3f}%".format(0.3 * 100) to get "30%" as well.
I'm implementing a cost function for a neural network, which takes in the predictions made on the training set by the forward propagation step, the true output labels Y, and calculates the cost of the model. The function definition is -
def calculate_cost (Y, prediction, activation_output = 'sigmoid'):
"""
Calculates the cost.
Takes in the output labels and the prediction from forward propagation, as well as the activation function of the output layer.
Returns the cost.
"""
m = Y.shape[1]
if activation_output.lower() == 'sigmoid':
cost = (1/m)*np.sum(Y*np.log(prediction) + (1-Y)*np.log(1-prediction))
if activation_output.lower() == 'softmax':
cost = (1/m)*np.sum(np.sum(Y*np.log(prediction), axis = 0, keepdims = True))
return costI get an error on the line where I declare m as the second dimension of the input parameter Y.
<ipython-input-6-0c38b0031612> in calculate_cost(Y, prediction, activation_output)
10 """
---> 11 m = Y.shape[1]
12
13 if activation_output.lower() == 'sigmoid':
AttributeError: 'function' object has no attribute 'shape'Why is this happening? Y is clearly a numpy array.
I am calling a function model() which I have defined which takes in the input data set, the corresponding output labels, and a few hyperparameters and trains the model. This is the function definition for the model() function -
def model (X, Y, architecture, activation_functions, learning_rate = 0.001, print_cost = True, number_of_iterations = 10000):
"""
Takes in the training set X, the labels Y, and all the required parameters and trains the defined model for the given number of iterations.
Prints the cost if print_cost is true.
"""
costs = []
number_of_layers = len(architecture) - 1
parameters = initialize_parameters(architecture)
for i in range(number_of_iterations):
prediction, cache = forward_propagation(X, parameters, number_of_layers, activation_functions)
cost = calculate_cost(Y, prediction, activation_functions[-1])
costs.append(cost)
gradients = backward_propagation(X, Y, cache, number_of_layers)
parameters = update_parameters(parameters, number_of_layers, gradients, alpha=0.01)
if print_cost and i%100 == 0:
print(f'Completed {i} iterarions.\n')
print(f'Cost after iteration {i} = {cost}')
plt.plot(costs)
plt.xlabel('Iterations (in hundereds)')
plt.ylabel('Cost')
plt.title(f'Learning rate = {learning_rate}')
plt.show()
print(f'Ran {number_of_iterations} iterations. Returning parameters now.')
print(f'The final cost of the model was: {costs[-1]}')
print(f'The training accuracy was: {calculate_accuracy(X, Y, parameters, number_of_layers, activation_functions)}')
return parametersI am calling the function like this -
X_train = pd.read_csv('training_set.csv', header = None).to_numpy()
Y_train = pd.read_csv('training_labels.csv', header = None).to_numpy
model(X_train, Y_train, [10,5,5,1], ['relu','relu','relu','relu','sigmoid'])The second parameter Y_train gets passed to the calculate_cost() function. And Y_train is clearly an array:/