python - 如何在 python 中从头开始选择用于逻辑回归的特征?

标签 python machine-learning logistic-regression feature-selection

我一直在尝试从头开始编写逻辑回归代码,我已经这样做了,但我正在使用我的乳腺癌数据集中的所有特征,我想选择一些特征(特别是我发现的 scikit-当我与它比较并在数据上使用它的特征选择时,learn 已经为自己选择了)。但是,我不确定在我的代码中在哪里执行此操作,我目前拥有的是:

X_train = ['texture_mean', 'smoothness_mean', 'compactness_mean', 'symmetry_mean', 'radius_se', 'symmetry_se'
'fractal_dimension_se', 'radius_worst', 'texture_worst', 'area_worst', 'smoothness_worst', 'compactness_worst']
X_test = ['texture_mean', 'smoothness_mean', 'compactness_mean', 'symmetry_mean', 'radius_se', 'symmetry_se'
'fractal_dimension_se', 'radius_worst', 'texture_worst', 'area_worst', 'smoothness_worst', 'compactness_worst']

def Sigmoid(z):
    return 1/(1 + np.exp(-z))

def Hypothesis(theta, X):   
    return Sigmoid(X @ theta)

def Cost_Function(X,Y,theta,m):
    hi = Hypothesis(theta, X)
    _y = Y.reshape(-1, 1)
    J = 1/float(m) * np.sum(-_y * np.log(hi) - (1-_y) * np.log(1-hi))
    return J

def Cost_Function_Derivative(X,Y,theta,m,alpha):
    hi = Hypothesis(theta,X)
    _y = Y.reshape(-1, 1)
    J = alpha/float(m) * X.T @ (hi - _y)
    return J

def Gradient_Descent(X,Y,theta,m,alpha):
    new_theta = theta - Cost_Function_Derivative(X,Y,theta,m,alpha)
    return new_theta

def Accuracy(theta):
    correct = 0
    length = len(X_test)
    prediction = (Hypothesis(theta, X_test) > 0.5) 
    _y = Y_test.reshape(-1, 1)
    correct = prediction == _y
    my_accuracy = (np.sum(correct) / length)*100
    print ('LR Accuracy: ', my_accuracy, "%")

def Logistic_Regression(X,Y,alpha,theta,num_iters):
    m = len(Y)
    for x in range(num_iters):
        new_theta = Gradient_Descent(X,Y,theta,m,alpha)
        theta = new_theta
        if x % 100 == 0:
            print #('theta: ', theta)    
            print #('cost: ', Cost_Function(X,Y,theta,m))
    Accuracy(theta)
ep = .012 
initial_theta = np.random.rand(X_train.shape[1],1) * 2 * ep - ep
alpha = 0.5
iterations = 10000
Logistic_Regression(X_train,Y_train,alpha,initial_theta,iterations)

我假设如果我手动更改包含 X_train 和 X_test 的哪些特征会起作用,但我收到一个错误:AttributeError: 'list' object has no attribute 'shape' at the initial_theta line。任何在正确方向上的帮助将不胜感激。

最佳答案

问题是 X_train 是一个列表,形状仅适用于数据帧。

你可以: -保留列表但改用 len(X_train),或者 - 将 X_train 类型更改为 pandas 数据框,pandas.DataFrame(X_train).shape[0]

关于python - 如何在 python 中从头开始选择用于逻辑回归的特征?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48403445/

相关文章:

python - 检测图像在较大图像中的位置

python - XGBoost plot_importance 不显示特征名称

python - 导入train_test_split时模块导入错误

machine-learning - sigmoid 函数在逻辑回归中真的很重要吗?

r - 在ggplot geom_raster中具有不同斜率的逻辑回归之间进行插值

python - Theano 中的 5D 张量

python - numpy,获取最大的子集

python - 在 Keras 中使用 fasttext 预训练模型作为嵌入层

matlab - matlab中的逻辑回归

python - 如何比较数据框中同一列的数据(Pandas)