How to use mutli-column functions in Patsy which return categorical output?
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 25/100
Rechercherichtung
Beginnen Sie mit dem im Issue gezeigten Aufruf von patsy.dmatrix und der benutzerdefinierten multival-Funktion und sehen Sie sich anschließend das verlinkte Duplikat in statsmodels/statsmodels#2843 an. Die Arbeit ist abgeschlossen, wenn die benutzerdefinierte kategoriale Ausgabe mit mehreren Spalten aussagekräftige, auf Ebenen basierende Spaltennamen anstelle numerischer Indizes bereitstellt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
I am dealing with a situation where each item can have 1 or 2 labels both of which come from the same set. In this case I only want to have 1 set of categorical variables which encode if the values of each level are present or absent with respect to the reference. I also want to inversely weight the values of these categorical variables based on how many labels are present.
E.g. Let my categories be {A, B, C}
data = pd.DataFrame({"col1": ["A", "A", "B"], "col2": ["O", "B", "C"], "num_vals": [1,2,2]})
def inverse_val(x):
return 1.0/x
# Using categorical coding will not give the correct output
X = patsy.dmatrix("(C(col1) + C(col2)):inverse_val(num_vals)", data, return_type="dataframe")
I tried to solve this issue using the following code:
def multival(*x, **kwargs):
#raise Exception("Not Implemented")
levels, reference = kwargs.get("levels", None), kwargs.get("reference", None)
weights = kwargs.get("weights", None)
if len(x[0].shape) != 1:
raise Exception("Mismatching Shapes. All arrays should be 1d and should have the same shape")
for k in x:
if k.shape != x[0].shape:
raise Exception("Mismatching Shapes. All arrays should be 1d and should have the same shape")
if levels is None:
levels = np.sort(np.unique(np.hstack(x))) # Sort the unique values and then use this ordering as levels
if reference is None:
reference = levels[0]
#print "Levels: %s, reference: %s" % (levels, reference)
levels = levels[levels != reference] # Remove reference from levels
level_len = len(levels)
#print x[0].shape[0], level_len
out = np.zeros((x[0].shape[0], level_len))
for i, v in enumerate(levels):
# print i, v
for col in x:
out[np.where(np.array(col) == v), i] = 1
#print "Created matrix with shape: ", out.shape
colnames = ["T.%s" % k for k in levels]
if weights is not None:
weights = weights.values
return pd.DataFrame(out, columns=colnames).divide(weights, axis=0)
return pd.DataFrame(out, columns=colnames)
X = patsy.dmatrix("multival(col1, col2, weights=num_vals)", data, return_type="dataframe")
There is no way of identifying the column names generated by patsy:
E.g. The names are like this:
multival(col1, col2, weights=num_vals, reference='O')[0]
multival(col1, col2, weights=num_vals, reference='O')[1]
multival(col1, col2, weights=num_vals, reference='O')[2]
I would much rather prefer column names like the following:
multival(col1, col2, weights=num_vals, reference='O')[T.0]
multival(col1, col2, weights=num_vals, reference='O')[T.1]
multival(col1, col2, weights=num_vals, reference='O')[T.2]
OR if I am passing the levels variable:
multival(col1, col2, weights=num_vals, reference='O')[T.A]
multival(col1, col2, weights=num_vals, reference='O')[T.B]
multival(col1, col2, weights=num_vals, reference='O')[T.C]
Is there a way to achieve this in patsy ?
Duplicate of statsmodels/statsmodels#2843
- Vorherrschende Sprache
- Python
- Sterne
- 989
- Forks
- 108
- Ø Merge
- 19 Std. 32 Min.
- Gemergte PRs (30 T.)
- 4
Entwicklungsumgebung
Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus pydata/patsy
-
VarLookupDict should implement __iter__, causes CPython crash when using statsmodels in marimo notebookEvtl. vergeben @kouyouqi123 hat das vor 151 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 72/100
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 68/100
-
cc() truncates periodic coordinates for integer input with fractional boundsEvtl. vergeben @ktz03 hat das vor 16 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 35/100
-
`build._eval_factor` error condition attempts to access attribute `dtype` on `DataFrame` object, but no such attribute existsEvtl. vergeben @kouyouqi123 hat das vor 151 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 55/100
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
btclib-org/btclib-node#2132 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
freelawproject/juriscraper#2367 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
infra tech-debt
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
docs(k8s-api-proxy): name the service-account-token Secret path in the read-write mode commentOffenneeds-triage
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 78/100
gke-labs/kube-agents#4190 ·
Maintainer antworten meist innerhalb von 1 Tag