我想知道sklearn logisticRegression
的densify()
和sparsify()
方法返回什么?
我原以为这些方法会打印一个包含coef_信息的矩阵,而不是像下面这样的输出。
只是好奇如何打印出文档中提到的密集coef_或稀疏coef_矩阵。
from sklearn.model_selection import train_test_splitfrom sklearn.linear_model import LogisticRegressiondf = pd.read_csv('https://raw.githubusercontent.com/justmarkham/pandas-videos/master/data/titanic_train.csv')df = df.dropna(how='any', subset = ['Age','Fare','Sex'])df['Sex'] = df['Sex'].map({'male':0, 'female':1})X = df[['Age','Fare','Sex']]y = df['Survived']X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 1)logreg = LogisticRegression()logreg.fit(X_train, y_train)y_pred = logreg.predict(X_test)logreg.densify()#Output<bound method SparseCoefMixin.densify of LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, l1_ratio=None, max_iter=100, multi_class='auto', n_jobs=None, penalty='l2', random_state=None, solver='lbfgs', tol=0.0001, verbose=0, warm_start=False)>logreg.sparsify()#OutputLogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, l1_ratio=None, max_iter=100, multi_class='auto', n_jobs=None, penalty='l2', random_state=None, solver='lbfgs', tol=0.0001, verbose=0, warm_start=False)
回答:
仔细查看文档,我们可以看到:
densify(self):
将系数矩阵转换为密集数组格式。
将
coef_
成员(重新)转换为numpy.ndarray。这是coef_
的默认格式sparsify(self):
将系数矩阵转换为稀疏数组格式。
将
coef_
成员转换为scipy.sparse矩阵
因此,这两个方法的效果是针对返回的系数矩阵coef_
(在密集和稀疏格式之间转换),并且在调用时它们在模型摘要中显示的输出中确实没有明显体现。
这里是使用鸢尾花数据的一个简单演示:
from sklearn.datasets import load_irisfrom sklearn.linear_model import LogisticRegressionX, y = load_iris(return_X_y=True)clf = LogisticRegression(random_state=0)clf.fit(X, y)# 默认情况下,系数是密集格式(numpy.ndarray):clf.coef_# array([[-0.41878528, 0.96703041, -2.5209973 , -1.08417682],# [ 0.53124457, -0.31475282, -0.20008433, -0.94861142],# [-0.1124593 , -0.65227759, 2.72108162, 2.03278825]])type(clf.coef_)# numpy.ndarray# 切换到稀疏格式:clf.sparsify()clf.coef_# <3x4 sparse matrix of type '<class 'numpy.float64'>'# with 12 stored elements in Compressed Sparse Row format>type(clf.coef_)# scipy.sparse.csr.csr_matrix# 切换回密集格式:clf.densify()type(clf.coef_)# numpy.ndarray