_ | 覦覈襦 | 豕蠏手 | 殊螳 | 譯殊碁
FrontPage › Python-LinearRegression

Contents

1 OLS vs Ridge vs Lasso
2 一危
3 OLS(ordinary least squares)
4 Ridge
5 Lasso


1 OLS vs Ridge vs Lasso #

Linear Regression 蠍磯蓋 螻襴讀 OLS Ridge, Lasso螳 螳. 讌 cost function るゴ.
lm_compare.png

覘.. 蠏碁.

2 一危 #

#iris 一危一誤 襷り鍵
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
iris = load_iris()
iris.data
iris.feature_names
iris.target
iris.target_names


iris_df = pd.DataFrame(iris.data, columns=iris.feature_names)
iris_df["target"] = iris.target 
iris_df["target_names"] = iris.target_names[iris.target]
iris_df[:5]



#誤, ろ語誤 蠍
from sklearn.model_selection import train_test_split
train_set, test_set = train_test_split(iris_df, test_size = 0.3)

train_set.shape
test_set.shape


3 OLS(ordinary least squares) #

# 蠏(豕螻)
from sklearn.linear_model import LinearRegression as lm
model_ols = lm().fit(X=train_set.ix[:, [2]], y=train_set.ix[:, [3]])
print(model_ols.coef_)
print(model_ols.intercept_)


#plot
import matplotlib.pyplot as plt
plt.scatter(train_set.ix[:, [2]], train_set.ix[:, [3]],  color='black')
plt.plot(test_set.ix[:, [2]], model_ols.predict(test_set.ix[:, [2]]))

蟆郁骸
lm.png

4 Ridge #

#Ridge: alpha螳 譟一 螻朱/螻殊 狩.
from sklearn.linear_model import Ridge
model_ridge = Ridge(alpha=10).fit(X=train_set.ix[:, [2]], y=train_set.ix[:, [3]])

#
print(model_ridge.score(X=train_set.ix[:, [2]], y=train_set.ix[:, [3]]))
print(model_ridge.score(X=test_set.ix[:, [2]], y=test_set.ix[:, [3]]))

#plot
import matplotlib.pyplot as plt
plt.scatter(train_set.ix[:, [2]], train_set.ix[:, [3]],  color='black')
plt.plot(test_set.ix[:, [2]], model_ridge.predict(test_set.ix[:, [2]]))

5 Lasso #


#Lasso: alpha螳 譟一 螻朱/螻殊 狩.
from sklearn.linear_model import Lasso
model_lasso = Lasso(alpha=0.1, max_iter=1000).fit(X=train_set.ix[:, [0,1,2]], y=train_set.ix[:, [3]])

#
print(model_lasso.score(X=train_set.ix[:, [0,1,2]], y=train_set.ix[:, [3]]))
print(model_lasso.score(X=test_set.ix[:, [0,1,2]], y=test_set.ix[:, [3]]))

# 轟煙
print(np.sum(model_lasso.coef_ != 0))




蠍 蠍郁鍵..
企: : るジ讓曙 襦螻豺 企Ν 譯殊語. 襦螻豺
EditText : Print : Mobile : FindPage : DeletePage : LikePages : Powered by MoniWiki : Last modified 2018-04-13 23:12:52

覓企 覓殊企 讌碁ゼ 磯 襯企 豺讌 螻 讌襯 螳豢 螳螻 讌企伎 襦 螳 襯 螳豢 螳朱 企 蟾 螻葛 襾碁蟇磯Μ讌 螻 れ願 蠍磯ゼ 螳讌 螳螻 レ覓殊 襷 螳覃 螻 襷讌 豌覈 螳朱 螻褐 蟇一 覃襴 襯伎襷 覓殊企襴 覯 朱 螳讌 螳. 豌讌 願朱 企伎螻 襷覓殊 願朱 伎螳覃 朱 願朱 至 襷 願朱 伎覃 襷覓殊 願朱 覦襯願 蟆企. 願 讌襦 螳 覓殊 譬 伎企. ( <<語>>)