Python 绘制正则化路径

Python 绘制正则化路径,python,matplotlib,machine-learning,scikit-learn,logistic-regression,Python,Matplotlib,Machine Learning,Scikit Learn,Logistic Regression,我有一段代码试图创建逻辑回归模型,并绘制模型的正则化路径。它只需提取一些稀疏数据,将其转换为训练集和测试集,然后尝试绘制正则化路径 我认为我所有的逻辑都是合理的,但是我从来没有为plotplt.plot(np.log10(min_c),LogRegCoefs)留下matplotlib调用。它似乎永远在运行,永远不会回来 我的数学有问题吗?有没有办法做到这一点,这将实际工作 from sklearn.svm import l1_min_c from sklearn.linear_model imp

我有一段代码试图创建逻辑回归模型,并绘制模型的正则化路径。它只需提取一些稀疏数据,将其转换为训练集和测试集,然后尝试绘制正则化路径

我认为我所有的逻辑都是合理的,但是我从来没有为plot
plt.plot(np.log10(min_c),LogRegCoefs)
留下matplotlib调用。它似乎永远在运行,永远不会回来

我的数学有问题吗?有没有办法做到这一点,这将实际工作

from sklearn.svm import l1_min_c
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import fetch_20newsgroups_vectorized
from sklearn.model_selection import train_test_split
x, y = fetch_20newsgroups_vectorized(subset='all', return_X_y=True)

#ignore sklearn's warning for a nicer output
def warn(*args, **kwargs):
    pass
import warnings
warnings.warn = warn

#regrab our same dataset from above
x_train, x_test, y_train, y_test = train_test_split(x, y,test_size=.1, random_state=42,stratify=y)

#get lowest bound for c, returns a list
#default argument is L2, must specify log
min_c = l1_min_c(x_train, y_train, loss='log') * np.logspace(0, 3)
logRegression = LogisticRegression(max_iter=1,random_state=42, solver="saga",multi_class="multinomial",penalty='l1')

#loop through c values and retrieve coefficients for each
LogRegCoefs = []
for i in range(len(min_c)):
    currentC = min_c[i]
    # set this C value, then refit and copy the parameters
    logRegression.set_params(C=currentC)
    # fit to current data 
    logRegression.fit(x_train, y_train)
    #after data is fit, get numpy data array with coefficients
    coefficients = logRegression.coef_
    #store in coefficients array
    LogRegCoefs.append(coefficients.ravel().copy())
# generate a plot
LogRegCoefs = np.array(LogRegCoefs)
plt.plot(np.log10(min_c), LogRegCoefs)
ymin, ymax = plt.ylim()
plt.xlabel('log(C)')
plt.ylabel('Coefficients')
plt.title('Logistic Regression Path')
plt.axis('tight')
plt.show()