Professional Documents
Culture Documents
Worksheet 2.2
Worksheet 2.2
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:
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.
10.ModelUsage:Usethetrainedmodelstomakepredictionsonnewdata.
11.ModelFine-Tuning(Optional):Dependingonyourresultsandobjectives,youcan fine-
tune your models by adjusting hyperparameters or selecting different features.
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)
#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.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.