为什么我的ML模型准确度很差?

问题描述 投票:0回答:1

我是机器学习的新手,我正在独立构建我的第一个模型。我有一个评估汽车的数据集,它包含价格,安全和奢侈品的特征,并分类是否良好,非常好,可接受和不可接受。我将所有非数字列转换为数字,训练数据并使用测试集进行预测。但是,我的预测很可怕;我使用了LinearRegression和r2_score输出0.05,实际上是0.我尝试了几种不同的模型,所有这些都给了我可怕的预测和准确性。

我究竟做错了什么?我看过教程,阅读类似方法的文章,但最终得到0.92准确度,我得到0.05。如何为您的数据建立一个好的模型,您如何知道使用哪种模型?

码:

import numpy as np
import pandas as pd
from sklearn import preprocessing, linear_model
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score

import seaborn as sns
import matplotlib.pyplot as plt

pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500) 
pd.set_option('display.width', 1000)

columns = ['buying', 'maint', 'doors', 'persons', 'lug_boot', 'safety', 'class value']
df = pd.read_csv('car.data.txt', index_col=False, names=columns)

for col in df.columns.values:
    try:
        if df[col].astype(int):
            pass
    except ValueError:
        enc = preprocessing.LabelEncoder()
        enc.fit(df[col])
        df[col] = enc.transform(df[col])

#Split the data
class_y = df.pop('class value')
x_train, x_test, y_train, y_test = train_test_split(df, class_y, test_size=0.2, random_state=0)

#Make the model
regression_model = linear_model.LinearRegression()
regression_model = regression_model.fit(x_train, y_train)

#Predict the test data
y_pred  = regression_model.predict(x_test)

score = r2_score(y_test, y_pred)
python-3.x machine-learning scikit-learn
1个回答
4
投票

您不应使用线性回归,它用于预测连续值而不是分类值。在你的情况下,你想要预测的是绝对的。从技术上讲,每种情况都是一个阶级。

我建议尝试使用Logistic回归或其他类型的分类方法,例如Naive Bayes,SVM,决策树分类器等。

© www.soinside.com 2019 - 2024. All rights reserved.