简体   繁体   中英

How to code all labels in list of pandas dataframes?

Parse the list of pandas dataframes from API. I need them to put int autoencoder which fits data with shape (100, 36, 18)

#encoder
input_sig = Input(shape=(num_events, features))
conv1 = Conv1D(32, 3, activation='relu', padding='same')(input_sig)
pool1 = MaxPooling1D(pool_size=2)(conv1) 
conv2 = Conv1D(64, 3, activation='relu', padding='same')(pool1)
pool2 = MaxPooling1D(pool_size=2)(conv2) 
conv3 = Conv1D(128, 3, activation='relu', padding='same')(pool2)
#decoder
conv4 = Conv1D(128, 3, activation='relu', padding='same')(conv3)
up1 = UpSampling1D(2)(conv4) 
conv5 = Conv1D(64, 3, activation='relu', padding='same')(up1)
up2 = UpSampling1D(2)(conv5) 
decoded = Conv1D(features, 3, activation='relu', padding='same')(up2)
model= Model(input_sig, decoded)
model.compile(loss='mean_squared_error', optimizer = RMSprop())
model.summary()
X_train, X_test, y_train, y_test = train_test_split(df_2,
df_2,
test_size=0.2,
random_state=50)

So I need to code categorical parameters in all my dataframes. But it's encoded in different values. It's strongly wrong..: For example :

lst1 = {'Name': ['Java', 'Python', 'C', 'C++',
'JavaScript']}
lst2 = {'Name': ['Scala', 'Python', 'C', 'C++',
'JavaScript', 'Node', 'Text']}
dframe1 = pd.DataFrame(lst1)
dframe2 = pd.DataFrame(lst2)
dframe1['Name'] = LabelEncoder().fit_transform(dframe1['Name'])
dframe2['Name'] = LabelEncoder().fit_transform(dframe2['Name'])
asd = [dframe1,dframe2]

I need some function, that encodes the value 'Python' in both dataframes into the same value. How can I do this?

Are the names in lst1 and lst2 fixed? Will there be more names in the future?

If not, what you have to do is get all the unique names/categories, and fit the LabelEncoder . For example,

names = dframe1['Name'].values.tolist() + dframe2['Name'].values.tolist()
enc = LabelEncoder()
enc.fit(names)

dframe1['Name'] = enc.transform(dframe1['Name'])
dframe2['Name'] = enc.transform(dframe2['Name'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM