> For the complete documentation index, see [llms.txt](https://tusharkolekar24.gitbook.io/package-kolekar-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tusharkolekar24.gitbook.io/package-kolekar-1/case-study/lassoregression.md).

# LassoRegression

### Import Required Libraries

```python
import pandas as pd
from package_Kolekar.Gradient_Descent.LassoRegression import LassoRegressionModel
```

### Import Dataset (case1 from NASA Milling Dataset)

```python
 df = pd.read_csv('https://raw.githubusercontent.com/tusharkolekar24/package_Kolekar/main/time_domain_case1.csv')
 print("Shape of the Dataset:", df.shape)
 df.head()
```

![](/files/iBfnHlEXqJJtfnqT4iIw)

### Model Building Process

```python
model = LassoRegressionModel(learning_rate=0.076, 
                             max_iter=1000,
                             l1_penalty=1)
```

### Data Preparation Process

```python
X, y = model.data_preparation(df)
print("Shape of X:",X.shape)
print("Shape of y:",y.shape)
```

<div align="left"><img src="/files/swjseOmX1wbUuFlyWExO" alt=""></div>

### Data Normalization

```python
X_scaled,y_scaled = model.data_normalization(df)
print("Shape of X_scaled:",X_scaled.shape)
print("Shape of y_scaled:",y_scaled.shape)
```

<div align="left"><img src="/files/swjseOmX1wbUuFlyWExO" alt=""></div>

### Train Test Splitting

```python
X_train,X_test,y_train,y_test = model.data_split_train_test(X_scaled,y_scaled)
print("Shape of X_train:",X_train.shape)
print("Shape of X_test :",X_test.shape)
print("Shape of y_train:",y_train.shape)
print("Shape of y_test :",y_test.shape)
```

<div align="left"><img src="/files/nsPxtNZaM6KVigVshqxA" alt=""></div>

### &#x20;Model training & performing Predictions

```python
model.get_fit(X_train,y_train)

y_train_pred = model.predict(X_train)
y_test_pred = model.predict(X_test)
```

### Model Performance Evaluation

```python
result_train = model.performance_evaluation(y_train,y_train_pred)
#print("MAE for train set       :",result_train[0])
#print("MSE for train set       :",result_train[1])
#print("RMSE for train set      :",result_train[2])
#print("MAPE for train set      :",result_train[3])
#print("R^2 score for train set :",result_train[4])

result_test = model.performance_evaluation(y_test,y_test_pred)
a = pd.DataFrame([["Lasso Regression",result_train[0],result_train[1],result_train[2],result_train[3],result_train[4],"Training"]],
                  columns=['Method','MAE','MSE','RMSE','MAPE','R2_score','Dataset'])
                  
b = pd.DataFrame([["Lasso Regression",result_test[0],result_test[1],result_test[2],result_test[3],result_test[4],"Testing"]],
                  columns=['Method','MAE','MSE','RMSE','MAPE','R2_score','Dataset'])                  
                  
a = a.append(b)                  
a
```

![](/files/ewXBAKR5CFo4tcdih2LP)

## Hyper-Parameter-tuning

Based on L1\_penalty

Where Epoch =1000

```python
import numpy as np
count =0
list1,list2,list3,list4,list5=[],[],[],[],[]
for i in np.arange(0,10,0.01):
    count+=1

    model = LassoRegressionModel(learning_rate=0.076, max_iter=1000,l1_penalty=i)
    model.get_fit(X_train,y_train)
    y_train_pred1 = model.predict(X_train)
    y_test_pred1 = model.predict(X_test)
    result_test = model.performance_evaluation(y_test,y_test_pred1)
    result_train = model.performance_evaluation(y_train,y_train_pred1)
    if result_test[4]>0:
        list1.append(i)
        list2.append(result_test[4])
        list3.append(result_test[1])
        list4.append(result_train[4])
        list5.append(result_train[1])
        #print(j,count,result_test[4])
result = pd.DataFrame({"l1_penalty":list1,'test_acc':list2,
            "test_MSE":list3,"train_acc":list4,"train_MSE":list5})
```

### Graphical Representation of the Predicted output

```python
import matplotlib.pyplot as plt
plt.figure(figsize=(12,6))
plt.subplot(1,2,1)
plt.plot(result.l1_penalty,result.test_acc)
plt.plot(result.l1_penalty,result.train_acc)
plt.xlim(result.l1_penalty.min(),result.l1_penalty.max())
plt.xlabel("l1_penalty")
plt.ylabel("Accuracy of the models")
plt.grid()
plt.subplot(1,2,2)
plt.plot(result.l1_penalty,result.test_MSE)
plt.plot(result.l1_penalty,result.train_MSE)
plt.xlim(result.l1_penalty.min(),result.l1_penalty.max())
plt.xlabel("l1_penalty")
plt.ylabel("MSE of the models")
plt.grid()
plt.show()
```

![](/files/CJAUTloByN7Bxvp810fz)

## Hyper-Parameter-tuning

Based on Epoch

Learning Rate =0.076

```python
import numpy as np
count =0
list11,list12,list13,list14,list15=[],[],[],[],[]
for i in np.arange(0,10000,100):
    count+=1    
    model = LassoRegressionModel(learning_rate=0.076, max_iter=i,l1_penalty=7.65)
    model.get_fit(X_train,y_train)
    y_train_pred2 = model.predict(X_train)
    y_test_pred2 = model.predict(X_test)
    result_test1 = model.performance_evaluation(y_test,y_test_pred2)
    result_train1 = model.performance_evaluation(y_train,y_train_pred2)
    if result_test1[4]>0:
        list11.append(i)
        list12.append(result_test1[4])
        list13.append(result_test1[1])
        list14.append(result_train1[4])
        list15.append(result_train1[1])
        #print(j,count,result_test[4])
result1 = pd.DataFrame({"Epoch":list11,'test_acc':list12,
          "test_MSE":list13,"train_acc":list14,"train_MSE":list15})
```

![](/files/B0aeX3qrPH3ACGnOEA8F)

### Graphical Representation of the Predicted output
