Challenge
Tarea
Swipe to start coding
Let’s indicate the number of nonflavanoid phenols based on the number of flavanoids, total phenols and evaluate our model.
Your plan:
- [Line #24] Split the data 70-30 (70% of the data is for training and 30% is for testing) and insert 1 as a random parameter.
- [Line #25-26] Initialize and fit the model (assign the model to the variable
model2
). - [Line #30-31] Calculate the MAE and assign the result to the variable
MAE
. - [Line #34-35] Calculate the R-squared and assign the result to the variable
r_squared
. - [Line #38] Print the intercept, the MAE and the R-squared in this order and round each value to second digit.
Solución
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['nonflavanoid_phenols'] = wine.target
# Prepare the data
X2 = data[['flavanoids', 'total_phenols']]
Y = data['nonflavanoid_phenols']
# Split the data, initialize 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)
# Calculate the MAE
y_test_predicted2 = model2.predict(X2_test)
from sklearn.metrics import mean_absolute_error
MAE = mean_absolute_error(Y_test, y_test_predicted2)
# Calculate the R-squared
from sklearn.metrics import r2_score
r_squared = r2_score(Y_test, y_test_predicted2)
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 5. Capítulo 3
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['nonflavanoid_phenols'] = wine.target
# Prepare the data
X2 = data[['flavanoids', 'total_phenols']]
Y = data['nonflavanoid_phenols']
# Split the data, initialize and fit the model
X2_train, X2_test, Y_train, Y_test = ___
model2 = ___
___
# Calculate the MAE
y_test_predicted2 = model2.predict(X2_test)
___
___
# Calculate the R-squared
___
___
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla