DEV Community

Cover image for Product developers’ guide to cross joins
Mage
Mage

Posted on

Product developers’ guide to cross joins

TLDR

Learn how to use AI to select your next outfit that matches the occasion. Featuring Pandas and the merge function to find all combinations.

Introduction

We’ve all been there staring into our closet, admiring all the clothes but not quite realizing all the different combinations of clothes. In this follow up guide, I’ll go over how we can use AI to make our selection more transparent by understanding all the combinations we can use and when we should wear them. Let’s get started by looking at our wardrobe!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dms0vhx0eam74dm62562.jpg)_What’s in your wardrobe? (Source: [Anna Mu](https://www.inprnt.com/gallery/annamu/))_

Before we begin

First, please make sure you know the basics of sorting and filtering in Pandas, as we’ll be using it frequently. For a quick refresher, read our introductory guide for information on importing, exporting, and processing all our data, and our guide to merge. With that out of the way, let’s start by downloading the wardrobe dataset and importing it on Google Collab.
Image description

My closet

The wardrobe dataset contains information on 5 different shirts, and 5 different pants, along with labels for whether they are casual wear. We’ll break down this dataset into 2 portions, 1 for shirts and another for pants.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qyy0dqlyjpjbb7irlb7q.jpg)_Shirts: [T-Shirt](https://www.google.com/imgres?imgurl=https%3A%2F%2Frfis.freetls.fastly.net%2Fpublic%2Fcontent%2F4845243%3Far%3D3%3A4%26w%3D412&imgrefurl=https%3A%2F%2Fwww.stylepit.com%2Fname-it-neon-yellow-nkmterl-ss-top-356984-592&tbnid=sLwJXNUB0Uq8qM&vet=12ahUKEwiyveXR-NvzAhXqADQIHRumC9QQMygGegUIARD5Ag..i&docid=X1cfD5hlKU_wcM&w=412&h=549&q=t-shirt%20neon%20yellow&hl=en&client=safari&ved=2ahUKEwiyveXR-NvzAhXqADQIHRumC9QQMygGegUIARD5Ag), [Sweatshirt](https://m.media-amazon.com/images/I/91lHoiqDkYL._AC_UL1500_.jpg), [Vest](https://b-edge.friartux.com/f00000000189637/www.friartux.com/pub/media/catalog/product/cache/a761eb790ae5e77b4635e4d17debaad9/a/p/apple-red-vest-102ap.jpg), [Tuxedo](https://www.google.com/imgres?imgurl=https%3A%2F%2Fwww.lelo.com%2Fsites%2Fdefault%2Ffiles%2FLELO_Accessories_TUX_product_2x_0.jpg&imgrefurl=https%3A%2F%2Fwww.lelo.com%2Ftux&tbnid=gbvGsB8w3BaFLM&vet=12ahUKEwjq0PP--NvzAhVfATQIHUJxCQMQMygFegUIARCVAw..i&docid=K1r3uXvNZQvDJM&w=1000&h=1000&q=tux&hl=en&client=safari&ved=2ahUKEwjq0PP--NvzAhVfATQIHUJxCQMQMygFegUIARCVAw), and [Suit](https://i.insider.com/549af980ecad042d5d8b456a?width=1300&format=jpeg&auto=webp)_
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3w0id6sdxluq7hcorw0f.jpg)_Pants: [Slacks](https://images.thdstatic.com/productImages/67e1a55e-e291-4662-9119-109faeb50bb0/svn/charcoal-red-kap-work-pants-pt38ch-34-32-64_100.jpg), [Jeans](https://cdni.llbean.net/is/image/wim/250265_1145_41?hei=1092&wid=950&resMode=sharp2&defaultImage=llbprod/A0211793_2), [Shorts](https://cdn.shopify.com/s/files/1/0077/0432/products/Khakinators_4_34-_1_1024x1024.jpg?v=1628772654), [Cargo pants](https://cdni.llbean.net/is/image/wim/503739_129_41?hei=1092&wid=950&resMode=sharp2&defaultImage=llbprod/A0211793_2), and [overalls](https://cdn.shopify.com/s/files/1/0207/7214/products/18430---Men_s-Insulated-Overall---019-Black---Front---Noble_620x.jpg?v=1608770176)_
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4y6ze0c5a37bks7jzo4b.png)_So many options_

Cartesian Product

Using our datasets, we’re going to begin creating a combination calculated by the cartesian product. This is an operation where every object is paired with another object. In other words, a shirt for each pair of pants to calculate all possible combinations. This is a many to many relationship, where many objects connect with many other objects.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pfq2d9lg6g3c3rrhetfh.png)_Source: [Mathstopia](https://www.mathstopia.net/wp-content/uploads/2021/01/cartesian-products.jpg)_

Cross Join

Another term for the cartesian product is a cross join, which is a join that follows the operation of a cartesian product, by crossing each object with each other. A many to many relationship can be specified in Pandas using on and matching values. In this case, we want everything to match with everything, so we add a new column called ‘keys’ and store in both the same value.
Image description
Once the key is created, we call merge on the keys to perform the Cartesian Product.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0q3ta9ugch6a6cjtih4.png)_Result of the cross join_

Our result shows that we have 25 different outfits, and will use this finalized data to answer questions about the wardrobe and pick the right outfit for each special occasion, whether it’s a date, casual hangout, trips and more. Let’s start by looking at common scenarios.

Scenarios

What could be worn to a date?

You’ve got to dress to impress by picking formal clothing.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cjfj2mcm80imr5b40dzf.png)_Slacks with a vest, tux, or suit. (Source: Date Night)_

Image description

How about playing at an Amusement Park?
Casual clothing for fun and games!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/er97u5ijmc5zh997x1vl.png)_You’ve got a lot of outfits to choose from._

Image description

What about going to class?
Wear clothes that match. The last thing you want is to be bullied because of the way you dress. There are 11 different combinations of matching clothes that are both casual or not.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdm63zotk9dd9vdyesgx.gif)_Outfits that match (Source: [Tumblr](https://media.giphy.com/media/KiXl0vfc9XIIM/giphy.gif))_

Image description

Anything special we could wear for hiking?
When hiking you want to be prepared, so wear cargo shorts with lots of pockets, along with any casual shirt.
Image description

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/homrxxbv341wmzstumy2.png)_A happy camper wears cargo pants and a t-shirt or a sweatshirt. (Source: [National Geographic](https://www.nationalgeographic.com/travel/national-parks/article/yosemite-national-park))_

Finally, what about a leisure-filled vacation?
You may wear whatever outfit you wish. You’re on vacation and it doesn’t need to match. You have 25 different combinations to choose from.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2mbx1uyc8j11ygq5xe0p.png)_A vacation to the Caribbean (Source: [Jade Mountain](https://www.jetsetter.com//uploads/sites/7/2018/04/ovlZ8yxN-1380x1035.jpeg))_

Conclusion

Outside of saving time and wearing the right attire for the occasion, cross joins are an efficient way to get all combinations. Combined with filters, this can greatly reduce the cost and increase the speed of data preprocessing. Speaking of which, our outfit isn’t complete yet. It’s become customary to wear a mask out in public. Cross join your new combination with another dataset of masks to complete the outfit.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uypzcu6jhjzufp8cm893.png)_Just in time for Halloween! (Source: [Disney](https://i.insider.com/5eaae05f3dac9a601e74b9b6?width=1136&format=jpeg))_

Top comments (4)

Collapse
 
thomaschung408 profile image
Tom Chungry

But what about Mage hats? 🧙

Collapse
 
dangerous profile image
Tommy DANGerous

LOL!

Collapse
 
nathanielthemage profile image
nathaniel-mage

Indeed.

Collapse
 
dangerous profile image
Tommy DANGerous

Love the developer education content!!!