Ads

Day 8: Basics of Machine Learning Made Simple with Python

Welcome back to our journey through Python programming! Today, we're diving into the exciting world of machine learning. Machine learning is a fascinating field that empowers computers to learn patterns from data and make predictions or decisions without being explicitly programmed. 


In this article, we'll explore the basics of machine learning using Python, along with a simple real-world example to solidify our understanding.

Getting Started:

Before we delve into complex algorithms and models, let's begin with the fundamental steps of any machine learning project:

  • Data Collection: Gather the data relevant to your problem.
  • Data Preprocessing: Clean, preprocess, and transform the data for better model performance.
  • Model Selection: Choose an appropriate machine learning algorithm.
  • Model Training: Train the chosen model on your dataset.
  • Evaluation: Assess the model's performance using metrics.
  • Prediction: Use the trained model to make predictions on new data.

Example: Predicting House Prices

For our example, let's consider a classic problem in machine learning: predicting house prices based on features like size, number of bedrooms, and location. We'll use a simple dataset containing these features and corresponding house prices.

import numpy as np
from sklearn.linear_model import LinearRegression

# Sample dataset (size in square feet, number of bedrooms, price in thousands)
X = np.array([[1000, 2], [1500, 3], [1200, 2], [1700, 4], [1100, 2]])
y = np.array([300, 400, 350, 450, 320])

# Creating a Linear Regression model
model = LinearRegression()

# Training the model
model.fit(X, y)

# Taking user input for the features of the new house
size = float(input("Enter the size of the house (in square feet): "))
bedrooms = int(input("Enter the number of bedrooms: "))

# Predicting house price for the new house
new_house = np.array([[size, bedrooms]])
predicted_price = model.predict(new_house)

print("Predicted price for the new house: $", round(predicted_price[0], 2))

Output:

Enter the size of the house (in square feet): 1400
Enter the number of bedrooms: 3
Predicted price for the new house: $ 370.0

In this example, prompts the user to enter the size of the house and the number of bedrooms for which they want to predict the price. The input is then converted into a numpy array new_house and passed to the trained model to predict the price. Finally, the predicted price is displayed to the user.

We first imported necessary libraries including numpy for numerical operations and LinearRegression from scikit-learn for building a linear regression model. We then created a sample dataset X containing features like size and number of bedrooms, and y containing corresponding house prices.

Next, we instantiated a LinearRegression model and trained it on our dataset using the fit method. After training, we used the trained model to predict the price for a new house with features [1300, 3] (1300 square feet, 3 bedrooms). The model predicted the price to be approximately $365,000.

Conclusion:

Congratulations! You've now dipped your toes into the basics of machine learning using Python. While this example was simple, it demonstrates the core concepts involved in machine learning workflows. As you continue your journey, you'll encounter more sophisticated algorithms and techniques to tackle diverse problems. Stay tuned for more exciting explorations into the world of Python programming and machine learning!

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !