gpt4 book ai didi

python - 如果不使用 Standard Scaler 会出现内存错误

转载 作者:行者123 更新时间:2023-12-04 07:14:12 26 4
gpt4 key购买 nike

我在这里读过 https://towardsdatascience.com/do-decision-trees-need-feature-scaling-97809eaa60c6并观看 https://www.youtube.com/watch?v=nmBqnKSSKfM&ab_channel=KrishNaik视频表明您不需要使用标准缩放器进行决策树机器学习。
但是,发生在我的代码上的情况恰恰相反。这是我试图运行的代码。

# importing libraries  
import numpy as nm
import matplotlib.pyplot as mpl
import pandas as pd

#importing datasets
data_set= pd.read_csv('Social_Network_Ads.csv')

#Extracting Independent and dependent Variable
x= data_set.iloc[:, [2,3]].values
y= data_set.iloc[:, 4].values

# Splitting the dataset into training and test set.
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test= train_test_split(x, y, test_size= 0.25, random_state=0)

#feature Scaling

from sklearn.preprocessing import StandardScaler
st_x= StandardScaler()
x_train= st_x.fit_transform(x_train)
x_test= st_x.transform(x_test)


#Fitting Decision Tree classifier to the training set
from sklearn.tree import DecisionTreeClassifier
classifier= DecisionTreeClassifier(criterion='entropy', random_state=0)
classifier.fit(x_train, y_train)
我继续尝试将数据可视化的部分的问题。这是代码。
#Visulaizing the trianing set result  
from matplotlib.colors import ListedColormap
x_set,y_set = x_train, y_train
x1, x2 = nm.meshgrid(nm.arange(start = x_set[:, 0].min() - 1, stop = x_set[:, 0].max() + 1, step =0.01),
nm.arange(start = x_set[:, 1].min() - 1, stop = x_set[:, 1].max() + 1, step = 0.01))
mpl.contourf(x1, x2, classifier.predict(nm.array([x1.ravel(), x2.ravel()]).T).reshape(x1.shape),
alpha = 0.75, cmap = ListedColormap(('purple','green' )))
mpl.xlim(x1.min(), x1.max())
mpl.ylim(x2.min(), x2.max())
for i, j in enumerate(nm.unique(y_set)):
mpl.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1], c = ListedColormap(('purple', 'green'))(i), label = j)
mpl.title('Decision Tree Algorithm (Training set)')
mpl.xlabel('Age')
mpl.ylabel('Estimated Salary')
mpl.legend()
mpl.show()
输出成功 如果我使用 StandardScaler 运行它 .该图显示得很好。但是,当我对 StandardScaler 部分进行散列(评论)时,它声明了 内存错误。
MemoryError                               Traceback (most recent call last)
<ipython-input-8-1282bf709e27> in <module>
3 x_set,y_set = x_train, y_train
4 x1, x2 = nm.meshgrid(nm.arange(start = x_set[:, 0].min() - 1, stop = x_set[:, 0].max() + 1, step =0.01),
----> 5 nm.arange(start = x_set[:, 1].min() - 1, stop = x_set[:, 1].max() + 1, step = 0.01))
6 mpl.contourf(x1, x2, classifier.predict(nm.array([x1.ravel(), x2.ravel()]).T).reshape(x1.shape),
7 alpha = 0.75, cmap = ListedColormap(('purple','green' )))

~\Anaconda3\lib\site-packages\numpy\lib\function_base.py in meshgrid(*xi, **kwargs)
4209
4210 if copy_:
-> 4211 output = [x.copy() for x in output]
4212
4213 return output

~\Anaconda3\lib\site-packages\numpy\lib\function_base.py in <listcomp>(.0)
4209
4210 if copy_:
-> 4211 output = [x.copy() for x in output]
4212
4213 return output

MemoryError:
错误只发生在可视化部分;在代码的另一部分,这种预测在没有标准缩放器的情况下也能很好地工作。
决策树可以在没有标准缩放器的情况下工作吗?如果是,我该如何解决这个问题?

最佳答案

决策树可以在没有标准缩放器和标准缩放器的情况下工作。这里要注意的重要一点是,缩放数据不会影响决策树模型的性能。
如果您之后绘制数据,尽管我想您不想绘制缩放数据,而是绘制原始数据;因此你的问题。
我能想到的最简单的解决方案是通过 sparse=True作为 numpy.meshgrid 的参数因为这似乎是在您的回溯中引发错误的原因。在过去的问题 here. 中对此有一些详细信息
因此适用于您的问题,这意味着您更改了这一行:

nm.meshgrid(
nm.arange(start=x_set[:, 0].min() - 1, stop=x_set[:, 0].max() + 1, step=0.01),
nm.arange(start=x_set[:, 1].min() - 1, stop=x_set[:, 1].max() + 1, step=0.01),
)
nm.meshgrid(
nm.arange(start=x_set[:, 0].min() - 1, stop=x_set[:, 0].max() + 1, step=0.01),
nm.arange(start=x_set[:, 1].min() - 1, stop=x_set[:, 1].max() + 1, step=0.01),
sparse=True,
)

关于python - 如果不使用 Standard Scaler 会出现内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68881786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com