Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Pejman Rezaei
Pejman Rezaei

Posted on

Supervised vs. Unsupervised Learning

Machine Learning (ML) is a powerful tool that enables computers to learn from data and make predictions or decisions. But not all ML is the same—there are different types of learning, each suited for specific tasks. Two of the most common types areSupervised Learning andUnsupervised Learning. In this article, we’ll explore the differences between them, provide real-world examples, and walk through code snippets to help you understand how they work.


What is Supervised Learning?

Supervised Learning is a type of ML where the algorithm learns from labeled data. In other words, the data you provide to the model includes both input features and the correct output (labels). The goal is for the model to learn the relationship between the inputs and outputs so it can make accurate predictions on new, unseen data.

Real-World Examples of Supervised Learning

Email Spam Detection:

  • Input: The text of an email.

  • Output: A label indicating whether the email is "spam" or "not spam."

  • The model learns to classify emails based on labeled examples.

House Price Prediction:

  • Input: Features of a house (e.g., square footage, number of bedrooms, location).

  • Output: The price of the house.

  • The model learns to predict prices based on historical data.

Medical Diagnosis:

  • Input: Patient data (e.g., symptoms, test results).

  • Output: A diagnosis (e.g., "healthy" or "diabetic").

  • The model learns to diagnose conditions based on labeled medical records.


What is Unsupervised Learning?

Unsupervised Learning is a type of ML where the algorithm learns from unlabeled data. Unlike supervised learning, there are no correct outputs provided. Instead, the model tries to find patterns, structures, or relationships in the data on its own.

Real-World Examples of Unsupervised Learning

Customer Segmentation:

  • Input: Customer data (e.g., age, purchase history, location).

  • Output: Groups of similar customers (e.g., "frequent buyers," "budget shoppers").

  • The model identifies clusters of customers with similar behaviors.

Anomaly Detection:

  • Input: Network traffic data.

  • Output: Identification of unusual patterns that could indicate a cyberattack.

The model detects outliers or anomalies in the data.

Market Basket Analysis:

  • Input: Transaction data from a grocery store.

  • Output: Groups of products frequently bought together (e.g., "bread and butter").

  • The model identifies associations between products.


Key Differences Between Supervised and Unsupervised Learning

AspectSupervised LearningUnsupervised Learning
DataLabeled (inputs and outputs provided)Unlabeled (only inputs provided)
GoalPredict outcomes or classify dataDiscover patterns or structures in data
ExamplesClassification, RegressionClustering, Dimensionality Reduction
ComplexityEasier to evaluate (known outputs)Harder to evaluate (no ground truth)
Use CasesSpam detection, price predictionCustomer segmentation, anomaly detection

Code Examples

Let’s dive into some code to see how supervised and unsupervised learning work in practice. We’ll use Python and the popularScikit-learn library.

Supervised Learning Example: Predicting House Prices

We’ll use a simple linear regression model to predict house prices based on features like square footage.

# Import librariesimportnumpyasnpimportpandasaspdfromsklearn.model_selectionimporttrain_test_splitfromsklearn.linear_modelimportLinearRegressionfromsklearn.metricsimportmean_squared_error# Create a sample datasetdata={'SquareFootage':[1400,1600,1700,1875,1100,1550,2350,2450,1425,1700],'Price':[245000,312000,279000,308000,199000,219000,405000,324000,319000,255000]}df=pd.DataFrame(data)# Features (X) and labels (y)X=df[['SquareFootage']]y=df['Price']# Split the data into training and testing setsX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42)# Train a linear regression modelmodel=LinearRegression()model.fit(X_train,y_train)# Make predictionsy_pred=model.predict(X_test)# Evaluate the modelmse=mean_squared_error(y_test,y_pred)print(f"Mean Squared Error:{mse:.2f}")
Enter fullscreen modeExit fullscreen mode

Unsupervised Learning Example: Customer Segmentation

We’ll use the K-Means clustering algorithm to group customers based on their age and spending habits.

# Import librariesimportnumpyasnpimportpandasaspdfromsklearn.clusterimportKMeansimportmatplotlib.pyplotasplt# Create a sample datasetdata={'Age':[25,34,22,45,32,38,41,29,35,27],'SpendingScore':[30,85,20,90,50,75,80,40,60,55]}df=pd.DataFrame(data)# Features (X)X=df[['Age','SpendingScore']]# Train a K-Means clustering modelkmeans=KMeans(n_clusters=3,random_state=42)df['Cluster']=kmeans.fit_predict(X)# Visualize the clustersplt.scatter(df['Age'],df['SpendingScore'],c=df['Cluster'],cmap='viridis')plt.xlabel('Age')plt.ylabel('Spending Score')plt.title('Customer Segmentation')plt.show()
Enter fullscreen modeExit fullscreen mode

When to Use Supervised vs. Unsupervised Learning

Use Supervised Learning when:

  • You have labeled data.

  • You want to predict outcomes or classify data.

  • Examples: Predicting sales, classifying images, detecting fraud.

Use Unsupervised Learning when:

  • You have unlabeled data.

  • You want to discover hidden patterns or structures.

  • Examples: Grouping customers, reducing data dimensions, finding anomalies.


Conclusion

Supervised and Unsupervised Learning are two fundamental approaches in Machine Learning, each with its own strengths and use cases. Supervised Learning is great for making predictions when you have labeled data, while Unsupervised Learning shines when you want to explore and uncover patterns in unlabeled data.

By understanding the differences and practicing with real-world examples (like the ones in this article), you’ll be well on your way to mastering these essential ML techniques. If you have any questions or want to share your own experiences, feel free to leave a comment below.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Tech Enthusiast
  • Work
    MLOps Engineer
  • Joined

More fromPejman Rezaei

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp