59
44

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LinearRegression クラスについてメモ

Posted at

sklearn の LinearRegression クラスについての個人メモ。

LinearRegression とは

線形回帰モデルの一つ。説明変数の値から目的変数の値を予測する。

導入

import sklearn.linear_model.LinearRegression

アトリビュート

coef_

回帰変数。

intercept_

切片。

メソッド

fit(x, y)

線形回帰モデルの当てはめを実行。訓練の開始。
xが対象データで、yが正解データ ※教師あり学習が前提

get_params()

推定に用いたパラメータを取得。

predict(x)

モデルを使用して、xに対して予測を実行し予測値を算出する。
###score(x, y)
決定係数を出力。予測値xと正解値yの相関を測る。

実践

training
import pandas as pd 
from sklearn.linear_model import LinearRegression
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

dataset = load_breast_cancer()

data_x = pd.DataFrame(dataset.data,columns=dataset.feature_names)
data_y = pd.DataFrame(dataset.target, columns = ['y'])

train_x,test_x,train_y,test_y = train_test_split(data_x,data_y)

model = LinearRegression()  #線形回帰モデルの呼び出し
model.fit(train_x, train_y)  #モデルの訓練


print(model.coef_)  #回帰変数の表示
print(model.intercept_)  #回帰直線の切片
print(model.get_params())  #パラメータの取得

print(model.predict(test_x))  #予測値の表示
print(model.score(test_x,test_y))  #決定係数の表示

以上。

59
44
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
59
44

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?