단순 선형 회귀 프로그램

이 기사에서는 단순 선형 회귀 프로그램의 한 가지 예를 보여줍니다.

[sc_fs_faq HTML = '참'헤드 라인 = 'H2'IMG = ''질문 = '단순 선형 회귀 프로그램'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

바꾸기 데이터 세트-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()

우리는 단순 선형 회귀 프로그램을 보여주었습니다.