# How to train a regressor model on data that has duplicate subjects but different records for each?

I am working on a dataset that is as follows (just an example):

prop_subj prop_comp bed_subj bath_comp sqft_subj sqft_comp
A B 2 1 1002 1006
A C 2 2 1002 1075
A D 2 2 1002 1000
B G 2 1 1002 978
B F 2 2 1002 1200
B D 2 1 1002 960

Let's just say a prop_subj can have multiple prop_comp. For training a regressor model using such dataset, I was wondering whether I can treat As, Bs, and so on as a group by assigning a random number to each and passing it to the model such that the model can determine the group as just one property.

Any help is highly appreciated. Thanks!

• Hi and welcome to the community:) A couple of quick questions in regards to the OP. 1. There seem to be a repeating column name prop_subj? 2. Do you want to treat ear row as a single property or you plan to do something else? 3. What's the target variable here 4. What is the grouping of A's and B's. Is this grouping meant along a row or along. A column Commented Sep 8, 2022 at 7:23
– GooJ
Commented Sep 8, 2022 at 19:41
• @Polymath thanks! and I should have been more precise with the example. 1) Sorry for the typo - I updated the table. 2) But what I am trying to achieve is a way to train the model such that the A's, B's, and so on would be treated by the model as a group instead of an individual row because that's associated with a single subject. 3) The target (not shown in that table) is price_diff as I am trying to predict: the difference in price. 4) grouping is along the column. So each property has a comp - multiple prop can have same comp. Think of A's B's as address (these won't go into training). Commented Sep 9, 2022 at 21:01

Assigning random number might not be ideal as number have a natural order. For example, if B is 1, C is 100, D is 10. The model will treat B<D<C, which shouldn't be the case.

One-Hot Encoder would be a better solution. It converts the categorical column to columns of 1 and 0, which will be independent when fitting into the regression model. If you are using pandas, you can use pd.get_dummies to achieve that.

df = pd.DataFrame([['A', 'B', 2, 1, 1002, 1006], ['A', 'C', 2, 2, 1002, 1075], ['A', 'D', 2, 2, 1002, 1000]], columns=['prop_subj', 'prop_comp', 'bed_subj', 'bath_comp', 'sqft_subj', 'sqft_comp'])
df

prop_subj prop_comp  bed_subj  bath_comp  sqft_subj  sqft_comp
0         A         B         2          1       1002       1006
1         A         C         2          2       1002       1075
2         A         D         2          2       1002       1000

df = pd.concat([df,pd.get_dummies(df['prop_comp'], prefix='prop_comp')],axis=1)
df = df.drop(['prop_comp'],axis=1)
df

prop_subj   bed_subj    bath_comp   sqft_subj   sqft_comp   prop_comp_B prop_comp_C prop_comp_D
0          A           2           1         1002        1006             1           0           0
1          A           2           2         1002        1075             0           1           0
2          A           2           2         1002        1000             0           0           1


If you are using sklearn, you can refer to the documentation.