Scikit线性模型何时返回负值得分?

Shy*_*mov 5 python linear-regression scikit-learn

我是机器学习的新手,我正在尝试实现线性模型估计器,以提供Scikit来预测二手车的价格。我用线性模型,的不同组合等LinearRegressionRidgeLassoElastic Net,但它们都在大多数情况下,返回负评分(-0.6 <=评分<= 0.1)。

有人告诉我这是因为多重共线性问题,但是我不知道如何解决。

我的示例代码:

import numpy as np
import pandas as pd
from sklearn import linear_model
from sqlalchemy import create_engine
from sklearn.linear_model import Ridge

engine = create_engine('sqlite:///path-to-db')

query = "SELECT mileage, carcass, engine, transmission, state, drive, customs_cleared, price FROM cars WHERE mark='some mark' AND model='some model' AND year='some year'"
df = pd.read_sql_query(query, engine)
df = df.dropna()
df = df.reindex(np.random.permutation(df.index))

X_full = df[['mileage', 'carcass', 'engine', 'transmission', 'state', 'drive', 'customs_cleared']]
y_full = df['price']

n_train = -len(X_full)/5
X_train = X_full[:n_train]
X_test = X_full[n_train:]
y_train = y_full[:n_train]
y_test = y_full[n_train:]

predict = [200000, 0, 2.5, 0, 0, 2, 0] # parameters of the car to predict

model = Ridge(alpha=1.0)
model.fit(X_train, y_train)
y_estimate = model.predict(X_test)

print("Residual sum of squares: %.2f" % np.mean((y_estimate - y_test) ** 2))
print("Variance score: %.2f" % model.score(X_test, y_test))
print("Predicted price: ", model.predict(predict))
Run Code Online (Sandbox Code Playgroud)

清除的,体,州,州和海关均为数字并代表类型。

什么是实施预测的正确方法?也许一些数据预处理或不同的算法。

谢谢你的进步!

And*_*eus 3

鉴于您使用的是岭回归,您应该使用 StandardScaler 或 MinMaxScaler 缩放变量:

http://scikit-learn.org/stable/modules/preprocessing.html#standardization-or-mean-removal-and-variance-scaling

也许使用管道:

http://scikit-learn.org/stable/modules/pipeline.html#pipeline-chaining-estimators

如果您使用普通回归,缩放就无关紧要;但对于岭回归,正则化惩罚项 ​​(alpha) 将以不同的方式处理不同尺度的变量。请参阅有关统计数据的讨论:

https://stats.stackexchange.com/questions/29781/when-should-you-center-your-data-when-should-you-standardize