class SigmoidNeuron:
def __init__(self):
self.w=None
self.b=None
def perceptron(self,x):
return np.dot(x,self.w.T)+self.b
def sigmoid(self,x):
return 1.0/(1.0+np.exp(-x))
def grad_w(self,x,y):
y_pred = self.sigmoid(self.perceptron(x))
return (y_pred-y)*y_pred*(1-y_pred)*x
def grad_b(self,x,y):
y_pred = self.sigmoid(self.perceptron(x))
return (y_pred-y)*y_pred*(1-y_pred)
def fit(self,x,y,epochs=1,learning_rate=1,initialise=True):
#initialise w,b
if initialise:
self.w=np.random.randn(1,X.shape[1])
self.b=0
for i in range(epochs):
dw=0
db=0
for x,y in zip(X,Y):
dw+=self.grad_w(x,y)
db+=self.grad_b(x,y)
self.w -= learning_rate*dw
self.b -= learning_rate*db
`
我正在运行一个S型神经网络代码,并且在运行带有数据的此类时遇到错误
X_scaled_train.astype(float)
array([[ 1.29929126, -0.90185206, 0.03173306, ..., -0.14142136,
-0.15523011, 0.21232515],
[-1.16225208, -0.86697607, 1.03451971, ..., -0.14142136,
-0.15523011, 0.21232515],
[ 1.77523922, 0.65594214, 0.03173306, ..., -0.14142136,
-0.15523011, 0.21232515],
...,
[ 1.44058831, -0.58796815, -0.66464655, ..., -0.14142136,
-0.15523011, 0.21232515],
[-1.42253612, 0.50481285, 1.54984063, ..., -0.14142136,
-0.15523011, 0.21232515],
[ 1.06875397, 0.6791928 , 0.97880934, ..., -0.14142136,
-0.15523011, 0.21232515]])
Y_scaled_train.astype(float)
array([[0.68],
[0.72],
[0.72],
[0.6 ],
[0.8 ],
[0.64],
[0.68],
这些是火车的数据 当我运行这条线时 sn.fit(X_scaled_train,Y_scaled_train,epochs = 10,learning_rate = 0.2) 我收到该类型错误 我该怎么做才能将其删除
错误显示
TypeError Traceback (most recent call last)
<ipython-input-167-51016d58d1f5> in <module>()
----> 1 sn.fit(X_scaled_train,Y_scaled_train,epochs=10,learning_rate=0.2)
2 frames
<ipython-input-25-2e09637c6d09> in perceptron(self, x)
4 self.b=None
5 def perceptron(self,x):
----> 6 return np.dot(x,self.w.T)+self.b
7 def sigmoid(self,x):
8 return 1.0/(1.0+np.exp(-x))
<__array_function__ internals> in dot(*args, **kwargs)
TypeError: Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'
答案 0 :(得分:1)
使用:
np.array(your_list)
your_list.values.astype(np.float)
或者:
new_list = [float(i) for i in your_list]
或者:
对于大型数组,我强烈建议您使用numpy:
np.array(your_list, dtype=np.float32)
np.array(your_list,dtype=float)
如果是浮点数,则无需分配:
np.array(your_list)
或者:
new_list = float("{:.1f}".format(float(input())))
list.append(new_list)