gpt4 book ai didi

python - 如果作为参数传递,机器学习模型会改变吗?

转载 作者:行者123 更新时间:2023-11-30 09:27:14 32 4
gpt4 key购买 nike

我正在用 python 创建机器学习算法。假设我有:

alg = DecisionTreeClassifier(random_state=1)

然后我创建一个适合的函数

def fit(X,Y,alg):
alg.fit(X,Y)

在我的主要部分我打电话

fit(X, Y, alg)
Y_pred = alg.predict(X_test)

这行得通吗?或者 alg 在拟合之外从未真正改变过?

我已阅读此问题How do I pass a variable by reference?但这很令人困惑,因为我不知道我的变量 alg 是如何更改的。

感谢您的帮助。

最佳答案

是的,您传递给该函数的模型对象是可变的,并且可以通过 fit 方法进行更改。 Python的求值策略是call-by-sharing ,因此更改将反射(reflect)在调用者中。

Call by sharing (also referred to as call by object or call by object-sharing) is an evaluation strategy first named by Barbara Liskov et al. for the language CLU in 1974.[5] It is used by languages such as Python,[6] Iota,[7] Java (for object references), Ruby, JavaScript, Scheme, OCaml, AppleScript, and many others. However, the term "call by sharing" is not in common use; the terminology is inconsistent across different sources. For example, in the Java community, they say that Java is call by value. Call by sharing implies that values in the language are based on objects rather than primitive types, i.e. that all values are "boxed".

The semantics of call by sharing differ from call by reference in that assignments to function arguments within the function aren't visible to the caller (unlike by reference semantics)[citation needed], so e.g. if a variable was passed, it is not possible to simulate an assignment on that variable in the caller's scope. However, since the function has access to the same object as the caller (no copy is made), mutations to those objects, if the objects are mutable, within the function are visible to the caller, which may appear to differ from call by value semantics. Mutations of a mutable object within the function are visible to the caller because the object is not copied or cloned — it is shared.

关于python - 如果作为参数传递,机器学习模型会改变吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41229672/

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