Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Model Using Transformation

Uploaded by

210555
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Model Using Transformation

Uploaded by

210555
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Untitled15.ipynb - Colab https://colab.research.google.com/drive/17YGcy1ylH0KYZGaRfAoaFUxD_bU6EdSK#scrol...

from sklearn.model_selection import train_test_split


from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error, r2_score
import pandas as pd
import numpy as np

dataset=pd.read_csv('/content/stress_dataset.csv')

X = dataset[['b (m)', 'h (m)', 'F (N)', 'd (m)']]

y = dataset['Stress (Pa)']

#log transformation
X_transformed = np.log1p(X)
y_transformed = np.log1p(y)

X_train, X_test, y_train, y_test = train_test_split(X_transformed, y_transformed, test_size=0.2, random_state=42)

linear_model = LinearRegression()
linear_model.fit(X_train, y_train)

▾ LinearRegression i ?

LinearRegression()

y_pred = linear_model.predict(X_test)

mse = mean_squared_error(y_test, y_pred)


print(mse)
r2 = r2_score(y_test, y_pred)

1 of 4 1/8/2025, 5:38 PM
Untitled15.ipynb - Colab https://colab.research.google.com/drive/17YGcy1ylH0KYZGaRfAoaFUxD_bU6EdSK#scrol...

print(r2)

0.9887355225772395
0.896305203788727

predicted_data=pd.DataFrame({'Actual Stress':y_test,'Predicted Stress':y_pred})


predicted_data

Actual Stress Predicted Stress

6252 11.227852 11.919922

4684 10.598566 10.785088

1731 9.425948 9.231151

4742 9.002890 8.835973

4521 8.441606 8.861607

... ... ...

6412 8.437165 8.736912

8285 12.236555 12.780474

7853 8.774751 7.951606

1095 19.580195 17.911179

6929 8.862626 8.511872

2000 rows × 2 columns

Generate code with predicted_data  View recommended plots New interactive sheet

#inverse transformation
y_pred_original = np.expm1(y_pred)
y_test_original = np.expm1(y_test)

2 of 4 1/8/2025, 5:38 PM
Untitled15.ipynb - Colab https://colab.research.google.com/drive/17YGcy1ylH0KYZGaRfAoaFUxD_bU6EdSK#scrol...

mse_original = mean_squared_error(y_test_original, y_pred_original)


r2_original = r2_score(y_test_original, y_pred_original)
print(mse_original)
print(r2_original)

1.7783031359913216e+16
0.10205764377583848

predicted_data=pd.DataFrame({'Actual Stress':y_test_original,'Predicted Stress':y_pred_original})


predicted_data

Actual Stress Predicted Stress

6252 7.519493e+04 1.502288e+05

4684 4.007631e+04 4.829421e+04

1731 1.240516e+04 1.020929e+04

4742 8.125532e+03 6.876239e+03

4521 4.634994e+03 7.054811e+03

... ... ...

6412 4.614449e+03 6.227629e+03

8285 2.061893e+05 3.552125e+05

7853 6.467836e+03 2.839132e+03

1095 3.188385e+08 6.007951e+07

6929 7.062007e+03 4.972466e+03

2000 rows × 2 columns

Generate code with predicted_data


 View recommended plots New interactive sheet

3 of 4 1/8/2025, 5:38 PM
Untitled15.ipynb - Colab https://colab.research.google.com/drive/17YGcy1ylH0KYZGaRfAoaFUxD_bU6EdSK#scrol...

4 of 4 1/8/2025, 5:38 PM

You might also like