Linear Regression Prediction with Scikit Learn – التنبؤ الخطى باستخدام السايكت ليرن

Posted by


Linear regression is a widely used method in machine learning for predicting a continuous variable based on one or more input variables. In this tutorial, we will learn how to perform linear regression using the scikit-learn library in Python.

Scikit-learn is a powerful machine learning library in Python that provides implementations of various machine learning algorithms, including linear regression. Scikit-learn is easy to use, efficient, and well-documented, making it a popular choice for machine learning practitioners.

In this tutorial, we will cover the following topics:

  1. Installing scikit-learn
  2. Loading and exploring the dataset
  3. Preprocessing the data
  4. Splitting the data into training and testing sets
  5. Building and training a linear regression model
  6. Evaluating the model
  7. Making predictions using the model

Let’s get started!

  1. Installing scikit-learn

If you haven’t already installed scikit-learn, you can do so using pip, the Python package manager. Simply run the following command in your terminal:

pip install scikit-learn
  1. Loading and exploring the dataset

For this tutorial, we will use the Boston housing dataset, which is included in scikit-learn. This dataset contains information about housing prices in Boston, as well as various features such as crime rate, number of rooms, and accessibility to highways.

To load the dataset, you can use the following code:

from sklearn.datasets import load_boston
boston = load_boston()
X = boston.data
y = boston.target

You can explore the dataset by printing the feature names and target variable:

print(boston.feature_names)
print(y)
  1. Preprocessing the data

Before building a linear regression model, it is important to preprocess the data. This includes steps such as normalizing the features and handling missing values.

For this tutorial, we will skip the preprocessing step for simplicity. However, in a real-world scenario, it is important to preprocess the data before building a model.

  1. Splitting the data into training and testing sets

To evaluate the performance of the linear regression model, we need to split the data into training and testing sets. The training set will be used to train the model, while the testing set will be used to evaluate its performance.

You can split the data using the train_test_split function from scikit-learn:

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

In this code snippet, we have split the data into training and testing sets, with 20% of the data used for testing.

  1. Building and training a linear regression model

Now that we have preprocessed the data and split it into training and testing sets, we can build and train a linear regression model using scikit-learn.

from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X_train, y_train)

In this code snippet, we have created an instance of the LinearRegression class and trained the model on the training data.

  1. Evaluating the model

After training the model, we can evaluate its performance on the testing set using metrics such as mean squared error or R-squared.

from sklearn.metrics import mean_squared_error
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print('Mean Squared Error:', mse)

In this code snippet, we have calculated the mean squared error between the predicted and actual target values on the testing set.

  1. Making predictions using the model

Finally, we can make predictions using the trained model on new data points. For example, let’s say we have a new data point with the following features:

new_data = [[0.02, 18.0, 2.31, 0, 0.537, 6.575, 65.2, 4.0900, 1, 296.0, 15.3, 396.90, 4.98]]
prediction = model.predict(new_data)
print('Predicted housing price:', prediction)

In this code snippet, we have made a prediction for a new data point and printed the predicted housing price.

That’s it! You have successfully built and trained a linear regression model using scikit-learn in Python. Linear regression is a simple yet powerful technique for predicting continuous variables and is widely used in various fields such as finance, economics, and healthcare. Experiment with different datasets and parameters to further improve your understanding of linear regression and machine learning in general. Happy coding!

0 0 votes
Article Rating

Leave a Reply

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@user-vf7ud4tb2h
14 days ago

جزاك الله خيرا

@basharshammala8120
14 days ago

هان شو القصد من ال scoe بالتحديد ؟؟؟؟!
وشكرا كثيرا عالشرح الاكثر من ممتاز

2
0
Would love your thoughts, please comment.x
()
x