我可以知道为什么收到错误消息吗?
NameError:名称'X_train_std'未定义
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression(C=1000.0, random_state=0)
lr.fit(X_train_std, y_train)
plot_decision_regions(X_combined_std,
y_combined, classifier=lr,
test_idx=range(105,150))
plt.xlabel('petal length [standardized]')
plt.ylabel('petal width [standardized]')
plt.legend(loc='upper left')
plt.tight_layout()
plt.show()
lr.predict_proba(X_test_std[0,:])
weights, params = [], []
for c in np.arange(-5, 5):
lr = LogisticRegression(C=10**c, random_state=0)
lr.fit(X_train_std, y_train)
weights.append(lr.coef_[1])
params.append(10**c)
weights = np.array(weights)
plt.plot(params, weights[:, 0],
label='petal length')
plt.plot(params, weights[:, 1], linestyle='--',
label='petal width')
plt.ylabel('weight coefficient')
plt.xlabel('C')
plt.legend(loc='upper left')
plt.xscale('log')
plt.show()
请看链接-
https://www.freecodecamp.org/forum/t/how-to-modify-my-python-logistic-regression/265795
https://bytes.com/topic/python/answers/972352-why-i-get-x_train_std-not-defined#post3821849
https://www.researchgate.net/post/Why_I_get_the_X_train_std_is_not_defined
。
答案 0 :(得分:0)
好,X_train_std
没有定义/声明。您需要先声明该变量并给它一个值,然后再使用它。
赞:
X_train_std = 3
答案 1 :(得分:0)
您没有复制足够的示例代码。在此之上的某个位置,可能会调用train_test_split
基本上,要执行所需的操作,您需要一组X变量,即Y变量(可以预测的变量)。通常,您将它们分为训练集和测试集,此外,许多算法在标准化(零均值,1个标准差)上效果更好,这就是_std在变量名中的含义。
代码段之前的代码可能类似于:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
my_df = pd.DataFrame(....this is your data for the test...)
X = my_df[[X_variable_column_names_here]]
Y = my_df[Y_variable_column_name]
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.3)
scaler = StandardScaler()
X_train_std = scaler.fit_transform(X_train)
X_test_std = scaler.transform(X_test)
编辑:从图上的轴标签上看,您正试图对Iris数据集进行逻辑回归。完整的示例在这里: https://scikit-learn.org/stable/auto_examples/linear_model/plot_iris_logistic.html