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.
Solution was linked on reshaped method on documentation page.
Insted of Y.reshape(-1,1) you need to use:
Y.values.reshape(-1,1)
The solution is indeed to do:
Y.values.reshape(-1,1)
This extracts a numpy array with the values of your pandas Series object and then reshapes it to a 2D array.
The reason you need to do this is that pandas Series objects are by design one dimensional. Another solution if you would like to stay within the pandas library would be to convert the Series to a DataFrame which would then be 2D:
Y = pd.Series([1,2,3,1,2,3,4,32,2,3,42,3])
scaler = StandardScaler()
Ys = scaler.fit_transform(pd.DataFrame(Y))
You can convert your list to a numpy array and then reshape it with numpy.reshape
import numpy as np
# Convert to numpy array
w_train = np.array(w_train)
w_test = np.array(w_train)
# Reshape
w_train = np.reshape(w_train, (2404,28,224,224,3))
w_test = np.reshape(w_test, (601,28,224,224,3))
reshape is a methode under numpy's library and as the error printed in your terminal the object list has no methode defined as reshape
import numpy as np
w_train=np.array(x_train)
then you can simply use the reshape function without any error :
w_train=x_train.reshape((2404,28,224,224,3))
you can check the reshape characteristics in this link for more understanding of the methode
Probably there's something wrong with the input values for X and/or T. The function from the question works ok:
import numpy as np
from math import e
def sigmoid(X, T):
return 1.0 / (1.0 + np.exp(-1.0 * np.dot(X, T)))
X = np.array([[1, 2, 3], [5, 0, 0]])
T = np.array([[1, 2], [1, 1], [4, 4]])
print(X.dot(T))
# Just to see if values are ok
print([1. / (1. + e ** el) for el in [-5, -10, -15, -16]])
print()
print(sigmoid(X, T))
Result:
[[15 16]
[ 5 10]]
[0.9933071490757153, 0.9999546021312976, 0.999999694097773, 0.9999998874648379]
[[ 0.99999969 0.99999989]
[ 0.99330715 0.9999546 ]]
Probably it's the dtype of your input arrays. Changing X to:
X = np.array([[1, 2, 3], [5, 0, 0]], dtype=object)
Gives:
Traceback (most recent call last):
File "/[...]/stackoverflow_sigmoid.py", line 24, in <module>
print sigmoid(X, T)
File "/[...]/stackoverflow_sigmoid.py", line 14, in sigmoid
return 1.0 / (1.0 + np.exp(-1.0 * np.dot(X, T)))
AttributeError: exp
You convert type np.dot(X, T) to float32 like this:
z=np.array(np.dot(X, T),dtype=np.float32)
def sigmoid(X, T):
return (1.0 / (1.0 + np.exp(-z)))
Hopefully it will finally work!
It is possible to return a value of type None, check the type of X_train in the following lines:
CopyX_train = X_train.resize((32, 32))
type(X_train)
X_train = X_train.reshape((len(X_train), 3, 32, 32))
type(X_train)
only the type of array can use reshape() and it can not change the number of the data your array contains. Maybe you can try something like this:
Copyimport numpy as np
from PIL import Image
import matplotlib as plt
x_train = Image.open('skyscraper.jpg')
x_train = x_train.resize((32,32))
x_train = np.array(x_train)
x_train = x_train.reshape((3,32,32))
print(x_train)
pandas.dataframe doesn't have a built-in reshape method, but you can use .values to access the underlying numpy array object and call reshape on it:
start = 0
for i in range(0, len(df.index)):
if (i + 1)%10 == 0:
result = df.iloc[start:i+1].values.reshape(2,5)
start = i + 1
print result
#[[ 52.1 32.2 44.6 99.1 12.3]
# [ 43.2 79.4 45.5 56.3 15.4]]
#[[ 35.7 23.7 66.7 33.8 12.9]
# [ 34.8 21.6 43.7 44.2 55.8]]
Simplest Answer
df = pd.read_csv("test.csv", header=None,usecols=[1])
df.values.reshape(-1,5)
array([[52.1, 32.2, 44.6, 99.1, 12.3],
[43.2, 79.4, 45.5, 56.3, 15.4],
[35.7, 23.7, 66.7, 33.8, 12.9],
[34.8, 21.6, 43.7, 44.2, 55.8]])
The error is pretty clear. you are trying to replace characters in a float. x probably is a float. you might want to do str(x) which gets you
f["text"] = [str(x).replace(':',' ') for x in df["text"]]
but i can't see a situation where a float contains a :
In my opinion problem is missing value in column, so use pandas methods Series.str.replace or Series.replace instead list comprehension:
df["text"] = df["text"].str.replace(':',' ')
Or:
df["text"] = df["text"].str.replace(':',' ', regex=True)
Solution with list comprehension is possible, only need if-else statement for test strings:
df["text"] = [x.replace(':',' ') if isinstance(x, str) else x for x in df["text"]]
Sample:
df = pd.DataFrame({'text':['qq:ww','e:', np.nan]})
df["text"] = df["text"].str.replace(':',' ')
print (df)
text
0 qq ww
1 e
2 NaN