Learning from data and use two machine learning concepts explored in the Python code snippets as POC
1 1.Concept: It's a statistical approach for modeling the relationship between a dependent variable (what you want to predict) and one or more independent variables (what you're basing your prediction on). The resulting model is a linear equation that approximates the underlying trend in the data.
 1 2.Example: In the code, linear regression predicts house prices (dependent variable) based on house size and number of bedrooms (independent variables). The model learns the linear relationship between these features and prices from the training data and uses that knowledge to estimate prices for new houses.
2. K-Nearest Neighbors (KNN):
2.1.Concept: A non-parametric classification algorithm that classifies data points based on their similarity to labeled data points in the training set. It finds the k nearest neighbors (data points most similar) to the new data point and assigns the majority class label from those neighbors.
2.2.Example: The code employs KNN to classify data points as "red" or "blue." For a new data point, KNN identifies the 3 closest labeled points (k=3) from the training data. If the majority of those 3 points are classified as "red," the new data point is also predicted as "red."
Here's a Python snippet showcasing both Linear Regression and K-Nearest Neighbors (KNN) for illustration purposes:
from sklearn.linear_model import LinearRegression
from sklearn.neighbors import KNeighborsClassifier
# Sample data for linear regression (house prices)
data = np.array([[1500, 2], [2000, 3], [2500, 4], [3000, 5]])
prices = np.array([250000, 300000, 350000, 400000])
# Linear Regression model and prediction
reg_model = LinearRegression()
reg_model.fit(data, prices)
new_data = np.array([[3500, 6]])
predicted_price = reg_model.predict(new_data)
print("Predicted house price using linear regression:", predicted_price[0])
# Sample data for KNN classification (data points)
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [1, 4], [3, 6], [5, 8]])
y = np.array(["red", "red", "blue", "blue", "red", "red", "blue"])
# KNN model and prediction
knn_model = KNeighborsClassifier(n_neighbors=3)
new_data = np.array([[2, 5]])
predicted_class = knn_model.predict(new_data)
print("Predicted class using KNN:", predicted_class[0])
This code demonstrates the core principles of both algorithms:
 Linear Regression: It learns a linear equation to approximate the relationship between house size/bedrooms (features) and house prices (target variable).
K-Nearest Neighbors (KNN): It classifies new data points based on the majority class of their k nearest neighbors in the training data (k=3 in this case).
These are just two fundamental ML algorithms, but they represent different approaches to learning from data. Linear regression excels at modeling continuous relationships, while KNN is well-suited for classification tasks.
Source https://github.com/RevanthK/Predicting-Book-Sales