Python ValueError:无法将字符串转换为浮点:';W';
我一直在尝试用熊猫乳腺癌来建立一个浅层神经网络,我一直在犯这个错误,如果有人能告诉我到底是什么错误以及如何修复它,我将不胜感激Python ValueError:无法将字符串转换为浮点:';W';,python,pandas,numpy,neural-network,Python,Pandas,Numpy,Neural Network,我一直在尝试用熊猫乳腺癌来建立一个浅层神经网络,我一直在犯这个错误,如果有人能告诉我到底是什么错误以及如何修复它,我将不胜感激 File "D:\Users\USUARIO\Desktop\una carpeta para los oasda proyectos\Ex_Files_Python_EssT\Exercise Files\basic_hands_on.py", line 55, in predict np.array(WT, dtype=np.float3
File "D:\Users\USUARIO\Desktop\una carpeta para los oasda proyectos\Ex_Files_Python_EssT\Exercise Files\basic_hands_on.py", line 55, in predict
np.array(WT, dtype=np.float32)
ValueError: could not convert string to float: 'W'
我试图将字典上的W值转换为float32,因为我需要它来实际处理predict函数上的方程,但我一直得到“W”的类型是字符串,尽管print([W])给了我一个矩阵
这是出于上下文的考虑而编写的代码
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
def initialiseNetwork(num_features):
W = np.zeros((num_features, 1))
b = 0
parameters = {"W": W, "b": b}
return parameters
def sigmoid(z):
a = 1/(1 + np.exp(-z))
return a
def forwardPropagation(X, parameters):
W = parameters["W"]
b = parameters["b"]
Z = np.dot(W.T,X) + b
A = sigmoid(Z)
return A
def cost(A, Y, num_samples):
cost = -1/num_samples *np.sum(Y*np.log(A) + (1-Y)*(np.log(1-A)))
return cost
def backPropagration(X, Y, A, num_samples):
dZ = A - Y
dW = (np.dot(X,dZ.T))/num_samples
db = np.sum(dZ)/num_samples
return dW, db
def updateParameters(parameters, dW, db, learning_rate):
W = parameters["W"] - (learning_rate * dW)
b = parameters["b"] - (learning_rate * db)
return {"W": W, "b": b}
def model(X, Y, num_iter, learning_rate):
num_features = X.shape[0]
num_samples = (X.shape[1])
print(num_samples)
parameters = initialiseNetwork(num_features)
for i in range(num_iter):
A = forwardPropagation(X, parameters)
if(i%100 == 0):
print("cost after {} iteration: {}".format(i, cost(A, Y, num_samples)))
dW, db = backPropagration(X, Y, A, num_samples)
parameters = updateParameters(parameters, dW, db, learning_rate)
return parameters
def predict(W, b, X):
WT = np.transpose(["W"])
np.array(WT, dtype=np.float32)
np.array(WT,dtype=float)
Z = np.dot(WT,X) + b
Y = np.array([1 if y > 0.5 else 0 for y in sigmoid(Z[0])]).reshape(1,len(Z[0]))
return Y
(X_cancer, y_cancer) = load_breast_cancer(return_X_y = True)
X_train, X_test, y_train, y_test = train_test_split(X_cancer, y_cancer,
random_state = 25)
def normalize(data):
col_max = np.max(data, axis = 0)
col_min = np.min(data, axis = 0)
return np.divide(data - col_min, col_max - col_min)
X_train_n = normalize(X_train)
X_test_n = normalize(X_test)
X_trainT = X_train_n.T
X_testT = X_test_n.T
y_trainT = y_train.reshape(1, (X_trainT.shape[1]))
y_testT = y_test.reshape(1, (X_testT.shape[1]))
parameters = model(X_trainT, y_trainT, 4000, 0.75)
print(parameters)
print(X_trainT)
yPredTrain = predict(['W'], ['b'], X_trainT) # pass weigths and bias from parameters dictionary and X_trainT as input to the function
yPredTest = predict(['W'], ['b'], X_testT) # pass the same parameters but X_testT as input data
accuracy_train = 100 - np.mean(np.abs(yPredTrain - y_trainT)) * 100
accuracy_test = 100 - np.mean(np.abs(yPredTest - y_testT)) * 100
print("train accuracy: {} %".format(accuracy_train))
print("test accuracy: {} %".format(accuracy_test))
with open("Output.txt", "w") as text_file:
text_file.write("train= %f\n" % accuracy_train)
text_file.write("test= %f" % accuracy_test)```
我正要骂你,因为你没有告诉我们问题出在哪里。但后来我碰巧将错误消息与
def predict(W, b, X):
WT = np.transpose(["W"])
np.array(WT, dtype=np.float32)
...
当然,这会产生这个错误。其中包含字符“W”的数组肯定不能转换为浮点
你使用了很多预测(['W'],但这不是其中一个。我本来想骂你,因为你没有告诉我们问题发生的地方。但是后来我碰巧将错误消息与
def predict(W, b, X):
WT = np.transpose(["W"])
np.array(WT, dtype=np.float32)
...
当然,这会产生这个错误。其中包含字符“W”的数组肯定不能转换为浮点
您可以使用
predict(['W']
许多地方,但这并不是其中之一。这里是出现问题的地方:
WT = np.transpose(["W"])
np.array(WT, dtype=np.float32)
这将创建一个数组,其中包含“W”
。不是变量W
,而是文本字符“W”
。我想您的意思是:
WT = np.transpose([W])
同样,当您调用
predict()
时,我假设您希望传入参数[“W”]
,而不是“W”
。请记住,“W”
只是一个字符,它与名为W
的变量不同。这里是出现问题的地方:
WT = np.transpose(["W"])
np.array(WT, dtype=np.float32)
这将创建一个数组,其中包含“W”
。不是变量W
,而是文本字符“W”
。我想您的意思是:
WT = np.transpose([W])
同样,当您调用
predict()
时,我假设您希望传入参数[“W”]
,而不是“W”
。请记住,“W”
只是一个字符,它与名为W
的变量不同。您的数据集有一个值为“W”的列。
您可以通过以下代码删除或删除该列:
dataset\u name=dataset\u name.drop(columns=[“column\u name”],axis=1)
您的数据集有一个值为“W”的列。
您可以通过以下代码删除或删除该列:
dataset\u name=dataset\u name.drop(columns=[“column\u name”]、axis=1)
谢谢,我的问题是没有正确调用字典。我忘了输入like“dictionaryname[“W”]”。谢谢,我的问题是没有正确调用字典。我忘了输入like“dictionaryname[“W”]”1.这并不能真正解决眼前的问题1.这并不能真正解决眼前的问题。。