0

## python 如何輸出每個observation的標準差

``````import pandas as pd
import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt
from sklearn import preprocessing, linear_model

# 自變量
X = df[['w1','w2','w3','w4']]
# 因變量
y = df['z'].values

res = sm.OLS(y, X2)
res = res.fit()
print(res.summary())

#另一種函式庫的線性回歸(不知道哪個比較方便取得標準差)
regr=linear_model.LinearRegression()
regr.fit(X, y)
``````

company name w1 w2 w3 w4
data0 99 13 1 26
data1 82 3 2 13
data2 80 42 8 91
data3 10 50 15 34
data4 20 12 71 17

import numpy as no
df[“stds”] = df.apply(lambda x: np.std(x.values), axis=1)

### 1 個回答

1
I code so I am
iT邦研究生 4 級 ‧ 2020-03-29 21:56:36

``````from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

lin_model = LinearRegression()
lin_model.fit(X_train, Y_train)

y_test_predict = lin_model.predict(X_test)
rmse = (np.sqrt(mean_squared_error(Y_test, y_test_predict)))
r2 = r2_score(Y_test, y_test_predict)

# 觀察值的預測誤差
y_test_predict - Y_test

``````