[英]What is the output of densify() and sparsify() methods of sklearn LogisticRegression
I am wondering what does sklearn logisticRegression
densify()
and sparsify()
methods return?我想知道
sklearn logisticRegression
densify()
和sparsify()
方法返回什么?
I thought these methods will print a matrix including coef_ info, instead of the output as below.我认为这些方法将打印一个包含 coef_ 信息的矩阵,而不是如下所示的 output。
Just curious how to print out dense coef_ or sparse coef_ matrix as the document mention.只是好奇如何打印出文档提到的密集 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)
Looking closely at the documentation , we see:仔细查看文档,我们看到:
densify ( self ):
致密化(自我):
Convert coefficient matrix to dense array format.
将系数矩阵转换为密集数组格式。
Converts the
coef_
member (back) to a numpy.ndarray.将
coef_
成员(返回)转换为 numpy.ndarray。 This is the default format ofcoef_
这是
coef_
的默认格式sparsify ( self ):
稀疏化(自我):
Convert coefficient matrix to sparse array format.
将系数矩阵转换为稀疏数组格式。
Converts the
coef_
member to a scipy.sparse matrix将
coef_
成员转换为 scipy.sparse 矩阵
So, the effect of both these methods is on the returned coefficient matrix coef_
(converting it between dense and sparse format), and it is indeed no apparent in the model summary they both show as output when called.因此,这两种方法的效果都在返回的系数矩阵
coef_
(将其在密集和稀疏格式之间转换),并且在 model 摘要中确实不明显,它们在调用时都显示为 output。
Here is a simple demonstration with the iris data:这是一个使用 iris 数据的简单演示:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.