sklearn LogisticRegression的densify()和sparsify()方法的输出是什么

我想知道sklearn logisticRegressiondensify()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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注