ユーザ用ツール

サイト用ツール


linearregression

文書の過去の版を表示しています。


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.
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!")
linearregression.1720104534.txt.gz · 最終更新: 2024/07/04 14:48 by bokupi

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki