Ich habe einen Datensatz mit den folgenden Spalten und ZeilenPandas Dataframe Spalte beschädigt, während eine Additions Spalte Berechnung
Scored Probabilities for Class "1" Scored Probabilities for Class "2" Scored Probabilities for Class "3" Scored Labels
0.258471 0.009299 0.005433 1
0.154108 0.009577 0.527308 3
0.001949 0.634572 0.000953 2
(Tatsächlich gibt es 17 „Klassen“, aber ich habe für diesen Beitrag zu 3 vereinfacht)
Ich würde gerne eine zusätzliche Spalte namens "Scored Label Probability" hinzufügen, die das Maximum der ersten drei Spalten ist (eigentlich das Maximum aller Spalten, die "Scored Wahrscheinlichkeiten für Klasse" X "" genannt werden). So sollte das Ergebnis so aussehen: -
Scored Label Probability (new)
0.258471 0.009299 0.005433 1 0.258471
0.154108 0.009577 0.527308 3 0.527308
0.001949 0.634572 0.000953 2 0.634572
Hier ist mein Code (unten). Leider wird die "Scored Labels" -Spalte (die 4. Spalte in den Beispieldaten) beschädigt (durch eine andere ganze Zahl ersetzt). Irgendwelche Vorschläge, wie man es beheben kann? Dank
# The script MUST contain a function named azureml_main
# which is the entry point for this module.
import pandas as pd
import numpy as np
# The entry point function can contain up to two input arguments:
# Param<dataframe1>: a pandas.DataFrame
# Param<dataframe2>: a pandas.DataFrame
def azureml_main(df = None, df2 = None):
# First add the empty column
df['Scored Label Probability'] = 0.0
for rowindex, row in df.iterrows():
max_probability =0.0
column_value = 0.0
column_name = ''
for column_name, column_value in row.iteritems():
if column_name.startswith('Scored Probabilities for Class'):
if column_value>max_probability:
max_probability = column_value
# print (max_probability,max_prob_column_name)
df.set_value(rowindex,'Scored Label Probability',max_probability)
# Return value must be of a sequence of pandas.DataFrame
return df
Schön! Ich werde es versuchen. – andyabel
Eigentlich muss ich vielleicht in R neu schreiben. Irgendeine Idee, wie Sie das gleiche in R schreiben? – andyabel