Challenge
Let’s combine our knowledge!
Завдання
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:
- [Line #18] Define the target (in this task it's
total_phenols
). - [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. - [Line #26] Initialize linear regression model .
- [Line #27] Fit the model using your tain data.
- [Line #30] Assign
np.array()
to the variablenew_flavanoids
if their number is1
(don't forget to use function.reshape(-1,1)
). - [Line #31] Predict and assign the amount of flavanoids to the variable
predicted_value
. - [Line #32] Print the predicted amount of flavanoids.
Рішення
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
# 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)
Все було зрозуміло?
Дякуємо за ваш відгук!
Секція 3. Розділ 4
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
# 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 = ___
___
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат