AttributeError: 'numpy.ndarray' object has no attribute 'assign'
- User
AttributeError: 'numpy.ndarray' object has no attribute 'assign'
- Assistant
assignis a function that belongs to the Pandas library and is used to assign new columns to a DataFrame. In your case, you are trying to use it with a NumPy array, which does not support theassignmethod.If you want to add a new column to a NumPy array, you can either create a new array with an additional column or use the
numpy.column_stack()ornumpy.hstack()functions. Here's an example:import numpy as np # Creating a sample NumPy array array1 = np.array([[1, 2], [3, 4], [5, 6]]) # Creating a new column (as an array) new_column = np.array([7, 8, 9]) # Using numpy.column_stack() array2 = np.column_stack((array1, new_column)) # Using numpy.hstack() array3 = np.hstack((array1, new_column[:, None])) print("Original array:") print(array1) print("\nArray with column_stack:") print(array2) print("\nArray with hstack:") print(array3) - User
def make_tf_dataset(data, batch_size=None): #data = data.fillna(-999) data.fillna(data.mean(), inplace=True) X = data.drop(['TransactionID','TransactionDT','TransactionAmt','DeviceInfo','DeviceType','ProductCD','card4','card6','P_emaildomain','R_emaildomain','M4', 'isFraud'], axis=1) y = data['isFraud']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) #sm = SMOTE(random_state=42) #X_train, y_train = sm.fit_resample(X_train, y_train) X_train =tf.convert_to_tensor(X_train, dtype=tf.float64) y_train = tf.convert_to_tensor(y_train, dtype=tf.int64) #X_train = X_train.astype('float32') balanced_df=X_train.assign(Class=y_train) y = balanced_df.pop('Class') # Dataset creation dataset = tf.data.Dataset.from_tensor_slices((balanced_df.values, y.to_frame().values)) dataset = dataset.shuffle(2048, seed=SEED) if batch_size: dataset = dataset.batch(batch_size) return datasetEPOCHS = 100 BATCH_SIZE = 128 SEED = 1337 train_data, val_data = [], []
for client_data in [alice_df, bob_df]: train_df, val_df = train_test_split(client_data, test_size=0.2, random_state=SEED)
# TF Datasets train_data.append(make_tf_dataset(train_df, batch_size=BATCH_SIZE)) val_data.append(make_tf_dataset(val_df, batch_size=1)) - Assistant
It looks like you are trying to create TensorFlow datasets from the provided data and then split them into training and validation datasets. Your code seems to be mostly correct, but there is a small issue when creating the
balanced_dfDataFrame from the tensors. Since you are using TensorFlow tensors, you cannot simply use pandas'assignmethod as you did before.Instead, you can leave the tensors as they are and create the TensorFlow dataset using the function. Here's the modified function: