Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Challenge | Multivariate Linear Regression
Explore the Linear Regression Using Python

book
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:

  1. [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.
  2. [Line #25-26] Initialize and fit the model (assign the model to the variable model2).
  3. [Line #30-31] Calculate the MAE and assign the result to the variable MAE.
  4. [Line #34-35] Calculate the R-squared and assign the result to the variable r_squared.
  5. [Line #38] Print the intercept, the MAE and the R-squared in this order and round each value to second digit.

Solución

# 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?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 3
# 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

expand
ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

some-alt