Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?
@pi-to

# 初めてのLinearRegressionモデル作成

More than 1 year has passed since last update.

モデル作成から数値予測までの過程の概要をざっとつかみたい！

# 回帰分析とは？

その際の方程式が必ずしも直線にはならないので、回帰分析と呼ぶ！(直線の場合は、線形回帰)

# ピザの直径25cmの時、ピザの値段は？

``````x=[[12],[16],[20],[28],[36]] #ピザの直径
y = [[700],[900],[1300],[1750],[1800]] #ピザの値段
``````
``````from sklearn.linear_model import LinearRegression
model = LinearRegression()#1.モデルの指定
model.fit(x,y)#2.学習
``````
``````import numpy as np
#n×1ベクトルに変換する必要があるらしい...
#その時、reshape()の中身を-1とすると、自動的にnumpyが判断してくれるらしい（便利だ）
price = model.predict(np.array([25]).reshape(-1, 1)) # 3.予測（25㎝を代入）
print('25 cm pizza should cost: \$%s'%price[0][0])
``````

``````25 cm pizza should cost: \$1416.9181034482758
``````

# この出力結果の精度がどれだけ正確か知りたい！

``````# テストデータを作成
x_test = [[16],[18],[22],[32],[24]]
y_test = [[1100],[850],[1500],[1800],[1100]]

score = model.score(x_test, y_test)
print("r-squared:",score)
``````

``````r-squared: 0.6620052929422553
``````

# 結果

モデルの精度は0.662と出たが、初めてにしては満足のいく結果となった。

1
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away