Linear-Regression-By Reyan Khattak
Linear-Regression-By Reyan Khattak
[4]: # Given data for simple linear regression as available in slides. defining two␣
↪numpy arrays, ages and glucose_levels, which are likely used to
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]: 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
[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.
2
user_age_reshaped = np.array([[user_age]]) #Since the model␣
↪expects the input in a specific 2D array format (like [[age]]),
#for the␣
↪entered age. The result is stored in predicted_glucose_user, which contains
#the␣
↪predicted glucose level based on the user's age.
#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()
3
The predicted glucose level for a patient aged 55.0 is: 86.33
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
[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: "))
[[157. 269.]]
#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
#visualization.
#Note:
#In multiple linear regression with multiple independent variables, it's␣
↪challenging to directly plot a single "regression line," because
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()
↪{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
[ ]: