Untitled
unknown
plain_text
2 years ago
8.3 kB
4
Indexable
Code- import pandas as pd import numpy as np from feature_engineering import feature_engineering from model_training_building import get_top_5_person_who_resolved, label_enc # Function to make real-time recommendations def make_recommendation(input_data, train_data, label_enc): # Create a copy of the input data to avoid modifying the original DataFrame input_data=input_data print(input_data.head()) input_data_copy = input_data.copy() # Drop the 'person_who_resolved' column from the input data input_data_copy.drop(columns=['person_who_resolved'], inplace=True) # Apply the function to the modified input data to get the recommendations input_data_copy['recommendations'], _ = zip(*input_data_copy.apply(lambda row: get_top_5_person_who_resolved(train_data, row), axis=1)) # Remove duplicate values from recommendations input_data_copy['recommendations'] = input_data_copy['recommendations'].apply(lambda x: list(set(x))) # Return the recommendations as a list recommendations = input_data_copy['recommendations'].tolist() return recommendations if __name__ == "__main__": sample_ticket_data = { 'ticket_category': [0], 'ticket_type': [14], 'ticket_item': [35], 'ticket_summary': [1], 'ticket_severity': [2], 'resolution_sla_violated': [0], 'reopen_count': [0], 'owner_user_id': [670], 'role_name_encoded': [1], 'ticket_resolution_time': [133], 'role_name_decoded': ['L2 Support'], 'person_who_resolved' : ['Shraddha Sonawane'] } # Drop the 'person_who_resolved' column from the sample_ticket_df sample_ticket_df_copy = sample_ticket_df.drop(columns=['person_who_resolved']) # Call the make_recommendation function to get the recommendations for the sample ticket recommendations = make_recommendation(sample_ticket_df_copy, train_data, label_enc) print("Recommended users for the sample ticket:") for i, rec in enumerate(recommendations[0]): print(f"Recommendation {i+1}: User {rec[0]}, Owner User ID {rec[1]}, Role Name {rec[2]}") Error- --------------------------------------------------------------------------- KeyError Traceback (most recent call last) File /Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/pandas/core/indexes/base.py:3361, in Index.get_loc(self, key, method, tolerance) 3360 try: -> 3361 return self._engine.get_loc(casted_key) 3362 except KeyError as err: File /Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/pandas/_libs/index.pyx:76, in pandas._libs.index.IndexEngine.get_loc() File /Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/pandas/_libs/index.pyx:108, in pandas._libs.index.IndexEngine.get_loc() File pandas/_libs/hashtable_class_helper.pxi:5198, in pandas._libs.hashtable.PyObjectHashTable.get_item() File pandas/_libs/hashtable_class_helper.pxi:5206, in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'person_who_resolved' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Input In [64], in <cell line: 31>() 50 sample_ticket_df_copy = sample_ticket_df.drop(columns=['person_who_resolved']) 52 # Call the make_recommendation function to get the recommendations for the sample ticket ---> 53 recommendations = make_recommendation(sample_ticket_df_copy, train_data, label_enc) 55 print("Recommended users for the sample ticket:") 56 for i, rec in enumerate(recommendations[0]): Input In [64], in make_recommendation(input_data, train_data, label_enc) 14 input_data_copy = input_data.copy() 16 # Drop the 'person_who_resolved' column from the input data 17 #input_data_copy.drop(columns=['person_who_resolved'], inplace=True) 18 19 # Apply the function to the modified input data to get the recommendations ---> 20 input_data_copy['recommendations'], _ = zip(*input_data_copy.apply(lambda row: get_top_5_person_who_resolved(train_data, row), axis=1)) 22 # Remove duplicate values from recommendations 23 input_data_copy['recommendations'] = input_data_copy['recommendations'].apply(lambda x: list(set(x))) File /Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/pandas/core/frame.py:8736, in DataFrame.apply(self, func, axis, raw, result_type, args, **kwargs) 8725 from pandas.core.apply import frame_apply 8727 op = frame_apply( 8728 self, 8729 func=func, (...) 8734 kwargs=kwargs, 8735 ) -> 8736 return op.apply() File /Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/pandas/core/apply.py:688, in FrameApply.apply(self) 685 elif self.raw: 686 return self.apply_raw() --> 688 return self.apply_standard() File /Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/pandas/core/apply.py:805, in FrameApply.apply_standard(self) 804 def apply_standard(self): --> 805 results, res_index = self.apply_series_generator() 807 # wrap results 808 return self.wrap_results(results, res_index) File /Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/pandas/core/apply.py:821, in FrameApply.apply_series_generator(self) 818 with option_context("mode.chained_assignment", None): 819 for i, v in enumerate(series_gen): 820 # ignore SettingWithCopy here in case the user mutates --> 821 results[i] = self.f(v) 822 if isinstance(results[i], ABCSeries): 823 # If we have a view on v, we need to make a copy because 824 # series_generator will swap out the underlying data 825 results[i] = results[i].copy(deep=False) Input In [64], in make_recommendation.<locals>.<lambda>(row) 14 input_data_copy = input_data.copy() 16 # Drop the 'person_who_resolved' column from the input data 17 #input_data_copy.drop(columns=['person_who_resolved'], inplace=True) 18 19 # Apply the function to the modified input data to get the recommendations ---> 20 input_data_copy['recommendations'], _ = zip(*input_data_copy.apply(lambda row: get_top_5_person_who_resolved(train_data, row), axis=1)) 22 # Remove duplicate values from recommendations 23 input_data_copy['recommendations'] = input_data_copy['recommendations'].apply(lambda x: list(set(x))) File /Analytics/venv/Jup/CAPE_ServicePlus_UC/model_training_building.py:50, in get_top_5_person_who_resolved(df, row, distance_metric) 47 closest_role_name_decoded = df.iloc[closest_indices]['role_name_decoded'] 49 # Get the actual person_who_resolved, owner_user_id, and role_name value for the input ticket ---> 50 actual_person_who_resolved = row['person_who_resolved'] 51 actual_owner_user_id = row['owner_user_id'] 52 actual_role_name_encoded = row['role_name_encoded'] File /Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/pandas/core/series.py:942, in Series.__getitem__(self, key) 939 return self._values[key] 941 elif key_is_scalar: --> 942 return self._get_value(key) 944 if is_hashable(key): 945 # Otherwise index.get_value will raise InvalidIndexError 946 try: 947 # For labels that don't resolve as scalars like tuples and frozensets File /Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/pandas/core/series.py:1051, in Series._get_value(self, label, takeable) 1048 return self._values[label] 1050 # Similar to Index.get_value, but we do not fall back to positional -> 1051 loc = self.index.get_loc(label) 1052 return self.index._get_values_for_loc(self, loc, label) File /Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/pandas/core/indexes/base.py:3363, in Index.get_loc(self, key, method, tolerance) 3361 return self._engine.get_loc(casted_key) 3362 except KeyError as err: -> 3363 raise KeyError(key) from err 3365 if is_scalar(key) and isna(key) and not self.hasnans: 3366 raise KeyError(key) KeyError: 'person_who_resolved'
Editor is loading...