Agentic AI Session 6: Machine Learning Basics: Linear Regression in Python (Predicting House Prices)
Welcome back to the channel! Before our AI Agents can make complex decisions, we need to understand the underlying predictive tools they use. Today, we are diving into Machine Learning fundamentals by building a Linear Regression model from scratch using Python. In this tutorial, we take a real-world dataset (USA_Housing.csv) and train a model to predict house prices based on features like area income, house age, and number of rooms. What you will learn in this coding session: Data Handling: Using pandas to load and explore our dataset. Feature Selection: Splitting our data into X (features) and y (target price). Train/Test Split: Using scikit-learn to properly divide our data for training and evaluation. Model Training: Initializing and fitting a LinearRegression model. Evaluation Metrics: Understanding how to calculate and read MAE, MSE, and RMSE to see how accurate our model really is. Looking for Structured, Instructor-Led Training? If you are serious about mastering AI, Machine Learning, and Data Analytics, a huge shoutout to our official sponsor: GDlearn.in. They provide top-tier, instructor-led courses featuring industry experts who give you real-time, hands-on experience. Whether you want to learn online or offline, GDlearn is the perfect place to grow your skills. Check the link below to connect with them! code import pandas as pd import numpy as np USAhousing=pd.read_csv('USA_Housing.csv') correlation_matrix=USAhousing.corr(numeric_only=True) #print(USAhousing.head()) #print(USAhousing.info()) #print(USAhousing.describe()) #print(USAhousing.columns) #print(correlation_matrix) X=USAhousing[['Avg. Area Income','Avg. Area House Age', 'Avg. Area Number of Rooms','Avg. Area Number of Bedrooms', 'Area Population']] y=USAhousing['Price'] #print(X.head()) #print(y.head()) from sklearn.model_selection import train_test_split X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3, random_state=101) from sklearn.linear_model import LinearRegression lm=LinearRegression() lm.fit(X_train,y_train) #print(lm.intercept_) coeff_df=pd.DataFrame(lm.coef_,X.columns,columns=['Coefficient']) #print(coeff_df) predictions=lm.predict(X_test) df=pd.DataFrame(predictions,y_test) #print(df) from sklearn import metrics print('MAE:', metrics.mean_absolute_error(y_test,predictions)) print('MSE:', metrics.mean_squared_error(y_test,predictions)) print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test,predictions)))
Download
0 formatsNo download links available.