資源簡介
機器學習(4)-多元線性回歸:數據集與源碼下載。博客當中用到的源碼與數據集

代碼片段和文件信息
import?numpy?as?np
import?pandas?as?pd
from?sklearn.preprocessing?import?LabelEncoder?OneHotEncoder
from?sklearn.model_selection?import?train_test_split
import?statsmodels.formula.api?as?sm
from?sklearn.linear_model?import?LinearRegression
dataset?=?pd.read_csv(‘COM.csv‘)
X?=?dataset.iloc[:?:-1].values
y?=?dataset.iloc[:?4].values
print(“自變量“)
print(X)
print(“...“)
print(“因變量“)
print(y)
print(“...“)
labelencoder_X?=?LabelEncoder()
X[:?3]?=?labelencoder_X.fit_transform(X[:?3])
onehotencoder?=?OneHotEncoder(categorical_features?=?[3])
X?=?onehotencoder.fit_transform(X).toarray()
float_formatter=?lambda?X:“%.2f“?%?X
np.set_printoptions(formatter={‘float_kind‘:float_formatter})
print(“轉換后的X“)
print(X)
print(“...“)
X?=?X[:?1:]??#刪除某一列???就是刪除了第0列
print(X)
X_train?X_test?y_train?y_test?=?train_test_split(X?y?test_size?=?0.2?random_state?=?0)
print(“/X_train/X_test/y_train/y_test“)
regressor?=?LinearRegression()
regressor.fit(X_train?y_train)
y_pred?=?regressor.predict(X_test)
print(“包含了的預測結果的向量“)
print(y_pred)
X_train?=?np.append(arr?=?np.ones((40?1)).astype(int)?values?=?X_train?axis?=?1)
Ximo?=?X_train
regressor_OLS?=?sm.OLS(endog?=?y_train?exog?=?Ximo).fit()
print(“第一次的summary“)
print(regressor_OLS.summary())
Ximo?=?X_train?[:?[0?1?3?4?5]]
regressor_OLS?=?sm.OLS(endog?=?y_train?exog?=?Ximo).fit()
print(“第二次的summary“)
print(regressor_OLS.summary())
Ximo?=?X_train?[:?[0?3?4?5]]
regressor_OLS?=?sm.OLS(endog?=?y_train?exog?=?Ximo).fit()
print(“第三次的summary“)
print(regressor_OLS.summary())
Ximo?=?X_train?[:?[0?3?5]]
regressor_OLS?=?sm.OLS(endog?=?y_train?exog?=?Ximo).fit()
print(“第四次的summary“)
print(regressor_OLS.summary())
Ximo?=?X_train?[:?[0?3]]
regressor_OLS?=?sm.OLS(endog?=?y_train?exog?=?Ximo).fit()
print(“第五次的summary“)
print(regressor_OLS.summary())
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????2408??2018-04-15?11:12??3\COM.csv
?????文件???????1951??2018-04-15?11:05??3\mlr.py
?????目錄??????????0??2018-04-15?11:17??3
-----------?---------??----------?-----??----
?????????????????4359????????????????????3
- 上一篇:多元Logistic 回歸分析
- 下一篇:PL/O語言編譯器
評論
共有 條評論