【问题标题】:Simple logistic regression with sklearn is generating an IndexError使用 sklearn 的简单逻辑回归正在生成 IndexError
【发布时间】:2020-08-20 17:39:05
【问题描述】:

我正在尝试使用数据集(CSV 文件:https://drive.google.com/file/d/1EbW22jvbnI0i8JnHC5TqiWemQpbV1zOd/view?usp=sharing)生成一个简单的逻辑回归模型。尝试生成分类图时,产生了回溯错误,原因一直不清楚。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler 
Titanic_data = pd.read_csv('Titanic_noNAN.csv')
x = pd.DataFrame(Titanic_data['Age'])
y = pd.DataFrame(Titanic_data['Survived'])
xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size = 0.25, random_state = 0) 

sc_x = StandardScaler() 
xtrain = sc_x.fit_transform(xtrain)  
xtest = sc_x.transform(xtest)

from matplotlib.colors import ListedColormap 
X_set, y_set = xtest, ytest

X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1,  
stop = X_set[:, 0].max() + 1, step = 0.01), 
np.arange(start = X_set[:, 1].min() - 1,  
stop = X_set[:, 1].max() + 1, step = 0.01)) 

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-253-7e103a17b07d> in <module>
4 X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1,  
5                                stop = X_set[:, 0].max() + 1, step = 0.01), 
----> 6                      np.arange(start = X_set[:, 1].min() - 1,  
7                                stop = X_set[:, 1].max() + 1, step = 0.01)) 
8 

IndexError: index 1 is out of bounds for axis 1 with size 1

【问题讨论】:

  • 为什么所有这些printplot 语句、准确性和混淆矩阵?请注意,在问题中抛出all our code as-is 不起作用;至少,应该忽略(编辑)错误之后出现的代码(因此它与问题无关,因为从未执行)。

标签: python numpy scikit-learn


【解决方案1】:

你想画什么? X_set 是您的 xtest 预测,仅是 Age 值,因此 X_set 只有 1 列...

【讨论】:

    猜你喜欢
    • 2018-04-10
    • 2015-12-19
    • 2015-05-04
    • 2016-03-06
    • 2014-01-20
    • 2022-01-12
    • 2019-07-26
    • 1970-01-01
    • 2014-04-24
    相关资源
    最近更新 更多