Fix formatting and typos.

communication
junos 2021-08-06 18:53:18 +02:00
parent cca5a29483
commit 02f2607be9
1 changed files with 27 additions and 29 deletions

View File

@ -151,10 +151,8 @@ def count_comms(comm_df: pd.DataFrame) -> pd.DataFrame:
comm_features = comm_counts.join(comm_duration_total) comm_features = comm_counts.join(comm_duration_total)
comm_features = comm_features.join(comm_duration_max) comm_features = comm_features.join(comm_duration_max)
try: try:
comm_features.drop(columns="duration_total_" + comm_features.drop(columns="duration_total_" + call_types[3], inplace=True)
call_types[3], inplace=True) comm_features.drop(columns="duration_max_" + call_types[3], inplace=True)
comm_features.drop(columns="duration_max_" +
call_types[3], inplace=True)
# The missed calls are always of 0 duration. # The missed calls are always of 0 duration.
except KeyError: except KeyError:
pass pass
@ -175,8 +173,7 @@ def count_comms(comm_df: pd.DataFrame) -> pd.DataFrame:
) )
# Ratio of incoming and outgoing messages to all messages. # Ratio of incoming and outgoing messages to all messages.
else: else:
raise KeyError( raise KeyError("The dataframe contains neither call_type or message_type")
"The dataframe contains neither call_type or message_type")
return comm_features return comm_features
@ -203,11 +200,10 @@ def contact_features(df_enumerated: pd.DataFrame) -> pd.DataFrame:
if "call_duration" in df_enumerated: if "call_duration" in df_enumerated:
# Add a column with the total duration of calls between two people # Add a column with the total duration of calls between two people
duration_count = ( duration_count = (
df_enumerated.groupby( df_enumerated.groupby(["participant_id", "contact_id"])
["participant_id", "contact_id"]
)
# For each participant and for each caller, sum durations of their calls # For each participant and for each caller, sum durations of their calls
["call_duration"].sum() ["call_duration"]
.sum()
.reset_index() # Make index (which is actually the participant id) a normal column .reset_index() # Make index (which is actually the participant id) a normal column
.rename(columns={"call_duration": "total_call_duration"}) .rename(columns={"call_duration": "total_call_duration"})
) )
@ -215,13 +211,14 @@ def contact_features(df_enumerated: pd.DataFrame) -> pd.DataFrame:
# participants, callers and the total duration of their calls. All that # participants, callers and the total duration of their calls. All that
# is now left to do is to merge the original df with the new one. # is now left to do is to merge the original df with the new one.
df_enumerated = df_enumerated.merge( df_enumerated = df_enumerated.merge(
duration_count, duration_count, on=["participant_id", "contact_id"]
on=["participant_id", "contact_id"] )
)
contact_count = ( contact_count = (
df_enumerated.groupby(["participant_id"]) df_enumerated.groupby(["participant_id"])
.nunique()["contact_id"] # For each participant, count the number of distinct contacts .nunique()[
"contact_id"
] # For each participant, count the number of distinct contacts
.reset_index() # Make index (which is actually the participant id) a normal column .reset_index() # Make index (which is actually the participant id) a normal column
.rename(columns={"contact_id": "no_contacts"}) .rename(columns={"contact_id": "no_contacts"})
) )
@ -258,7 +255,7 @@ def calls_sms_features(df_calls: pd.DataFrame, df_sms: pd.DataFrame) -> pd.DataF
* proportion_calls: * proportion_calls:
proportion of calls in total number of communications proportion of calls in total number of communications
* proportion_calls_incoming: * proportion_calls_incoming:
proportion of incoming calls in total number of incoming/recieved communications proportion of incoming calls in total number of incoming/received communications
* proportion_calls_outgoing: * proportion_calls_outgoing:
proportion of outgoing calls in total number of outgoing/sent communications proportion of outgoing calls in total number of outgoing/sent communications
* proportion_calls_missed_sms_received: * proportion_calls_missed_sms_received:
@ -290,12 +287,14 @@ def calls_sms_features(df_calls: pd.DataFrame, df_sms: pd.DataFrame) -> pd.DataF
) )
# Calculate new features and create additional columns # Calculate new features and create additional columns
)[ )[
["participant_id", [
"proportion_calls", "participant_id",
"proportion_calls_incoming", "proportion_calls",
"proportion_calls_outgoing", "proportion_calls_incoming",
"proportion_calls_missed_sms_received"] "proportion_calls_outgoing",
] # Filter out only the relevant feautres "proportion_calls_missed_sms_received",
]
] # Filter out only the relevant features
) )
features_calls = contact_features(enumerate_contacts(df_calls)) features_calls = contact_features(enumerate_contacts(df_calls))
@ -305,22 +304,21 @@ def calls_sms_features(df_calls: pd.DataFrame, df_sms: pd.DataFrame) -> pd.DataF
features_calls.merge( features_calls.merge(
features_sms, on="participant_id", suffixes=("_calls", "_sms") features_sms, on="participant_id", suffixes=("_calls", "_sms")
) # Merge calls and sms features ) # Merge calls and sms features
.reset_index() # Make participand_id a regular column .reset_index() # Make participant_id a regular column
.assign( .assign(
proportion_calls_contacts=( proportion_calls_contacts=(
lambda x: x.no_contacts_calls / lambda x: x.no_contacts_calls
(x.no_contacts_calls + x.no_contacts_sms) / (x.no_contacts_calls + x.no_contacts_sms)
) # Calculate new features and create additional columns ) # Calculate new features and create additional columns
)[ )[
["participant_id", ["participant_id", "proportion_calls_contacts"]
"proportion_calls_contacts"] ] # Filter out only the relevant features
] # Filter out only the relevant feautres
# Since we are interested only in some features and ignored # Since we are interested only in some features and ignored
# others, a lot of duplicate rows were created. Remove them. # others, a lot of duplicate rows were created. Remove them.
.drop_duplicates() .drop_duplicates()
) )
# Join the newely created dataframes # Join the newly created dataframes
df_calls_sms = count_joined.merge(features_joined, on="participant_id") df_calls_sms = count_joined.merge(features_joined, on="participant_id")
return df_calls_sms return df_calls_sms