【问题标题】:What is the output of densify() and sparsify() methods of sklearn LogisticRegressionsklearn LogisticRegression 的 densify() 和 sparse() 方法的输出是什么
【发布时间】:2020-08-23 13:35:46
【问题描述】:

我想知道 sklearn logisticRegression densify()sparsify() 方法返回什么?

我认为这些方法会打印一个包含 coef_ 信息的矩阵,而不是如下所示的输出。

只是好奇如何打印出文档中提到的密集 coef_ 或稀疏 coef_ 矩阵。

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

df = 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()
#Output
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)

【问题讨论】:

    标签: python machine-learning scikit-learn logistic-regression


    【解决方案1】:

    仔细观察documentation,我们看到:

    致密化自我):

    将系数矩阵转换为密集数组格式。

    coef_ 成员(返回)转换为 numpy.ndarray。这是coef_的默认格式

    sparsify(self):

    将系数矩阵转换为稀疏数组格式。

    coef_ 成员转换为 scipy.sparse 矩阵

    因此,这两种方法的效果都在返回的系数矩阵coef_ 上(在密集和稀疏格式之间转换),并且在调用时它们都显示为输出的模型摘要中确实不明显。

    这是一个使用虹膜数据的简单演示:

    from sklearn.datasets import load_iris
    from sklearn.linear_model import LogisticRegression
    
    X, y = load_iris(return_X_y=True)
    clf = LogisticRegression(random_state=0)
    clf.fit(X, y)
    
    # by default, the coefficients are in dense format (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
    
    # switch to sparse format:
    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
    
    # switch back to dense format:
    clf.densify()
    
    type(clf.coef_)
    # numpy.ndarray
    

    【讨论】:

      猜你喜欢
      • 2017-04-12
      • 2020-06-26
      • 2018-04-06
      • 2016-05-25
      • 2014-10-15
      • 2021-02-27
      • 2016-05-20
      • 2013-01-24
      • 2017-10-08
      相关资源
      最近更新 更多