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

Conteúdo do Curso

Explore the Linear Regression Using Python

Explore the Linear Regression Using Python

1. What is the Linear Regression?
2. Correlation
3. Building and Training Model
4. Metrics to Evaluate the Model
5. Multivariate Linear Regression

bookFitting

Fitting the model means finding the most appropriate model based on training data. In our case, it is linear regression parameters - slope and intercept. Then we will use the appropriate model to make predictions and evaluate the predictions. As said before, for the training model you will use only the training subset and also the fit() function:

1
model.fit(X_train,Y_train)
copy

This method executes computations storing the result in the model object. So, our model has been built, and we have parameters for our straight line. Let’s take a look at the intercept and slope we have received.

12
print(model.intercept_) print(model.coef_)
copy

All model parameters in sklearn have trailing underscores.

Having these two parameters, we can build the line to predict future values on our dataset. Let’s do the same with the wine dataset to predict the number of flavonoids on total phenols.

1234567891011121314151617181920212223242526272829303132
# Import the libraries from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split import pandas as pd from sklearn.datasets import load_wine # 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['flavanoids'] = wine.target # Define the data we will work with x = data[['total_phenols']] y = data['flavanoids'] # Build and fit the model model = LinearRegression() X_train, X_test, Y_train, Y_test = train_test_split(x, y, test_size = 0.3,random_state = 1) model.fit(X_train, Y_train) # Print parameters print(model.intercept_) print(model.coef_)
copy

Tarefa

Let’s see the difference between our fitted models by splitting the data another way:

  1. [Line #25] Initialize linear regression model.
  2. [Line #26] Get train-split variables using the function train_test_split() and x, y, test_size = 0.4, random_state = 1 as parameters.
  3. [Line #27] Fit the model inserting X_train and Y_train as parameters.
  4. [Lines #30-31] Print the intercept and slope we have received.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 2
toggle bottom row

bookFitting

Fitting the model means finding the most appropriate model based on training data. In our case, it is linear regression parameters - slope and intercept. Then we will use the appropriate model to make predictions and evaluate the predictions. As said before, for the training model you will use only the training subset and also the fit() function:

1
model.fit(X_train,Y_train)
copy

This method executes computations storing the result in the model object. So, our model has been built, and we have parameters for our straight line. Let’s take a look at the intercept and slope we have received.

12
print(model.intercept_) print(model.coef_)
copy

All model parameters in sklearn have trailing underscores.

Having these two parameters, we can build the line to predict future values on our dataset. Let’s do the same with the wine dataset to predict the number of flavonoids on total phenols.

1234567891011121314151617181920212223242526272829303132
# Import the libraries from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split import pandas as pd from sklearn.datasets import load_wine # 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['flavanoids'] = wine.target # Define the data we will work with x = data[['total_phenols']] y = data['flavanoids'] # Build and fit the model model = LinearRegression() X_train, X_test, Y_train, Y_test = train_test_split(x, y, test_size = 0.3,random_state = 1) model.fit(X_train, Y_train) # Print parameters print(model.intercept_) print(model.coef_)
copy

Tarefa

Let’s see the difference between our fitted models by splitting the data another way:

  1. [Line #25] Initialize linear regression model.
  2. [Line #26] Get train-split variables using the function train_test_split() and x, y, test_size = 0.4, random_state = 1 as parameters.
  3. [Line #27] Fit the model inserting X_train and Y_train as parameters.
  4. [Lines #30-31] Print the intercept and slope we have received.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 2
toggle bottom row

bookFitting

Fitting the model means finding the most appropriate model based on training data. In our case, it is linear regression parameters - slope and intercept. Then we will use the appropriate model to make predictions and evaluate the predictions. As said before, for the training model you will use only the training subset and also the fit() function:

1
model.fit(X_train,Y_train)
copy

This method executes computations storing the result in the model object. So, our model has been built, and we have parameters for our straight line. Let’s take a look at the intercept and slope we have received.

12
print(model.intercept_) print(model.coef_)
copy

All model parameters in sklearn have trailing underscores.

Having these two parameters, we can build the line to predict future values on our dataset. Let’s do the same with the wine dataset to predict the number of flavonoids on total phenols.

1234567891011121314151617181920212223242526272829303132
# Import the libraries from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split import pandas as pd from sklearn.datasets import load_wine # 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['flavanoids'] = wine.target # Define the data we will work with x = data[['total_phenols']] y = data['flavanoids'] # Build and fit the model model = LinearRegression() X_train, X_test, Y_train, Y_test = train_test_split(x, y, test_size = 0.3,random_state = 1) model.fit(X_train, Y_train) # Print parameters print(model.intercept_) print(model.coef_)
copy

Tarefa

Let’s see the difference between our fitted models by splitting the data another way:

  1. [Line #25] Initialize linear regression model.
  2. [Line #26] Get train-split variables using the function train_test_split() and x, y, test_size = 0.4, random_state = 1 as parameters.
  3. [Line #27] Fit the model inserting X_train and Y_train as parameters.
  4. [Lines #30-31] Print the intercept and slope we have received.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Fitting the model means finding the most appropriate model based on training data. In our case, it is linear regression parameters - slope and intercept. Then we will use the appropriate model to make predictions and evaluate the predictions. As said before, for the training model you will use only the training subset and also the fit() function:

1
model.fit(X_train,Y_train)
copy

This method executes computations storing the result in the model object. So, our model has been built, and we have parameters for our straight line. Let’s take a look at the intercept and slope we have received.

12
print(model.intercept_) print(model.coef_)
copy

All model parameters in sklearn have trailing underscores.

Having these two parameters, we can build the line to predict future values on our dataset. Let’s do the same with the wine dataset to predict the number of flavonoids on total phenols.

1234567891011121314151617181920212223242526272829303132
# Import the libraries from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split import pandas as pd from sklearn.datasets import load_wine # 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['flavanoids'] = wine.target # Define the data we will work with x = data[['total_phenols']] y = data['flavanoids'] # Build and fit the model model = LinearRegression() X_train, X_test, Y_train, Y_test = train_test_split(x, y, test_size = 0.3,random_state = 1) model.fit(X_train, Y_train) # Print parameters print(model.intercept_) print(model.coef_)
copy

Tarefa

Let’s see the difference between our fitted models by splitting the data another way:

  1. [Line #25] Initialize linear regression model.
  2. [Line #26] Get train-split variables using the function train_test_split() and x, y, test_size = 0.4, random_state = 1 as parameters.
  3. [Line #27] Fit the model inserting X_train and Y_train as parameters.
  4. [Lines #30-31] Print the intercept and slope we have received.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 3. Capítulo 2
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
some-alt