Зміст курсу
Explore the Linear Regression Using Python
Explore the Linear Regression Using Python
Fitting
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:
model.fit(X_train,Y_train)
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.
print(model.intercept_) print(model.coef_)
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.
# 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_)
Завдання
Let’s see the difference between our fitted models by splitting the data another way:
- [Line #25] Initialize linear regression model.
- [Line #26] Get train-split variables using the function
train_test_split()
andx
,y
,test_size = 0.4
,random_state = 1
as parameters. - [Line #27] Fit the model inserting
X_train
andY_train
as parameters. - [Lines #30-31] Print the intercept and slope we have received.
Дякуємо за ваш відгук!
Fitting
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:
model.fit(X_train,Y_train)
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.
print(model.intercept_) print(model.coef_)
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.
# 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_)
Завдання
Let’s see the difference between our fitted models by splitting the data another way:
- [Line #25] Initialize linear regression model.
- [Line #26] Get train-split variables using the function
train_test_split()
andx
,y
,test_size = 0.4
,random_state = 1
as parameters. - [Line #27] Fit the model inserting
X_train
andY_train
as parameters. - [Lines #30-31] Print the intercept and slope we have received.
Дякуємо за ваш відгук!
Fitting
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:
model.fit(X_train,Y_train)
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.
print(model.intercept_) print(model.coef_)
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.
# 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_)
Завдання
Let’s see the difference between our fitted models by splitting the data another way:
- [Line #25] Initialize linear regression model.
- [Line #26] Get train-split variables using the function
train_test_split()
andx
,y
,test_size = 0.4
,random_state = 1
as parameters. - [Line #27] Fit the model inserting
X_train
andY_train
as parameters. - [Lines #30-31] Print the intercept and slope we have received.
Дякуємо за ваш відгук!
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:
model.fit(X_train,Y_train)
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.
print(model.intercept_) print(model.coef_)
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.
# 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_)
Завдання
Let’s see the difference between our fitted models by splitting the data another way:
- [Line #25] Initialize linear regression model.
- [Line #26] Get train-split variables using the function
train_test_split()
andx
,y
,test_size = 0.4
,random_state = 1
as parameters. - [Line #27] Fit the model inserting
X_train
andY_train
as parameters. - [Lines #30-31] Print the intercept and slope we have received.