Python逻辑回归,使用2个特征数据X和标签Y – 训练准确度

huangapple go评论46阅读模式
英文:

Python Logistic regression with 2 features data X and label Y - Training accuracy

问题

# 导入sklearn和必要的库
from sklearn.linear_model import LogisticRegression

# 在给定数据X和标签Y上应用sklearn逻辑回归
X_skl = np.vstack((df1, df2))  # 10000 x 2 数组
Y_skl = Y  # 10000 x 1 数组
LogR = LogisticRegression()
LogR.fit(X_skl, Y_skl)
Y_skl_hat = LogR.predict(X_skl)

# 计算准确度
# 检查Y_skl与Y_skl_hat不相等的点的数量
error_count_skl = 0  # 计算错误点的数量
for i in range(N):
    if Y_skl[i] == Y_skl_hat[i]:
        error_count_skl = error_count_skl
    else:
        error_count_skl = error_count_skl + 1

# 计算准确度
准确度 = 100 * (N - error_count_skl) / N
print("准确度(%):")
print(准确度)

输出:
准确度(%):
99.48

你好,

我正在尝试在大小为10000 x 2的数组X和大小为10000 x 1的标签Y上使用Python中的sklearn库应用逻辑回归模型。我完全不知所措,因为我以前从未使用过这个库。有人可以帮我编写代码吗?

编辑:
抱歉问题比较模糊,目标是使用整个数据集X来找到训练准确度。上面是我想出的代码,有人可以看一下看是否合理吗?

英文:
# import sklearn and necessary libraries
from sklearn.linear_model import LogisticRegression

# Apply sklearn logistic regression on the given data X and labels Y
X_skl = np.vstack((df1,df2))   # 10000 x 2 array
Y_skl = Y   # 10000 x 1 array
LogR = LogisticRegression()
LogR.fit(X_skl,Y_skl)
Y_skl_hat = LogR.predict(X_skl)

# Calculate the accuracy
# Check the number of points where Y_skl is not equal to Y_skl_hat
error_count_skl = 0   # Count the number of error points
for i in range(N):
    if Y_skl[i] == Y_skl_hat[i]:
        error_count_skl = error_count_skl
    else:
        error_count_skl = error_count_skl + 1

# Calculate the accuracy 
Accuracy = 100*(N - error_count_skl)/N
print("Accuracy(%):")
print(Accuracy)

Output:
Accuracy(%):
99.48

Hello,

I'm trying to apply logistic regression model on array X (with size of 10000 x 2) and label Y (10000 x 1)
using sklearn library in Python. I'm completely lost cause I've never used this library before. Can anyone help me with the coding?

Edited:
Sorry for the vague question, the goal is to find the training accuracy using the entire dataset of X. Above is what I came up with, can anyone take a look and see if it makes sense?

答案1

得分: 2

要计算准确率,你可以简单地使用sklearn方法。

sklearn.metrics.accuracy_score(y_true, y_pred)

在你的情况下,

sklearn.metrics.accuracy_score(Y_skl, Y_skl_hat)

如果你想查看sklearn文档以获取有关accuracy_score的信息。

此外,你应该在一些数据上训练你的模型,并在其他数据上进行测试,以检查模型是否可以泛化,并避免过拟合。要将数据拆分为训练集和测试集,你可以使用:

sklearn.model_selection.train_test_split

如果你想查看sklearn文档以获取有关train_test_split的信息。

英文:

To calculate accuracy you can simply use this sklearn method.

sklearn.metrics.accuracy_score(y_true, y_pred)

In your case

sklearn.metrics.accuracy_score(Y_skl, Y_skl_hat)

If you want to take a look at
sklearn documentation for accuracy_score

And also you should train your model on some data and test it on others to check if the model can be generalized and to avoid overfitting.
To split your data in train and test datasets you could use:

sklearn.model_selection.train_test_split

If you want to take a look at
sklearn documentation for train_test_split

huangapple
  • 本文由 发表于 2023年2月19日 18:02:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定