単純な線形回帰プログラム

この記事では、単純な線形回帰プログラムの一例を示します。

[sc_fs_faq html = 'true' headline = 'h2″ img =' 'question =' Simple Linear Regression Program '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()

単純な線形回帰プログラムを示しました。