As a relative new-comer to Python I am trying to use the sklearn RandomForestClassifier. One example from a how-to guide by yhat is the following:
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import numpy as np
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['is_train'] = np.random.uniform(0, 1, len(df)) <= .75
df['species'] = pd.Factor(iris.target, iris.target_names)
df.head()
train, test = df[df['is_train']==True], df[df['is_train']==False]
features = df.columns[:4]
clf = RandomForestClassifier(n_jobs=2)
y, _ = pd.factorize(train['species']) # assignment I don't understand
clf.fit(train[features], y)
preds = iris.target_names[clf.predict(test[features])]
pd.crosstab(test['species'], preds, rownames=['actual'], colnames=['preds'])
Can some explain what the y, _ assignment does and how it works. It isn't used explicitly, but I get an error if I leave it out.
You decompose the returned tuple into two distinct values, y
and _
.
_
is convention for "I don't need that value anymore".
It's basically the same as:
y = pd.factorize(train['species'])[0]
with the exception that this code would work for any indexable return value with at least 1 element, while yours explicitly needs exactly two items in the returned value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With