0% found this document useful (0 votes)
5 views7 pages

Linear-Regression-By Reyan Khattak

Uploaded by

yasiniqbal302
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Linear-Regression-By Reyan Khattak

Uploaded by

yasiniqbal302
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

linear-regression-hw

September 30, 2024

[1]: import numpy as np #Numpy is a library used for␣


↪numerical computations. It provides support for arrays and matrices,

#along with a collection of ␣


↪mathematical functions to operate on these arrays efficiently.

import matplotlib.pyplot as plt # It is used for creating␣


↪static, animated, and interactive visualizations in Python.

from sklearn.linear_model import LinearRegression #sklearn.linear_model.


↪LinearRegression: This imports the LinearRegression class from the

#linear_model module of␣


↪scikit-learn, which is a powerful machine learning library in Python.

#The LinearRegression class is␣


↪used to create a linear regression model, which can be used to fit a line

#to data and make predictions␣


↪based on it.

[4]: # Given data for simple linear regression as available in slides. defining two␣
↪numpy arrays, ages and glucose_levels, which are likely used to

#represent data related to individuals' ages and their corresponding glucose␣


↪levels.

ages = np.array([43, 21, 25, 42, 57, 59]).reshape(-1, 1) #.reshape(-1, 1)␣


↪reshapes the array into a 2D column vector, where each number is now its own

#row. This is useful␣


↪for machine learning models, as they expect input to be in this form.

glucose_levels = np.array([99, 65, 79, 75, 87, 81]) #This array remains␣
↪as a 1D array.

print(ages)
print(glucose_levels)

[[43]
[21]
[25]
[42]
[57]

1
[59]]
[99 65 79 75 87 81]

[5]: # Creatinng the Simple Linear Regression model


model_simple = LinearRegression() #This line creates an instance of the␣
↪LinearRegression model from the sklearn.linear_model module.

#This model is designed to find a linear␣


↪relationship between one or more independent variables (in this case, ages)

#and a dependent variable (glucose_levels).

#Training the Simple Linear Regression model


model_simple.fit(ages, glucose_levels) #The .fit() method trains the␣
↪linear regression model using the provided data

#(ages as input features and␣


↪glucose_levels as target values).

[5]: LinearRegression()

[6]: # Predicted glucose levels for original data mean without new instance i.e. age␣
↪= 55

#This line of code predicts glucose levels using the trained simple linear␣
↪regression model.

glucose_predicted = model_simple.predict(ages) #The .predict() method is␣


↪called on the trained model_simple with the input ages.

#The model uses the linear␣


↪relationship it learned during training to estimate the corresponding␣

↪glucose

#levels for each age in the␣


↪ages array.

#The predicted glucose levels are stored in the variableglucose_predicted.These␣


↪predicted values represent the model’s best guess for glucose levels

#based on the learned linear trend between age and glucose.dicted.

[9]: #This section of the code takes an age input from the user and predicts the␣
↪corresponding glucose level using the trained linear regression model.

# Taking age value input from the user


user_age = float(input("Enter the age of the patient: ")) #This line prompts␣
↪the user to enter an age (as a floating-point number). The

#input() function reads␣


↪the input as a string, and float() converts it to a numeric value.

# Predict glucose level for the entered age value

2
user_age_reshaped = np.array([[user_age]]) #Since the model␣
↪expects the input in a specific 2D array format (like [[age]]),

#the entered age is␣


↪reshaped using np.array([[user_age]]). This ensures the age is passed in

#the correct format␣


↪for prediction.

predicted_glucose_user = model_simple.predict(user_age_reshaped) #This line␣


↪uses the predict() method of the model_simple to predict the glucose level

#for the␣
↪entered age. The result is stored in predicted_glucose_user, which contains

#the␣
↪predicted glucose level based on the user's age.

Enter the age of the patient: 55

[10]: # Plot the original data and the regression line


plt.scatter(ages, glucose_levels, color='blue', label='Actual Glucose Levels')
plt.plot(ages, glucose_predicted, color='red', label='Regression Line')

# Plot the predicted point for the user input


plt.scatter(user_age, predicted_glucose_user, color='green', label=f'Predicted␣
↪Glucose ({user_age} years)', s=100, marker='x')

#In a plot (likely a scatter plot), the parameters s=100 and marker='x' specify␣
↪the size and shape of the markers that represent data points.

# Customize plot
plt.title('Simple Linear Regression: Age vs Glucose Level')
plt.xlabel('Age')
plt.ylabel('Glucose Level')
plt.legend()

# Display plot
plt.show()

# Print the predicted glucose value for the user's age


print(f"The predicted glucose level for a patient aged {user_age} is:␣
↪{predicted_glucose_user[0]:.2f}")

#This is an f-string in Python, which allows embedding variables directly into␣


↪the string.{user_age} inserts the value of user_age into the string.

#{predicted_glucose_user[0]:.2f} inserts the predicted glucose level, formatted␣


↪to two decimal places (the .2f part).

#Since the prediction result is likely an array (from the model_simple.


↪predict() call), this extracts the first (and likely only) predicted value.

3
The predicted glucose level for a patient aged 55.0 is: 86.33

[11]: # Multiple Linear Regression

[12]: import numpy as np


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

[13]: # Given data for multiple linear regression as mentioned in slides


blood_pressure = np.array([130, 140, 150, 135, 145]).reshape(-1, 1)
cholesterol_level = np.array([200, 220, 240, 210, 230]).reshape(-1, 1)
stroke = np.array([0.25, 0.35, 0.50, 0.30, 0.40])

[ ]: # Combine blood pressure and cholesterol into a single matrix


X = np.hstack((blood_pressure, cholesterol_level))
print(X)

#np.hstack((blood_pressure, cholesterol_level)) combines both independent␣


↪variables into a single matrix X, which is necessary for

#multiple linear regression.

4
[15]: # Creating and training the Multiple Linear Regression model
model_multiple = LinearRegression()
model_multiple.fit(X, stroke) #model_multiple.fit(X, stroke) fits the␣
↪linear regression model using the combined independent variables X and

#the target variable stroke.

[15]: LinearRegression()

[16]: # Taking blood pressure and cholesterol level input from the user
user_blood_pressure = float(input("Enter the blood pressure of the patient: "))
user_cholesterol_level = float(input("Enter the cholesterol level of the␣
↪patient: "))

Enter the blood pressure of the patient: 157


Enter the cholesterol level of the patient: 269

[18]: # Predict stroke risk for the entered values


X_new_user = np.array([[user_blood_pressure, user_cholesterol_level]])
print(X_new_user)
predicted_stroke_user = model_multiple.predict(X_new_user)

[[157. 269.]]

[21]: # Plot the original data and predicted values


plt.scatter(blood_pressure, stroke, color='blue', label='Actual Stroke (Blood␣
↪Pressure)')

plt.scatter(cholesterol_level, stroke, color='green', label='Actual Stroke␣


↪(Cholesterol)')

plt.plot(blood_pressure, model_multiple.predict(X), color='red',␣


↪label='Regression Line')

#Scatter Plot:
#Blue points represent the actual stroke values against blood_pressure.
#Green points represent the actual stroke values against cholesterol_level.

#Regression Line:
#The red line represents the predicted stroke values based on blood_pressure.␣
↪However, since this is a multiple linear regression with

#two predictors (blood_pressure and cholesterol_level), the line is only␣


↪indicative of how the model fits using blood_pressure alone for this

#visualization.

#Note:
#In multiple linear regression with multiple independent variables, it's␣
↪challenging to directly plot a single "regression line," because

#it's a plane (or higher-dimensional shape) instead of a line.

5
# Plot the predicted point for the user input
plt.scatter(user_blood_pressure, predicted_stroke_user, color='purple',␣
↪label=f'Predicted Stroke', s=100, marker='x')

# Customize plot
plt.title('Multiple Linear Regression: Stroke Prediction')
plt.xlabel('Blood Pressure / Cholesterol Level')
plt.ylabel('Stroke Risk')
plt.legend()

# Display plot
plt.show()

# Print the predicted stroke value for the user's inputs


print(f"The predicted stroke risk for a patient with blood pressure␣
↪{user_blood_pressure} and cholesterol {user_cholesterol_level} is:␣

↪{predicted_stroke_user[0]:.2f}")

The predicted stroke risk for a patient with blood pressure 157.0 and

6
cholesterol 269.0 is: 0.64

[ ]:

You might also like