I am wondering what does sklearn logisticRegression
densify()
and sparsify()
methods return?
I thought these methods will print a matrix including coef_ info, instead of the output as below.
Just curious how to print out dense coef_ or sparse coef_ matrix as the document mention.
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. This is the default format ofcoef_
sparsify ( self ):
Convert coefficient matrix to sparse array format.
Converts the
coef_
member to a scipy.sparse matrix
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.
Here is a simple demonstration with the iris data:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.