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

时间:2015-06-07 10:12:13

标签: python scikit-learn linear-regression

我是机器学习的新手,并试图实施线性模型估算器,提供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))

清除屠体,州,司机和海关是数字和代表类型。

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

感谢您的任何进展!

1 个答案:

答案 0 :(得分:1)

鉴于您正在使用Ridge Regression,您应该使用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

如果您使用的是vanilla回归,缩放并不重要;但是使用岭回归,正则化惩罚项(alpha)将以不同方式处理不同比例的变量。请参阅有关统计数据的讨论:

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