Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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