Sunday, 11 January 2026

Very Simple Linear Regression Programs



Very Simple  Linear Regression Programs to predict the Study Hours vs Marks Prediction


Step 1: Import Required Libraries

👉 Libraries are ready-made tools

import pandas as pd import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression

Explain:

  • pandas → handles data

  • matplotlib → shows graph

  • LinearRegression → regression model


Step 2: Create Data

👉 This is our learning data

data = { 'Hours': [1, 2, 3, 4, 5], 'Marks': [20, 35, 50, 65, 80] } df = pd.DataFrame(data)

Explain:

  • Hours → Input (X)

  • Marks → Output (Y)


Step 3: Separate Input and Output

X = df[['Hours']] y = df['Marks']

Explain:

  • Machine learns X → Y relation


Step 4: Train the Regression Model

model = LinearRegression() model.fit(X, y)

Explain:

  • fit() → Machine learns pattern


Step 5: Predict Marks

new_data = pd.DataFrame({'Hours': [6]})
predicted_marks = model.predict(new_data)
print("Predicted Marks:", predicted_marks)

Explain:

  • If student studies 6 hours, marks are predicted


Step 6: Show Graph

plt.scatter(X, y) plt.plot(X, model.predict(X)) plt.xlabel("Study Hours") plt.ylabel("Marks") plt.show()

Explain:

  • Dots → Actual data

  • Line → Prediction line

No comments:

Post a Comment

Blockchain technology

  Blockchain technology is a decentralized, distributed digital ledger that securely records transactions across many computers, creating an...