Docsity

machine learning report including using linear classification model with python coding, Lab Reports of Machine Learning

Partial preview of the text

Download machine learning report including using linear classification model with python coding and more Lab Reports Machine Learning in PDF only on Docsity!

University of engineering

and technology taxila

Lab 3

(ML & AI)

Name: Hajra Wajid

Reg. No: 23 - CP- 60

Section: Omega

Course title: Machine Learning & Artificial Intelligence

Lab Instructor: Eng. Shahid Ali Bhutta

Task 1:

Linear Regression with one variable

CODE:

The script starts by loading the "big three" of Python data science:

  • NumPy: Used for mathematical operations and generating random numbers.
  • Pandas: Used for creating Data Frames and exporting data to files.
  • Matplotlib: While imported here, it acts as a placeholder for future data visualization.
  • np.random.seed(42) : This is crucial for reproducibility. It ensures that every time you run the code, you get the exact same "random" numbers.
  • np.random.randint(500, 3500, 1000) : This creates 1,000 houses ranging from small apartments (500 sq ft) to large homes (3,500 sq ft). The script uses a linear equation to determine the price based on the size: Price = (Size*100) + 50,000 + Noise
  • The Base Price: Each square foot adds 100$ to the price, with a starting base of 50,000$.
  • np.random.normal function adds "random noise" so the data looks natural.
  • DataFrame: It bundles the sizes and prices into a Pandas table with labeled columns.
  • to_csv : It writes this table to a file named house_data.csv. The index=False argument prevents Pandas from adding an extra column for row numbers.

The model uses Gradient Descent to iteratively minimize the difference between predicted and actual prices. By adjusting the weight (w)or theta0 and bias (b) or theta1over 1, rounds, it converges on a mathematical formula that can predict the price of any house based on its size.

  • theta0 = 0 : This is the Bias. It represents the price when the house size is 0. We start at zero and let the model adjust it.
  • theta1 = 0 : This is the Weight. It represents how much the price increases per square foot.
  • learning_rate = 0.01 : This is the "step size." It tells the model how much to change theta0 and theta1 in each step.
  • epochs = 1000 : This is the number of times the model will practice and update its guess.
  • n = len(X_train_scaled) : This simply counts how many houses (700) are in our training set.

The for i in range(epochs): line starts a loop that repeats 1,000 times. Inside that loop, four things happen:

1. Making a Prediction y_pred = theta0 + theta1 * X_train_scaled: The model calculates a "guessed price" (y_pred) for every house using its current theta0 and theta1. At the very first step, since both are 0, every guess is 0. 2. Calculating the Error (The "Cost function") cost = (1 / (2 * n)) * np.sum((y_pred - y_train) ** 2): This measures how wrong the model is. It takes the difference between the guess (y_pred) and the actual price (y_train), squares it (to make all errors positive), and finds the average. A high cost means the model is doing a poor job. 3. Calculating the Gradients (The "Direction") d_theta0 = (1 / n) * np.sum(y_pred - y_train) d_theta1 = (1 / n) * np.sum((y_pred - y_train) * X_train_scaled) These lines calculate the "slope" of the error.

  • d_theta0 : Tells us if we need to move theta0 up or down.
  • d_theta1 : Tells us if we need to move theta1 up or down based on the house size. 4. Updating the Parameters theta0 = theta0 - learning_rate * d_theta theta1 = theta1 - learning_rate * d_theta This is the moment the model actually "learns." We subtract the gradient (multiplied by the learning rate) from our current thetas. This moves theta0 and theta1 closer to the values that make the most accurate predictions. After 1,000 rounds of this, the code prints out the final "learned" values. print(f"Final theta0: {theta0}") print(f"Final theta1: {theta1}")
  • Final theta0 : The base price the model settled on.
  • Final theta1 : The price increase per scaled square foot.
  • plt.legend() : This adds a small box in the corner explaining that "Blue = Real Data" and "Red = Model Prediction."
  • plt.show() : This final command actually renders the window so you can see the graph on your screen.
  • If the red line cuts through the center of the blue cloud, the Gradient Descent successfully found the pattern.
  • The vertical distance between a blue dot and the red line represents the "error" (or residual) for that specific house.

Task 2: