【发布时间】:2019-08-03 07:06:14
【问题描述】:
我是新来的机器学习,我独立自主地建设我的第一个模型。我有一个数据集,用于评估的汽车,它包含价格,安全和豪华和分类的功能,如果它的好,非常好,可接受和不可接受。我转换所有的非数字列为数字,训练数据,并用一个测试组预测。然而,我的预测是可怕的;我用线性回归和r2_score产出0.05这几乎是0。我已经尝试了几种不同型号和一直给我可怕的预测性和准确性。
我做错了什么?我见过的教程,用类似的方法读文章,但他们最终以0.92的精度和我越来越0.05。你如何让你的数据的好模型,你怎么知道哪个型号使用? P>
代码:
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)
【问题讨论】:
-
前面已经回答了,你正在使用您的问题(分类)不恰当的模型(回归);检查scikit学习文档可用classiication模型(请注意,logistic回归,尽管它的名字,是一个分类模型,如回答以下建议)。我的回答here可能是有帮助的一般(PS请接受下面的答案,因为它本质上是正确的) SPAN>
标签: python-3.x machine-learning scikit-learn