basic Python question

MRAB python at mrabarnett.plus.com
Fri May 8 16:50:14 EDT 2020


On 2020-05-08 21:19, joseph pareti wrote:
> yet, something is still unclear; in Python you can do things like:
>
> *clf0.fit(X_train,y_train)*
>
> which is not the way I programmed in other languages where a left-hand 
> side and a right hand side is required.
>
All it's doing is performing the calculation and then storing the result 
in the object itself. Most other languages can do that too.

> Am Fr., 8. Mai 2020 um 21:52 Uhr schrieb joseph pareti 
> <joepareti54 at gmail.com <mailto:joepareti54 at gmail.com>>:
>
>     yes, it is random forest classifier from scikit learn. Thank you.
>
>     Am Fr., 8. Mai 2020 um 21:50 Uhr schrieb MRAB
>     <python at mrabarnett.plus.com <mailto:python at mrabarnett.plus.com>>:
>
>         On 2020-05-08 20:02, joseph pareti wrote:
>         > In general I prefer doing:
>         >
>         >
>         > X_train, X_test, y_train, y_test = train_test_split(X, y,
>         test_size=0.33, random_state=42)
>          >clf = RandomForestClassifier(n_estimators = 100, max_depth=
>         > None) *clf_f = clf.fit(X_train, y_train)* predicted_labels =
>         clf_f.predict(
>         > X_test) score = clf.score(X_test, y_test) score1 =
>         metrics.accuracy_score(
>         > y_test, predicted_labels)
>         >
>         >
>         > rather than:
>         >
>         > X_train, X_test, y_train, y_test = train_test_split(X, y,
>         test_size=0.33,
>         > random_state=42)
>         clf0=RandomForestClassifier(n_estimators=100, max_depth=
>         > None) *clf0.fit(X_train, y_train)* y_pred
>         =clf0.predict(X_test) score=
>         > metrics.accuracy_score(y_test, y_pred)
>         >
>         >
>         > Are the two codes really equivalent?
>         >
>         You didn't give any context and say what package you're using!
>
>         After searching for "RandomForestClassifier", I'm guessing
>         that you're
>         using scikit.
>
>          From the documentation here:
>
>         https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html#sklearn.ensemble.RandomForestClassifier.fit
>
>         it says:
>
>              Returns: self : object
>
>         so it looks like clf.fit(...) returns clf.
>
>         That being the case, then, yes, they're equivalent.
>
>



More information about the Python-list mailing list