Monday, 2 February 2026

DATA SCIENCE LAB MANUAL

 

 Example 1: Linear Regression

PREDITING PRICE BASES ON AREA

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# 1. Example Data
# X = Input (Area of house)
# Y = Output (Price)
X = np.array([100, 200, 300, 400, 500]).reshape(-1, 1)
Y = np.array([10, 20, 30, 40, 50])

# 2. Create and train the model
model = LinearRegression()   # intercept is included by default
model.fit(X, Y)

# 3. Get slope and intercept
m = model.coef_[0]        # slope
b = model.intercept_      # intercept

# 4. Prediction
x_new = 350
y_pred = model.predict([[x_new]])

# 5. Plot the graph
X_line = np.linspace(100, 500, 100).reshape(-1, 1)
Y_line = model.predict(X_line)

plt.scatter(X, Y)          # original data points
plt.plot(X_line, Y_line)   # regression line
plt.scatter(x_new, y_pred) # predicted point
plt.xlabel("Area")
plt.ylabel("Price")
plt.title("Simple Linear Regression")
plt.show()

# 6. Print results
print("Slope (m):", m)
print("Intercept (b):", b)
print("Equation: Y =", m, "* X +", b)
print("Predicted Price for", x_new, ":", y_pred[0])








 Example 2: Linear Regression (Study Hours vs Marks)

👉 Problem Statement

Predict student marks based on study hours.



import numpy as np

import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression


# Data

X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)  # Study hours

Y = np.array([35, 40, 50, 60, 70])            # Marks


# Model

model = LinearRegression()

model.fit(X, Y)


# Slope and intercept

m = model.coef_[0]

b = model.intercept_


# Prediction

hours = 6

predicted_marks = model.predict([[hours]])


# Plot

X_line = np.linspace(1, 6, 100).reshape(-1, 1)

Y_line = model.predict(X_line)


plt.scatter(X, Y)

plt.plot(X_line, Y_line)

plt.scatter(hours, predicted_marks)

plt.xlabel("Study Hours")

plt.ylabel("Marks")

plt.title("Linear Regression: Study Hours vs Marks")

plt.show()


# Output

print("Slope (m):", m)

print("Intercept (b):", b)

print("Equation: Y =", m, "* X +", b)

print("Predicted Marks for", hours, "hours:", predicted_marks[0])



No comments:

Post a Comment

DATA SCIENCE LAB MANUAL

   Example 1: Linear Regression PREDITING PRICE BASES ON AREA import numpy as np import matplotlib.pyplot as plt from sklearn.linear_mo...