Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 机器学习中回归的局限性?_Python_Machine Learning_Scikit Learn_Regression_Non Linear Regression - Fatal编程技术网

Python 机器学习中回归的局限性?

Python 机器学习中回归的局限性?,python,machine-learning,scikit-learn,regression,non-linear-regression,Python,Machine Learning,Scikit Learn,Regression,Non Linear Regression,我最近一直在学习ML的一些核心概念,并使用Sklearn库编写代码。经过一些基本实践,我尝试了kaggle的AirBnb NYC数据集,该数据集有大约40000个样本- 我试图建立一个模型,根据数据集的各种特征预测房间/公寓的价格。我意识到这是一个回归问题,并使用这个sklearn备忘单,我开始尝试各种回归模型 我使用sklearn.linear_model.Ridge作为基线,在清理了一些基本数据之后,我在测试集上得到了糟糕的R^2分数0.12。然后我想,也许线性模型太简单了,所以我尝试了适

我最近一直在学习ML的一些核心概念,并使用Sklearn库编写代码。经过一些基本实践,我尝试了kaggle的AirBnb NYC数据集,该数据集有大约40000个样本-

我试图建立一个模型,根据数据集的各种特征预测房间/公寓的价格。我意识到这是一个回归问题,并使用这个sklearn备忘单,我开始尝试各种回归模型

我使用sklearn.linear_model.Ridge作为基线,在清理了一些基本数据之后,我在测试集上得到了糟糕的R^2分数0.12。然后我想,也许线性模型太简单了,所以我尝试了适用于回归sklearn.kernel_ridge.kernel_ridge的“内核技巧”方法,但它们需要太多时间来拟合>1小时!为了解决这个问题,我使用sklearn.kernel_approximation.Nystroem函数来近似核映射,在训练之前将转换应用于特征,然后使用一个简单的线性回归模型。然而,如果我增加n_components参数,即使这样也需要花费大量时间进行变换和拟合,我必须获得任何有意义的精度提高

所以我现在在想,当你想在一个巨大的数据集上做回归时会发生什么?内核技巧在计算上非常昂贵,而线性回归模型过于简单,因为实际数据很少是线性的。那么,是神经网络是唯一的答案,还是我遗漏了一些聪明的解决方案


另外,我刚刚开始溢出,所以请让我知道我可以做些什么,使我的问题更好

这是一个很好的问题,但正如经常发生的那样,复杂的问题没有简单的答案。回归并不像通常所说的那样简单。它涉及许多假设,不限于线性最小二乘模型。要完全理解它需要几门大学课程。下面,我将写一份关于回归的快速且远未完成的备忘录:

没有什么能取代正确的分析。这可能涉及专家访谈,以了解数据集的限制。 你的模型任何模型,不局限于回归,只与你的特征一样好。如果房价取决于当地税率或学校评级,没有这些特征,即使是一个完美的模型也不会有好的表现。 有些功能无法通过设计包含在模型中,因此不要期望在现实世界中获得完美分数。例如,几乎不可能考虑到进入杂货店、餐馆、俱乐部等。这些功能中的许多功能也是移动目标,因为它们往往会随着时间的推移而改变。如果人类专家的表现更差,即使是0.12 R2也可能很棒。 模型有其假设。线性回归预计因变量价格与独立价格(如房地产规模)呈线性相关。通过探索残差,您可以观察到一些非线性,并用非线性特征覆盖它们。然而,有些模式很难发现,而其他模型,如非参数回归和神经网络,仍然可以处理。 那么,为什么人们仍然使用线性回归呢

这是最简单、最快的型号。对实时系统和统计分析有很多影响,所以这很重要 它通常被用作基线模型。在尝试一种奇特的神经网络架构之前,了解一下与一种简单的方法相比,我们有多大的改进是很有帮助的。 有时回归用于测试某些假设,例如效应的线性和变量之间的关系 总而言之,在大多数情况下,回归肯定不是最终的工具,但这通常是首先尝试的最便宜的解决方案

UPD,以说明关于非线性的观点

建立回归后,计算残差,即回归误差预测值-真实值。然后,为每个特征绘制散点图,其中横轴为特征值,纵轴为误差值。理想情况下,残差具有正态分布且不依赖于特征值。基本上,误差往往是小的而不是大的,并且在整个情节中是相似的

它应该是这样的:

这仍然是正常的-它只反映了样本密度的差异,但误差具有相同的分布:

这是一个周期模式的非线性示例,添加sinx+b作为特征:

非线性添加平方特征的另一个示例应有助于:

上述两个示例可以描述为取决于特征值的不同残差平均值。其他问题包括但不限于:

不同的方差取决于特征值 残差误差的非正态分布为+1或-1,聚类等 上面的一些照片是从这里拍摄的:


这是一本非常适合初学者阅读的回归诊断书。

我将尝试一下这本书。请看代码中嵌入的注释/注释。记住,这个 只是我测试过的一些想法。还有各种各样的事情你可以尝试获取更多的数据,测试不同的模型,等等

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
#%matplotlib inline
import sklearn
from sklearn.linear_model import RidgeCV, LassoCV, Ridge, Lasso
from sklearn.datasets import load_boston
#boston = load_boston()

# Predicting Continuous Target Variables with Regression Analysis
df = pd.read_csv('C:\\your_path_here\\AB_NYC_2019.csv')
df

# get only 2 fields and convert non-numerics to numerics
df_new = df[['neighbourhood']]
df_new = pd.get_dummies(df_new)
# print(df_new.columns.values)

# df_new.shape
# df.shape

# let's use a feature selection technique so we can see which features (independent variables) have the highest statistical influence on the target (dependent variable).
from sklearn.ensemble import RandomForestClassifier
features = df_new.columns.values
clf = RandomForestClassifier()
clf.fit(df_new[features], df['price'])

# from the calculated importances, order them from most to least important
# and make a barplot so we can visualize what is/isn't important
importances = clf.feature_importances_
sorted_idx = np.argsort(importances)

# what kind of object is this
# type(sorted_idx)
padding = np.arange(len(features)) + 0.5
plt.barh(padding, importances[sorted_idx], align='center')
plt.yticks(padding, features[sorted_idx])
plt.xlabel("Relative Importance")
plt.title("Variable Importance")
plt.show()
结果:

Best alpha using built-in LassoCV: 0.040582
Best score using built-in LassoCV: 0.103947
Lasso picked 78 variables and eliminated the other 146 variables
MAE 1004799260.0756996
MSE 9.87308783180938e+21
RMSE 99363412943.64531
R squared error -2.603867717517002e+17
MAE 85.35438165291622
MSE 36552.6244271195
RMSE 191.18740655994972
R squared error 0.03598346983552425
下一步

imp_coef = coef.sort_values()
import matplotlib
matplotlib.rcParams['figure.figsize'] = (8.0, 10.0)
imp_coef.plot(kind = "barh")
plt.title("Feature importance using Lasso Model")


# get the top 25; plotting fewer features so we can actually read the chart
type(imp_coef)
imp_coef = imp_coef.tail(25)
matplotlib.rcParams['figure.figsize'] = (8.0, 10.0)
imp_coef.plot(kind = "barh")
plt.title("Feature importance using Lasso Model")
结果:

Best alpha using built-in LassoCV: 0.040582
Best score using built-in LassoCV: 0.103947
Lasso picked 78 variables and eliminated the other 146 variables
MAE 1004799260.0756996
MSE 9.87308783180938e+21
RMSE 99363412943.64531
R squared error -2.603867717517002e+17
MAE 85.35438165291622
MSE 36552.6244271195
RMSE 191.18740655994972
R squared error 0.03598346983552425
这太可怕了!我们知道这行不通。我们试试别的吧。我们仍然需要用数字数据划船,所以让我们试试lng和lat坐标

X = df[['longitude','latitude']]
y = df['price']

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 10)

# Training the Model
# We will now train our model using the LinearRegression function from the sklearn library.

from sklearn.linear_model import LinearRegression
lm = LinearRegression()
lm.fit(X_train, y_train)

# Prediction
# We will now make prediction on the test data using the LinearRegression function and plot a scatterplot between the test data and the predicted value.
prediction = lm.predict(X_test)
plt.scatter(y_test, prediction)

df1 = pd.DataFrame({'Actual': y_test, 'Predicted':prediction})
df2 = df1.head(10)
df2
df2.plot(kind = 'bar')
结果:

Best alpha using built-in LassoCV: 0.040582
Best score using built-in LassoCV: 0.103947
Lasso picked 78 variables and eliminated the other 146 variables
MAE 1004799260.0756996
MSE 9.87308783180938e+21
RMSE 99363412943.64531
R squared error -2.603867717517002e+17
MAE 85.35438165291622
MSE 36552.6244271195
RMSE 191.18740655994972
R squared error 0.03598346983552425
让我们看看OLS:

import statsmodels.api as sm
model = sm.OLS(y, X).fit()


# run the model and interpret the predictions
predictions = model.predict(X)
# Print out the statistics
model.summary()
我假设如下:

一个热编码就是做它应该做的事情,但是它不能帮助你得到你想要的结果。使用lng/lat的性能稍好,但这也无助于实现您想要的结果。正如您所知,您必须处理回归问题的数字数据,但这些特性都不能帮助您预测价格,至少不是很好。当然,我可能在什么地方犯了个错误。如果我犯了错误,请告诉我

查看下面的链接,了解使用各种功能预测房价的好例子。注意:所有变量都是数字,结果相当不错,大约为70%,有给有取,但仍然比我们在Air BNB数据集上看到的要好得多


欢迎来到SO;首先,它是关于特定的编码问题,而不是ML理论和方法论问题。此类问题应发送至或,但。对于这一个,我推荐后者。我投票结束这个问题,因为它不是关于指南中定义的编程,而是关于一般的ML方法,因此应该将其发布到Data Science SE。非常感谢!这是很多工作,真的很感激!谢谢你,玛拉!两个后续问题-1-如何利用残差探索非线性?2-如果你有一个大数据集,其中特征标签关系是非线性的,你甚至可以做什么,因为内核技巧在计算上非常昂贵?