[英]What features to use for regression or classification?
有沒有辦法確定與我的機器學習模型最相關的功能。 如果我有20個功能,是否有一個功能可以決定我應該使用哪些功能(或可以自動刪除不相關的功能的功能)? 我計划對回歸模型或分類模型進行此操作。
我想要的輸出是最相關的值列表和預測
import pandas as pd
from sklearn.linear_model import LinearRegression
dic = {'par_1': [10, 30, 11, 19, 28, 33, 23],
'par_2': [1, 3, 1, 2, 3, 3, 2],
'par_3': [15, 3, 16, 65, 24, 56, 13],
'outcome': [101, 905, 182, 268, 646, 624, 465]}
df = pd.DataFrame(dic)
variables = df.iloc[:,:-1]
results = df.iloc[:,-1]
print(variables.shape)
print(results.shape)
reg = LinearRegression()
reg.fit(variables, results)
x = reg.predict([[18, 2, 21]])[0]
print(x)
您可以訪問reg
對象的coef_屬性:
print(reg.coef_)
稱這些權重為過於簡單,因為它們在線性回歸中具有特定含義。 但是他們就是你所擁有的。
使用線性模型時,重要的是使用線性獨立的特征。 您可以使用df.corr()
可視化相關性:
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.decomposition import PCA
from sklearn.metrics import mean_squared_error
numpy.random.seed(2)
dic = {'par_1': [10, 30, 11, 19, 28, 33, 23],
'par_2': [1, 3, 1, 2, 3, 3, 2],
'par_3': [15, 3, 16, 65, 24, 56, 13],
'outcome': [101, 905, 182, 268, 646, 624, 465]}
df = pd.DataFrame(dic)
print(df.corr())
out:
par_1 par_2 par_3 outcome
par_1 1.000000 0.977935 0.191422 0.913878
par_2 0.977935 1.000000 0.193213 0.919307
par_3 0.191422 0.193213 1.000000 -0.158170
outcome 0.913878 0.919307 -0.158170 1.000000
您可以看到par_1
和par_2
是高度相關的。 如@taga所述,您可以使用PCA
將要素映射到線性獨立的較低維度空間:
variables = df.iloc[:,:-1]
results = df.iloc[:,-1]
pca = PCA(n_components=2)
pca_all = pca.fit_transform(variables)
print(np.corrcoef(pca_all[:, 0], pca_all[:, 1]))
out:
[[1.00000000e+00 1.87242048e-16]
[1.87242048e-16 1.00000000e+00]]
請記住要根據樣本數據驗證模型:
X_train = variables[:4]
y_train = results[:4]
X_valid = variables[4:]
y_valid = results[4:]
pca = PCA(n_components=2)
pca.fit(X_train)
pca_train = pca.transform(X_train)
pca_valid = pca.transform(X_valid)
print(pca_train)
reg = LinearRegression()
reg.fit(pca_train, y_train)
yhat_train = reg.predict(pca_train)
yhat_valid = reg.predict(pca_valid)
print(mean_squared_error(yhat_train, y_train))
print(mean_squared_error(yhat_valid, y_valid))
功能選擇並非易事:有許多sklearn模塊可以實現它(請參閱docs ),您應該始終嘗試至少使用其中的幾個,看看哪些可以提高樣本外數據的性能。
好了,最初我遇到了同樣的問題,我發現選擇相關功能有用的兩種方法是這些。
1,您可以通過使用模型的特征重要性屬性來獲取數據集中每個特征的特征重要性.Feature重要度是基於樹的分類器隨附的內置類。
import pandas as pd
import numpy as np
data = pd.read_csv("D://Blogs//train.csv")
X = data.iloc[:,0:20] #independent columns
y = data.iloc[:,-1] #target column i.e price range
from sklearn.ensemble import ExtraTreesClassifier
import matplotlib.pyplot as plt
model = ExtraTreesClassifier()
model.fit(X,y)
print(model.feature_importances_) #use inbuilt class feature_importances of tree based classifiers
#plot graph of feature importances for better visualization
feat_importances = pd.Series(model.feature_importances_, index=X.columns)
feat_importances.nlargest(10).plot(kind='barh')
plt.show()
2.帶有熱圖的相關矩陣
關聯說明要素如何相互關聯或與目標變量關聯。 它給出了特征如何與目標變量關聯的直覺。
這不是我的研究,而是此博客功能的選擇 ,這有助於消除我的疑問,我敢肯定,您也可以這樣做。:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.