برنامج الانحدار الخطي البسيط

في هذه المقالة ، سوف نعرض مثالاً واحدًا لبرنامج الانحدار الخطي البسيط.

[sc_fs_faq html = 'true' headline = 'h2 ″ img =' 'question =' برنامج الانحدار الخطي البسيط 'img_alt =' 'css_class =' ​​'] استيراد مكتبات
استيراد مجموعة البيانات
وتقسيم مجموعة البيانات إلى مجموعة التدريب ومجموعة الاختبار
تدريب نموذج الانحدار الخطي البسيط في مجموعة التدريب
توقع نتيجة مجموعة الاختبار
تصور نتيجة مجموعة التدريب
تصور نتيجة مجموعة الاختبار
[/ sc_fs_faq]

الصيغة على النحو التالي:

y = b0 + b1 * x1

y = المتغير التابع (DV)
b0 = ثابت
b1 = المعامل
x1 = المتغير المستقل (IV)

هنا مثال واحد من البرنامج:

مكتبات الاستيراد

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

قم باستيراد مجموعة البيانات

dataset = pd.read_csv('dataset-file.csv')

x = dataset.iloc[:, :-1].values

x = dataset.iloc[:, -1].values

استبدل ملف dataset-file.csv بملفك واذكر المسار الكامل للملف.

تقسيم مجموعة البيانات إلى مجموعة التدريب ومجموعة الاختبار

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 = 0)

تدريب نموذج الانحدار الخطي البسيط على مجموعة التدريب

from sklearn.linear_model import LinearRegression

regressor = LinearRegression()

regressor.fit(x_train, y_train)

توقع نتيجة مجموعة الاختبار

y_pred = regressor.predict(x_test)

تصور نتائج مجموعة التدريب

plt.scatter(x_train, y_train, color = 'red')

plt.plot(x_train, regressor.predit(x_train), color = 'blue')

plt.title('Name of the graph')

plt.xlabel('Name of the x label')

plt.ylabel('name of the y label')

plt.show()

تصور نتائج مجموعة الاختبار

plt.scatter(x_test, y_test, color = 'red')

plt.plot(x_train, regressor.predit(x_train), color = 'blue')

plt.title('Name of the graph')

plt.xlabel('Name of the x label')

plt.ylabel('name of the y label')

plt.show()

لقد أظهرنا برنامج الانحدار الخطي البسيط.