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

Worksheet 2.2

The document describes implementing linear regression and logistic regression models. It outlines the steps to: 1) prepare and split the data, 2) import libraries, 3) load the data, 4) select features, 5) split into training and testing sets, 6) implement the models, 7) evaluate the models, 8) use the models to make predictions, and 9) optionally fine-tune or deploy the models. Code examples are provided to fit linear and logistic regression models to sample data and calculate evaluation metrics. The learning outcomes of the experiment are also summarized.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views7 pages

Worksheet 2.2

The document describes implementing linear regression and logistic regression models. It outlines the steps to: 1) prepare and split the data, 2) import libraries, 3) load the data, 4) select features, 5) split into training and testing sets, 6) implement the models, 7) evaluate the models, 8) use the models to make predictions, and 9) optionally fine-tune or deploy the models. Code examples are provided to fit linear and logistic regression models to sample data and calculate evaluation metrics. The learning outcomes of the experiment are also summarized.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

DEPARTMENTOF

COMPUTERSCIENCE&ENGINEERING

Experiment-2.2
Student Name: Abhishek UID: 21BCS4789
Branch: CSE Section/Group: 644-B
Semester: 5th Date of Performance: 24/10/23
Subject Name: AIML-Lab Subject Code: 21CSH-316

1. Aim:ImplementingLinearRegressionandLogisticRegressionmodels
2. Objective:
 Tolearnaboutdifferentfunctions.
 TolearnAboutDifferentLinearRegressionTechniques.
 ToLearnaboutLinearRegressionModelor algorithms

3. Procedure:

1. Data Preparation:Collect and preprocess your dataset, ensuring it's cleaned,


formatted,andsplitintotrainingandtestingsets(typically70-80%fortrainingandthe rest for
testing).

2. ImportLibraries:InPython,importnecessarylibraries,includingNumPyfor
numericaloperations,pandasfordatamanipulation,andscikit-learnformachine
learning tools.

3. LoadData:LoadyourdatasetintoapandasDataFrame oranotherappropriatedata
structure.
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING

4. FeatureSelection:Choosetherelevantfeatures(independentvariables)andtarget
variable (dependent variable) for your regression model.

5. SplitData:Splityourdataintotrainingandtestingsets.

6. ImplementLinearRegression:Createandtrainalinearregressionmodelusing
scikit-learn.

7. ImplementLogisticRegression:Createandtrainalogisticregressionmodelusing
scikit-learn.

8. Model Evaluation (Linear Regression): For linear regression, evaluate the model
usingmetricslikeMeanSquaredError(MSE),R-squared,andvisualizationssuchas
scatter plots for predictions vs. actual values.

9. Model Evaluation (Logistic Regression): For logistic regression, evaluate the


modelusingclassificationmetricslikeaccuracy,precision,recall,F1-score,and
ROC-AUC.

10.ModelUsage:Usethetrainedmodelstomakepredictionsonnewdata.

11.ModelFine-Tuning(Optional):Dependingonyourresultsandobjectives,youcan fine-
tune your models by adjusting hyperparameters or selecting different features.

12.Deployment (Optional):This procedure provides a high-level overview of


implementinglinearandlogisticregressionmodels.Dependingonyourspecific
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING

dataset and problem, you may need to customize and expand upon these steps.
Additionally,youcanexploremoreadvancedtechniqueslikeregularizedregression or
feature engineering to improve model performance.

4. Code:

#Importnecessarylibraries
import numpy as np
importmatplotlib.pyplotasplt
fromsklearn.linear_modelimportLinearRegression,LogisticRegression from
sklearn.model_selection import train_test_split
fromsklearn.metricsimportmean_squared_error,accuracy_score

#GeneratesomesampledataforLinearRegression
np.random.seed(0)
X=np.random.rand(100, 1)
y=4*X+1+0.1*np.random.randn(100,1)

#Splitthedataintotrainingandtestingsets
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2, random_state=42)

#CreateaLinearRegressionmodel
linear_reg = LinearRegression()

#Fitthemodeltothetrainingdata
linear_reg.fit(X_train, y_train)

# Make predictions on the test data


y_pred=linear_reg.predict(X_test)
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING

#Calculatethemeansquarederror
mse=mean_squared_error(y_test,y_pred)
print("----Sparsh Gupta 21BCS4907----")
print(f"LinearRegressionMeanSquaredError:{mse}")

#Plotthedataandthelinearregressionline plt.scatter(X,
y, label='Data')
plt.plot(X_test,y_pred,color='red',linewidth=3,label='LinearRegression')
plt.legend()
plt.title('LinearRegression')
plt.show()

importnumpyasnp
fromsklearn.linear_modelimportLogisticRegression import
matplotlib.pyplot as plt

np.random.seed(0)
X=2*np.random.rand(100,1) y
= (X > 1).astype(int).flatten()

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

X_new=np.linspace(0,2,100).reshape(-1,1) y_prob
= model.predict_proba(X_new)[:, 1]

plt.scatter(X, y, c='b', marker='o', label='Actual Data')


plt.plot(X_new,y_prob,'r-',label='LogisticRegression')
plt.xlabel("X")
plt.ylabel("Probability")
plt.legend()
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING

plt.title("LogisticRegressionSampleData")
plt.show()

5. Output:
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING

LearningOutcomes:

LinearRegression:

1. Learnhowtomodelandunderstandlinearrelationshipsbetweenvariables.

2. Masterthetechniqueofestimatingmodelparametersusingleastsquares.
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING

3. Developtheabilitytointerpretcoefficientstoexplainhowpredictorsimpactthe target
variable.

4. UnderstandhowtoassessmodelperformanceusingmetricslikeMeanSquared Error
(MSE) and R-squared.

5. Recognizeandaddressviolationsoflinearregressionassumptions.

LogisticRegression:

1. Learntoperformbinaryclassificationbymodelingprobabilities.

2. Understandthelogisticfunctionandhowittransformslinearpredictorsinto
probabilities.

3. Interpretcoefficientstounderstandhowpredictorsaffecttheprobabilityofa
positive class.

4. Evaluatemodelperformanceinbinaryclassificationtasksusingmetricslike
accuracy and ROC curves.

5. Grasptheconceptofmaximumlikelihoodestimationforparameterestimationin
logistic regression.

You might also like