====== 線形回帰モデル ======
===== コードテンプレート =====
Kaggleのtitanic competitionで使った、Scikit learnによる線形回帰モデルによる機械学習コードのテンプレート。
もう少し直す予定。
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All"
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
train_df = pd.read_csv('/kaggle/input/titanic/train.csv', encoding='SHIFT-JIS')
# Fill NaN value with front value.
train_df = train_df.ffill()
train_X = train_df[["Fare"]].values
train_Y = train_df[["Survived"]].values
model = LinearRegression(
fit_intercept=True,
copy_X=True,
n_jobs=1,
positive=False,
)
model.fit(train_X, train_Y)
test_df = pd.read_csv('/kaggle/input/titanic/test.csv', encoding='SHIFT-JIS')
#test_df = test_df.dropna(subset="Fare")
test_df = test_df.ffill()
test_X = test_df[["Fare"]].values
test_passengerId = test_df[["PassengerId"]].values
Y_pred = model.predict(test_X)
def my_round(x, decimals=0):
return np.floor(x * 10**decimals + 0.5) / 10**decimals
Y_pred = my_round(Y_pred)
# Output result to csv.
# Kaggleで結果を評価させるために、フォーマットを合わせている
output = pd.DataFrame({'PassengerId': np.ravel(test_passengerId), 'Survived': np.array(np.ravel(Y_pred), dtype=int)})
output.to_csv('/kaggle/working/submission.csv', index=False)
print("Finished!")
===== 評価方法 =====
from sklearn.metrics import r2_score
# 線形回帰モデル作成(略)
# test.csvは正解が分からないので、train.csvのデータで評価する
Y_pred = model.predict(train_X)
# 決定係数による評価
r2_score_val = r2_score(train_Y, Y_pred)
print("R2 score = ", r2_score_val)
titanicのtrain.csvのデータを対象に、単回帰分析で予測した結果の評価結果。
かなり厳しい。
^説明変数^決定係数^
|PClass|0.11456941170524182 |
|Age |0.004299594973638632 |
|Fare |0.06620664646184304 |
|SibSp |0.001247678927532414 |