gpt4 book ai didi

TypeError:unsupported format string passed to function .__format__(TypeError:传递给函数的格式字符串不受支持。__FORMAT__)

转载 作者:bug小助手 更新时间:2023-10-25 10:37:40 31 4
gpt4 key购买 nike



Working on jupyter notebooks, I came across this problem:

在研究Jupyter笔记本电脑时,我遇到了这个问题:


TypeError:unsupported format string passed to function .__format__

This is where the code starts:

这是代码开始的地方:


def evaluate_preds(y_true, y_preds):
"""
Performs Evaluation comp on y_true labels and y_preds labels on a classification
"""
accuracy = accuracy_score(y_true,y_preds)
precision = precision_score(y_true,y_preds)
recall = recall_score(y_true,y_preds)
f1 = f1_score(y_true,y_preds)
metric_dict = {"accuracy": round(accuracy,2),
"precision": round(precision,2),
"recall": round(recall,2),
"f1": round(f1,2)}

print(f"Accuracy :{ accuracy_score*100:.2f}%")
print(f"Precision :{ precision_score:.2f}")
print(f"Recall :{ recall_score:.2f}")
print(f"F1 :{ f1_score:.2f}")

return metric_dict

This below code is being run on a different cell in jupyter notebook

下面的代码是在jupyter笔记本的另一个单元上运行的


from sklearn.ensemble import RandomForestClassifier

np.random.seed(42)

heart_disease_shuffled= heart_disease.sample(frac=1)

X= heart_disease_shuffled.drop("target",axis =1)
y = heart_disease_shuffled["target"]

train_split = round(0.7 * len(heart_disease_shuffled))
valid_split = round(train_split + 0.15 * len(heart_disease_shuffled))

X_train,y_train = X[:train_split], y[:train_split]
X_valid,y_valid = X[train_split:valid_split], y[train_split:valid_split]

X_test,y_test = X[valid_split:], y[:valid_split]

clf = RandomForestClassifier()
clf.fit(X_train,y_train)


# Make Baseline preds

y_preds = clf.predict(X_valid)

# Evaluate classifier on validation set

baseline_metrics = evaluate_preds(y_valid, y_preds)
baseline_metrics

How can I resolve it?

我怎么才能解决它呢?


Tried changing the parameters and a bunch of other things but all of them popped up some errors like the one listed above

尝试更改参数和一系列其他内容,但所有这些都弹出了一些类似上面列出的错误


更多回答

There should have been a full traceback message showing the line where this code failed. If so, post it in the question as another code block.

应该有一条完整的回溯消息,显示此代码失败的行。如果是,则将其作为另一个代码块发布在问题中。

Try print(type(x), x) for each of your print lines.

尝试为您的每行打印行打印(type(X),x)。

I haven't spotted exactly where this happens in your code, but basically you are trying to format a function object with a format specifier. Suppose i def foo(): pass. And then I try f"my data {foo:.2f}". I'm trying to format function "foo" with a float specifier. And that doesn't make sense. As @jwal suggested, find the failing line then print the types of each of the formated parameters.

我还没有发现代码中发生这种情况的确切位置,但基本上您正在尝试使用格式说明符来格式化函数对象。假设我定义foo():通过。然后我尝试f“我的数据{foo:.2f}”。我正在尝试使用浮点型说明符来格式化函数“foo”。这说不通。正如@jwal建议的那样,找到失败的行,然后打印每个格式化参数的类型。

Where exactly? Please post the full error trace - see how to create a minimal reproducible example

具体在哪里?请发布完整的错误跟踪-请参阅How to Create a Minimum Replexable Example

优秀答案推荐

You are trying to print functions instead of the values they return. For instance, you assign

您正在尝试打印函数,而不是它们返回的值。例如,您可以将


accuracy = accuracy_score(y_true,y_preds)

but then try to format the function, not the result

但然后尝试格式化函数,而不是结果


f"Accuracy :{ accuracy_score*100:.2f}%"

Instead, you should use the calculated value

相反,您应该使用计算出的值


f"Accuracy :{ accuracy*100:.2f}%"

But you've also created a dictionary with rounded values. You could use that dictionary with an f-string or use the .format method instead

但您还创建了一个四舍五入值的词典。您可以将该词典与f字符串一起使用,也可以改用.Format方法


f"Accuracy :{ metric_dict['accuracy']:.2f}"
# -- or --
"Accuracy : {accuracy:.2f}".format(**metric_dict)


Here's the updated/ working version,

以下是更新/工作版本,


import pandas as pd

def evaluate_preds(y_true, y_preds):
"""
Performs Evaluation comp on y_true labels and y_preds labels on a classification
"""
accuracy = accuracy_score(y_true,y_preds)
precision = precision_score(y_true,y_preds)
recall = recall_score(y_true,y_preds)
f1 = f1_score(y_true,y_preds)
metric_dict = {"accuracy": round(accuracy,2),
"precision": round(precision,2),
"recall": round(recall,2),
"f1": round(f1,2)}

print(f"Accuracy :{ accuracy *100:.2f}%")
print(f"Precision :{ precision:.2f}")
print(f"Recall :{ recall:.2f}")
print(f"F1 :{ f1:.2f}")
return metric_dict


from sklearn.ensemble import RandomForestClassifier

np.random.seed(42)

heart_disease_shuffled= heart_disease.sample(frac=1)

X= heart_disease_shuffled.drop("target",axis =1)
y = heart_disease_shuffled["target"]

train_split = round(0.7 * len(heart_disease_shuffled))
valid_split = round(train_split + 0.15 * len(heart_disease_shuffled))

X_train,y_train = X[:train_split], y[:train_split]
X_valid,y_valid = X[train_split:valid_split], y[train_split:valid_split]

X_test,y_test = X[valid_split:], y[:valid_split]

clf = RandomForestClassifier()
clf.fit(X_train,y_train)

# Make Baseline preds
y_preds = clf.predict(X_valid)
# Evaluate classifier on validation set
baseline_metrics = evaluate_preds(y_valid, y_preds)
baseline_metrics


更多回答

Thanks @tdelaney for your kind feedback Changed it to the actual accuracy , precision and got my answer Btw the reason i was using accuracy_score was that the instructor used the same things and got the result but changed these parameters after sometime and I skipped through it & I was just copying him after I got error in my own code cuz I've just started learning Machine Learning Really sorry for all the inconvenience

谢谢@tdelaney的好意反馈,将它更改为实际的准确度和精确度,并得到了我的答案顺便说一句,我使用Accuracy_Score的原因是教练使用了相同的东西并得到了结果,但过了一段时间后更改了这些参数,我跳过了它&我只是在我自己的代码中遇到错误后复制他的,因为我刚刚开始学习机器学习,真的很抱歉给我带来了不便

As posted, this does not work. Indentation matters. Please clarify what you've changed

如发布的,这不起作用。压痕很重要。请澄清你改变了什么

The indentation in the 1st code is correct I've just changed the thing accuracy_score, precision_score, recall_score, f1_score to the actual accuracy, precision, as posted by someone in the print statements in between the {} brackets

第一个代码中的缩进是正确的,我刚刚将Accuracy_Score、Precision_Score、Recall_Score、F1_Score更改为实际的精度、精度,就像某人在{}括号之间的print语句中发布的那样

I've fixed the indentation for you. Please edit if not correct.

我已经为你修好了凹痕。如果不正确,请编辑。

Thanks mate, I couldn't correct the indentation cuz when I copied and pasted the same as posted above I got the result in my Jupyter Notebook, So I thought the indentation is correct. Really appreciate your helping hand and cooperation :)

谢谢你,我不能纠正缩进,因为当我复制和粘贴的时候,我在我的Jupyter笔记本上得到了上面的结果,所以我认为缩进是正确的。非常感谢您的帮助和合作:)

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com