Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge | Building and Training Model
Explore the Linear Regression Using Python

book
Challenge

Let’s combine our knowledge!

Task

Swipe to start coding

In this task, you build, train and fit your model and make predictions based on it. This time you will make predictions about total_phenols, based on flavanoids. It means that your target now is total_phenols.

Your plan:

  1. [Line #18] Define the target (in this task it's total_phenols).
  2. [Line #25] Split the data 70-30 (70% of the data is for training and 30% is for testing) and insert 1 as a random parameter.
  3. [Line #26] Initialize linear regression model .
  4. [Line #27] Fit the model using your tain data.
  5. [Line #30] Assign np.array() to the variable new_flavanoids if their number is 1 (don't forget to use function .reshape(-1,1)).
  6. [Line #31] Predict and assign the amount of flavanoids to the variable predicted_value.
  7. [Line #32] Print the predicted amount of flavanoids.

Solution

# 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
import numpy as np

# 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

# Define the data we will work with
x = data[['flavanoids']]
y = data['total_phenols']

# Build and fit the model
X_train, X_test, Y_train, Y_test = train_test_split(x, y, test_size = 0.3, random_state = 1)
model = LinearRegression()
model.fit(X_train, Y_train)

# Make predictions
new_flavanoids = np.array([1]).reshape(-1, 1)
predicted_value = model.predict(new_flavanoids)
print(predicted_value)

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 4
# 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
import numpy as np

# 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
___ = wine.target

# Define the data we will work with
x = data[['flavanoids']]
y = data['total_phenols']

# Build and fit the model
X_train, X_test, Y_train, Y_test = ___
model = ___
___

# Make predictions
new_flavanoids = ___
predicted_value = ___
___
toggle bottom row
some-alt