gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:50:43 25 4
gpt4 key购买 nike

我一直在尝试从头开始编写逻辑回归代码,我已经这样做了,但我正在使用我的乳腺癌数据集中的所有特征,我想选择一些特征(特别是我发现的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com