我一直在尝试使用大熊猫乳腺癌来获得浅层神经网络,而我一直保持这个错误,如果有人能说出真正的错误和解决方法,我将不胜感激。
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,因为我需要它实际处理预测函数上的方程,但是尽管 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)```
答案 0 :(得分:0)
我正因为没有向我们显示问题发生的位置而将您赶出场。但是后来我碰巧将错误消息与
匹配def predict(W, b, X):
WT = np.transpose(["W"])
np.array(WT, dtype=np.float32)
...
当然会产生此错误。当然,不能将其中带有字符“ W”的数组转换为浮点数。
您在多个地方使用predict(['W']
,但这不是其中一个。
答案 1 :(得分:0)
似乎是问题所在的地方:
WT = np.transpose(["W"])
np.array(WT, dtype=np.float32)
这将创建一个包含"W"
的数组。不是变量W
,而是文字字符"W"
。我认为您要输入的内容是:
WT = np.transpose([W])
同样,当您致电predict()
时,我想您想传递parameters["W"]
而不是"W"
。请记住,"W"
只是一个字符,它与名为W
的变量不同
答案 2 :(得分:0)
您的数据集有一列值为“W”。 您可以通过以下代码删除或删除该列:
dataset_name = dataset_name.drop(columns=["column_name"],axis = 1)