hi
- User
hi
- Assistant
Hello! How can I assist you today?
- User
categorical_columns = ['Product Code']
categorical_columns = ['Sales/Purchase/Production Order ID', 'Origin Code', 'Destination Code', 'Product Code']
for target in categorical_columns: random_rows = df.sample(n=1) random_rows_2 = random_rows.copy() missing_indexes = random_rows.index df.loc[missing_indexes, target] = np.nan
df["Sales/Purchase/Production Order ID"].astype("category") df["Product Code"].astype("category") df["Origin Code"].astype("category") df["Destination Code"].astype("category") nan_records_df = df.loc[missing_indexes] no_nan_records_df = df.drop(missing_indexes) prediction_columns = categorical_columns + numeric_columns target_column = target clf = xgb.XGBClassifier(enable_categorical=True) clf.fit(no_nan_records_df[prediction_columns], no_nan_records_df[target]) prediction_features = nan_records_df[prediction_columns] nan_records_df[target_column + "_predicted"] = xgb_classifier.predict(prediction_features) nan_records_df[target] = random_rows_2[target] actual_values = nan_records_df[target].values predicted_values = nan_records_df[target + '_predicted'].values actual_values = actual_values.astype(str) predicted_values = predicted_values.astype(str) nan_records_df['Accuracy'] = accuracy_score(actual_values, predicted_values) nan_records_df['Precision'] = precision_score(actual_values, predicted_values, average='weighted') nan_records_df['Recall'] = recall_score(actual_values, predicted_values, average='weighted') nan_records_df['F1'] = f1_score(actual_values, predicted_values, average='weighted') no_nan_records_df = no_nan_records_df.append(nan_records_df) no_nan_records_df = no_nan_records_df.reset_index(drop=True) filled = filled.append(nan_records_df) filled.to_excel('{}.xlsx'.format(target)) print('Done!')filled 22 clf = xgb.XGBClassifier(enable_categorical=True) ---> 23 clf.fit(no_nan_records_df[prediction_columns], no_nan_records_df[target]) 25 prediction_features = nan_records_df[prediction_columns] 26 nan_records_df[target_column + "_predicted"] = xgb_classifier.predict(prediction_features)
File c:\Users\CR7\AppData\Local\Programs\Python\Python310\lib\site-packages\xgboost\core.py:620, in require_keyword_args..throw_if..inner_f(*args, **kwargs) 618 for k, arg in zip(sig.parameters, args): 619 kwargs[k] = arg --> 620 return func(**kwargs)
File c:\Users\CR7\AppData\Local\Programs\Python\Python310\lib\site-packages\xgboost\sklearn.py:1459, in XGBClassifier.fit(self, X, y, sample_weight, base_margin, eval_set, eval_metric, early_stopping_rounds, verbose, xgb_model, sample_weight_eval_set, base_margin_eval_set, feature_weights, callbacks) 1457 expected_classes = cp.arange(self.n_classes_) 1458 else: -> 1459 self.classes_ = np.unique(np.asarray(y)) 1460 self.n_classes_ = len(self.classes_) 1461 expected_classes = np.arange(self.n_classes_)
File <array_function internals>:200, in unique(*args, **kwargs)
File c:\Users\CR7\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\lib\arraysetops.py:274, in unique(ar, return_index, return_inverse, return_counts, axis, equal_nan) 272 ar = np.asanyarray(ar) ... --> 336 ar.sort() 337 aux = ar 338 mask = np.empty(aux.shape, dtype=np.bool_)
- Assistant
It seems that there is an error in your code. The error occurs in line 23 where you are trying to fit the XGBClassifier model. The error message indicates that the target variable
ycontains non-numeric values, which is not supported by the XGBoost library.To fix this issue, you can encode your categorical target variable into numeric values before fitting the model. One way to do this is by using scikit-learn's LabelEncoder:
from sklearn.preprocessing import LabelEncoder le = LabelEncoder() encoded_target = le.fit_transform(no_nan_records_df[target]) clf.fit(no_nan_records_df[prediction_columns], encoded_target)Make sure to update the prediction and evaluation parts of your code accordingly to use the encoded target variable.
Additionally, please ensure that you have imported the necessary libraries (
xgb,np,accuracy_score,precision_score,recall_score,f1_score) before running the code.Let me know if you need any further assistance!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- gpt-3.5-turbo-0301Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-4-0314Compare side by side →