NumPy, Pandas, Matplotlib Essentials with ML
Train-Test Split Example
This document covers the most important concepts of NumPy, Pandas, and Matplotlib along with a
simple machine learning train-test split example.
Train-Test Split and Model Fitting
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import pandas as pd
# Sample dataset
data = [Link]({
'Hours_Studied': [1,2,3,4,5,6,7,8,9,10],
'Score': [15, 25, 35, 40, 50, 60, 65, 70, 85, 95]
})
X = data[['Hours_Studied']] # features
y = data['Score'] # target
# Split dataset into train (80%) and test (20%)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Fit linear regression model
model = LinearRegression()
[Link](X_train, y_train)
# Evaluate on test data
y_pred = [Link](X_test)
print("Predictions:", y_pred)
print("Model Coefficients:", model.coef_)
print("Intercept:", model.intercept_)
This completes the essentials of NumPy, Pandas, and Matplotlib with a demonstration of how data
is split into training and testing sets and fitted into a simple regression model.