**Step 1: Import Libraries**

```
import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestRegressor
```

**Step 2: Prepare Your Data**

Ensure your dataset contains independent features (X) and the corresponding target variable (y). Make sure your data is in a NumPy array or a DataFrame.

**Step 3: Create the Random Forest Regressor**

`regressor = RandomForestRegressor(n_estimators=100, random_state=0) # You can adjust hyperparameters like n_estimators, max_depth, etc.`

`n_estimators`

: The number of decision trees in the random forest.`max_depth`

: The maximum depth of each decision tree (optional).

**Step 4: Train the Random Forest Regressor**

`regressor.fit(X, y)`

**Step 5: Make Predictions**

`y_pred = regressor.predict(X)`

**Step 6: Visualize the Results (Optional)**

You can visualize the actual values and predicted values to assess how well the Random Forest model performs.

```
plt.scatter(X, y, color='red', label='Actual')
plt.scatter(X, y_pred, color='blue', label='Predicted')
plt.title('Random Forest Regression')
plt.xlabel('X-axis')
plt.ylabel('y-axis')
plt.legend()
plt.show()
```

**Step 7: Evaluate the Model**

Evaluate the model’s performance using appropriate metrics such as Mean Absolute Error (MAE), Mean Squared Error (MSE), and R-squared (R²).

```
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
mae = mean_absolute_error(y, y_pred)
mse = mean_squared_error(y, y_pred)
r2 = r2_score(y, y_pred)
print(f'Mean Absolute Error: {mae}')
print(f'Mean Squared Error: {mse}')
print(f'R-squared: {r2}')
```

In practice, you should split your dataset into training and testing subsets to assess the model’s generalization performance. You can use Scikit-Learn’s `train_test_split`

function for this purpose. Additionally, hyperparameter tuning and cross-validation can help optimize the Random Forest model’s performance.

**Step 1: Import Libraries**

```
import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeRegressor
```

**Step 2: Prepare Your Data**

Prepare your dataset with independent features (X) and the corresponding target variable (y). Ensure your data is in a NumPy array or a DataFrame.

**Step 3: Create the Decision Tree Regressor**

`regressor = DecisionTreeRegressor(random_state=0) # You can adjust hyperparameters like max_depth, min_samples_split, etc.`

**Step 4: Train the Decision Tree Regressor**

`regressor.fit(X, y)`

**Step 5: Make Predictions**

`y_pred = regressor.predict(X)`

**Step 6: Visualize the Results (Optional)**

You can visualize the actual values and predicted values to assess how well the Decision Tree model performs.

```
plt.scatter(X, y, color='red', label='Actual')
plt.plot(X, y_pred, color='blue', label='Predicted')
plt.title('Decision Tree Regression')
plt.xlabel('X-axis')
plt.ylabel('y-axis')
plt.legend()
plt.show()
```

**Step 7: Evaluate the Model**

It’s essential to evaluate the model’s performance using appropriate metrics. For regression, common metrics include Mean Absolute Error (MAE), Mean Squared Error (MSE), and R-squared (R²). You can use Scikit-Learn’s functions to calculate these metrics.

```
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
mae = mean_absolute_error(y, y_pred)
mse = mean_squared_error(y, y_pred)
r2 = r2_score(y, y_pred)
print(f'Mean Absolute Error: {mae}')
print(f'Mean Squared Error: {mse}')
print(f'R-squared: {r2}')
```

Keep in mind that in practice, you should split your dataset into training and testing subsets to assess the model’s generalization performance. You can use Scikit-Learn’s `train_test_split`

function for this purpose. Additionally, hyperparameter tuning and cross-validation can help optimize the Decision Tree model’s performance.

**Step 1: Import Libraries**

```
import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler
```

**Step 2: Prepare Your Data**

You should have your dataset ready, with independent features (X) and the corresponding target variable (y). Ensure that the data is in a NumPy array or a DataFrame.

**Step 3: Feature Scaling**

SVR is sensitive to the scale of input features, so it’s essential to perform feature scaling. Use the `StandardScaler`

from Scikit-Learn to standardize your data.

```
sc_X = StandardScaler()
sc_y = StandardScaler()
X = sc_X.fit_transform(X)
y = sc_y.fit_transform(y.reshape(-1, 1)).ravel()
```

**Step 4: Create the SVR Model**

`svr = SVR(kernel='rbf') # You can choose different kernels like 'linear', 'poly', or 'sigmoid'`

**Step 5: Train the SVR Model**

`svr.fit(X, y)`

**Step 6: Make Predictions**

`y_pred = svr.predict(X)`

**Step 7: Visualize the Results (Optional)**

You can plot the actual values and the predicted values to visualize how well the SVR model performs.

```
plt.scatter(X, y, color='red', label='Actual')
plt.plot(X, y_pred, color='blue', label='Predicted')
plt.title('SVR Prediction')
plt.xlabel('X-axis')
plt.ylabel('y-axis')
plt.legend()
plt.show()
```

Remember that this is a basic example of using SVR in Python. In practice, you may need to tune hyperparameters, perform cross-validation, and evaluate the model’s performance using metrics like Mean Squared Error (MSE) or R-squared (R²).

Also, it’s crucial to split your dataset into training and testing subsets to assess the model’s generalization performance. You can use Scikit-Learn’s `train_test_split`

function for this purpose.

**Import Necessary Libraries**:

```
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.model_selection import train_test_split
```

**Load and Prepare Data**: Load your dataset and organize it into the independent variable (feature) and the dependent variable (target).

```
# Example data
data = pd.read_csv('your_dataset.csv')
# Separate the feature (independent variable) and the target (dependent variable)
X = data['Feature'] # Independent variable (feature)
y = data['Target'] # Dependent variable (target)
```

**Split Data**: Split your dataset into a training set and a test set to evaluate the model’s performance on unseen data.

` X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)`

**Create Polynomial Features**: Use scikit-learn’s`PolynomialFeatures`

to create polynomial features from your original feature(s). You specify the degree of the polynomial.

```
degree = 2 # Choose the degree of the polynomial (e.g., 2 for quadratic)
poly_features = PolynomialFeatures(degree=degree)
X_train_poly = poly_features.fit_transform(X_train.values.reshape(-1, 1))
X_test_poly = poly_features.transform(X_test.values.reshape(-1, 1))
```

This step transforms your original feature(s) into a set of features including the original feature(s) and their polynomial combinations.

**Create and Fit the Model**: Create a LinearRegression model and fit it to your training data with the polynomial features.

```
# Create a linear regression model
model = LinearRegression()
# Fit the model to the training data with polynomial features
model.fit(X_train_poly, y_train)
```

**Predictions**: Once the model is trained, you can use it to make predictions on the test data with polynomial features.

` y_pred = model.predict(X_test_poly)`

**Evaluate the Model**: You can evaluate the model’s performance using various metrics, such as Mean Squared Error (MSE), R-squared (R^2), or others, depending on your specific goals.

```
from sklearn.metrics import mean_squared_error, r2_score
mse = mean_squared_error(y_test, y_pred)
r_squared = r2_score(y_test, y_pred)
print(f"Mean Squared Error: {mse}")
print(f"R-squared: {r_squared}")
```

This example demonstrates how to perform polynomial linear regression using scikit-learn in Python. By introducing polynomial features, you can model more complex relationships between the independent and dependent variables. You can adjust the `degree`

parameter to control the complexity of the polynomial model.

**Import Necessary Libraries**:

```
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
```

**Load and Prepare Data**: Load your dataset and organize it into independent variables (features) and a dependent variable (target).

```
# Example data
data = pd.read_csv('your_dataset.csv')
# Separate features and target variable
X = data[['Feature1', 'Feature2', 'Feature3']] # Independent variables (features)
y = data['Target'] # Dependent variable (target)
```

**Split Data**: Split your dataset into a training set and a test set. This helps evaluate the model’s performance on unseen data.

` X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)`

**Create and Fit the Model**: Create a LinearRegression model and fit it to your training data.

```
# Create a linear regression model
model = LinearRegression()
# Fit the model to the training data
model.fit(X_train, y_train)
```

**Predictions**: Once the model is trained, you can use it to make predictions on the test data.

` y_pred = model.predict(X_test)`

**Evaluate the Model**: You can evaluate the model’s performance using various metrics, such as Mean Squared Error (MSE), R-squared (R^2), or others, depending on your specific goals.

```
from sklearn.metrics import mean_squared_error, r2_score
mse = mean_squared_error(y_test, y_pred)
r_squared = r2_score(y_test, y_pred)
print(f"Mean Squared Error: {mse}")
print(f"R-squared: {r_squared}")
```

**Interpret the Coefficients**: The coefficients of the linear regression model represent the relationship between each independent variable and the dependent variable.

```
coefficients = model.coef_
intercept = model.intercept_
print("Coefficients:", coefficients)
print("Intercept:", intercept)
```

This is a basic example of how to perform multiple linear regression using scikit-learn in Python. You can extend this approach to handle more complex datasets and explore various aspects of regression analysis, such as feature selection, regularization, and model diagnostics, to build and evaluate better predictive models for your specific use case.

]]>**Linear Regression is a simple machine learning model for regression problems, i.e., when the target variable is a real value.**

Example: Let’s start with an example — suppose we have a dataset with information about the **area of a house** (in square feet) and its **price** and our task is to build a machine learning model which can **predict the price** given the area.

**Linear Regression** is a linear model, e.g. a model that assumes a linear relationship between the input variables (x) and the single output variable (y). More specifically, that y can be calculated from a linear combination of the input variables (x).

- When there is a
**single input variable (x)**, the method is referred to as`simple linear regression.`

- When there are
**multiple input variables**, the method is known as`multiple linear regression.`

**X** is the Independent Variable

**Y** is the Dependent Variable

Simple linear regression is a statistical method used to model the relationship between a single independent variable (predictor) and a dependent variable (response) by fitting a linear equation to the observed data. In Python, you can perform simple linear regression using the scikit-learn library. Here’s a step-by-step guide:

**Import Necessary Libraries**:

```
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
```

**Load and Prepare Data**: Load your dataset and organize it into the independent variable (feature) and the dependent variable (target).

```
# Example data
data = pd.read_csv('your_dataset.csv')
# Separate the feature (independent variable) and the target (dependent variable)
X = data['Feature'] # Independent variable (feature)
y = data['Target'] # Dependent variable (target)
```

**Split Data**: Split your dataset into a training set and a test set to evaluate the model’s performance on unseen data.

` X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)`

**Create and Fit the Model**: Create a LinearRegression model and fit it to your training data.

```
# Create a linear regression model
model = LinearRegression()
# Fit the model to the training data
model.fit(X_train.values.reshape(-1, 1), y_train)
```

Note that `values.reshape(-1, 1)`

is used to reshape the feature data since scikit-learn expects the input to be a 2D array, and `X_train`

is a 1D array.

**Predictions**: Once the model is trained, you can use it to make predictions on the test data.

` y_pred = model.predict(X_test.values.reshape(-1, 1))`

**Evaluate the Model**: You can evaluate the model’s performance using various metrics, such as Mean Squared Error (MSE), R-squared (R^2), or others, depending on your specific goals.

```
from sklearn.metrics import mean_squared_error, r2_score
mse = mean_squared_error(y_test, y_pred)
r_squared = r2_score(y_test, y_pred)
print(f"Mean Squared Error: {mse}")
print(f"R-squared: {r_squared}")
```

**Interpret the Coefficients**: In simple linear regression, you can interpret the coefficients of the linear regression model to understand the relationship between the independent variable and the dependent variable.

```
coefficient = model.coef_[0]
intercept = model.intercept_
print("Coefficient:", coefficient)
print("Intercept:", intercept)
```

This example demonstrates how to perform simple linear regression using scikit-learn in Python. You can extend this approach to handle more complex datasets and explore various aspects of regression analysis, such as feature selection, regularization, and model diagnostics, for your specific use case.

]]>