Try to Evaluate
Let’s see which model is better using the metrics we already know.
MSE:
9
1
2
3
from sklearn.metrics import mean_squared_error
print(mean_squared_error(Y_test, y_test_predicted).round(2))
print(mean_squared_error(Y_test, y_test_predicted2).round(2))
123from sklearn.metrics import mean_squared_error print(mean_squared_error(Y_test, y_test_predicted).round(2)) print(mean_squared_error(Y_test, y_test_predicted2).round(2))
python9123Output:0.280.27
MAE:
9
1
2
3
from sklearn.metrics import mean_absolute_error
print(mean_absolute_error(Y_test, y_test_predicted).round(2))
print(mean_absolute_error(Y_test, y_test_predicted2).round(2))
123from sklearn.metrics import mean_absolute_error print(mean_absolute_error(Y_test, y_test_predicted).round(2)) print(mean_absolute_error(Y_test, y_test_predicted2).round(2))
python9123Output:0.450.43
R-squared:
9
1
2
3
from sklearn.metrics import r2_score
print(r2_score(Y_test, y_test_predicted).round(2))
print(r2_score(Y_test, y_test_predicted2).round(2))
123from sklearn.metrics import r2_score print(r2_score(Y_test, y_test_predicted).round(2)) print(r2_score(Y_test, y_test_predicted2).round(2))
python9123Output:0.530.55
As a general rule, the more features a model includes, the lower the MSE (RMSE) and MAE will be. However, be careful about including too many features. Some of them may be extremely random, degrading the model's interpretability.
Tehtävä
Swipe to start coding
Let’s evaluate the model from the previous task:
- [Line #30] Import
mean_squared_error
for calculating metrics fromscikit.metrics
. - [Line #31] Find MSE using method
mean_squared_error()
andY_test
,y_test_predicted2
as the parameters, assign it to the variableMSE
, round the result to second digit. - [Line #32] Print the variable
MSE
. - [Line #35] Import
r2_score
fromscikit.metrics
. - [Line #36] Find R-squared and assign it to the variable
r_squared
, round the result to second digit. - [Line #37] Print the variable
r_squared
.
Ratkaisu
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Import the libraries
import pandas as pd
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# Load the dataset
wine = load_wine()
# Configure pandas to show all features
pd.set_option('display.max_rows', None, 'display.max_columns', None)
# Define the DataFrame
data = pd.DataFrame(data = wine['data'], columns = wine['feature_names'])
# Define the target
data['total_phenols'] = wine.target
# Prepare the data
X2 = data[['flavanoids', 'nonflavanoid_phenols']]
Y = data['total_phenols']
# Train and fit the model
X2_train, X2_test, Y_train, Y_test = train_test_split(X2, Y, test_size = 0.3, random_state = 1)
model2 = LinearRegression()
model2.fit(X2_train, Y_train)
y_test_predicted2 = model2.predict(X2_test)
# Calculate the MSE
from sklearn.metrics import mean_squared_error
MSE = mean_squared_error(Y_test, y_test_predicted2).round(2)
print(MSE)
# Calculate the R-squared
from sklearn.metrics import r2_score
r_squared = r2_score(Y_test, y_test_predicted2).round(2)
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 5. Luku 2
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Import the libraries
import pandas as pd
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# Load the dataset
wine = load_wine()
# Configure pandas to show all features
pd.set_option('display.max_rows', None, 'display.max_columns', None)
# Define the DataFrame
data = pd.DataFrame(data = wine['data'], columns = wine['feature_names'])
# Define the target
data['total_phenols'] = wine.target
# Prepare the data
X2 = data[['flavanoids', 'nonflavanoid_phenols']]
Y = data['total_phenols']
# Train and fit the model
X2_train, X2_test, Y_train, Y_test = train_test_split(X2, Y, test_size = 0.3, random_state = 1)
model2 = LinearRegression()
model2.fit(X2_train, Y_train)
y_test_predicted2 = model2.predict(X2_test)
# Calculate the MSE
from ___ import ___
MSE = ___
print(___)
# Calculate the R-squared
from ___ import ___
r_squared = ___
print(___)
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme