我有一个包含以下列的数据文件
'customer', 'calibrat' - 校准样本 = 1; 验证样本 = 0; 'churn', 'churndep', '收入', 'mou',
数据文件包含大约 40000 行,其中 20000 行的校准值为 1。我想将此数据拆分为
X1 = data.loc[:, data.columns != 'churn']
y1 = data.loc[:, data.columns == 'churn']
from imblearn.over_sampling import SMOTE
os = SMOTE(random_state=0)
X1_train, X1_test, y1_train, y1_test = train_test_split(X1, y1, test_size=0.3, random_state=0)
Run Code Online (Sandbox Code Playgroud)
我想要的是,在我的 X1_train 中应该带有 calibrat =1 的校准数据,而在 X1_test 中应该带有用于验证的所有数据 calibrat = 0
python machine-learning logistic-regression train-test-split smote
我正在尝试在 R 中对不平衡数据集进行 SMOTE。我尝试为此安装“DMwR”包,但似乎该包已从 cran 存储库中删除。\n我收到错误:“包 \xe2\x80\x98DMwR\xe2\x80\x99 不可用(对于 R版本 4.0.2) "
\n谁能帮我解决这个问题吗?或者建议在 R 中使用 SMOTE 的任何其他包?
\n蒂亚!
\n我已经在这里、这里和这里提到了这些帖子。不要将其标记为重复。
我正在研究一个二元分类问题,其中我的数据集具有分类列和数字列。
但是,某些分类列混合有数字和字符串值。尽管如此,它们仅指示类别名称。
例如,我有一个名为的列biz_category
,其中包含诸如等的值A,B,C,4,5
。
我猜想以下错误是由于诸如 之类的值引发的4 and 5
。
因此,我尝试了以下将它们转换为category
数据类型。(但仍然不起作用)
cols=X_train.select_dtypes(exclude='int').columns.to_list()
X_train[cols]=X_train[cols].astype('category')
Run Code Online (Sandbox Code Playgroud)
我的数据信息如下所示
<class 'pandas.core.frame.DataFrame'>
Int64Index: 683 entries, 21 to 965
Data columns (total 9 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Feature_A 683 non-null category
1 Product Classification 683 non-null category
2 Industry 683 non-null category
3 DIVISION 683 non-null category
4 biz_category 683 non-null category
5 Country 683 non-null category
6 Product …
Run Code Online (Sandbox Code Playgroud) 问题:在实施 SMOTE(一种过采样)时,我的数据帧正在转换为 numpy 数组)。
test_train_split
from sklearn.model_selection import train_test_split
X_train, X_test, y_train_full, y_test_full = train_test_split(X, y, test_size=0.20, random_state=66)
[IN]type(X_train)
[OUT]pandas.core.frame.DataFrame
Run Code Online (Sandbox Code Playgroud)
在 SMOTE 之后,X_train 的数据类型从 pandas 数据帧变为 numpy 数组
from imblearn.over_sampling import SMOTE
sm = SMOTE(random_state = 42)
X_train, y_train = sm.fit_sample(X_train, y_train)
[IN]type(X_train)
[OUT]numpy.ndarray
Run Code Online (Sandbox Code Playgroud)
预期输出 我想在 SMOTE 后保留 X_train 和 X_test 的数据帧结构。怎么做?
我想我在下面的代码中遗漏了一些东西。
from sklearn.model_selection import train_test_split
from imblearn.over_sampling import SMOTE
# Split into training and test sets
# Testing Count Vectorizer
X = df[['Spam']]
y = df['Value']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=40)
X_resample, y_resampled = SMOTE().fit_resample(X_train, y_train)
sm = pd.concat([X_resampled, y_resampled], axis=1)
Run Code Online (Sandbox Code Playgroud)
当我收到错误时
ValueError:无法将字符串转换为浮点数:---> 19 X_resampled,y_resampled = SMOTE().fit_resample(X_train,y_train)
数据示例是
Spam Value
Your microsoft account was compromised 1
Manchester United lost against PSG 0
I like cooking 0
Run Code Online (Sandbox Code Playgroud)
我会考虑转换训练集和测试集来解决导致错误的问题,但我不知道如何应用于两者。我在谷歌上尝试了一些例子,但它没有解决问题。
导入“from imblearn.over_sampling import SMOTE”时,出现导入错误。请检查并提供帮助。我尝试升级 sklearn,但升级因“OSError”而被撤消。首先通过pip安装不平衡-学习。
!pip install -U imbalanced-learn
Run Code Online (Sandbox Code Playgroud)
使用 jupyter 笔记本
Windows 10
sklearn 版本 - 0.24.1
numpy 版本 - 1.19.5
--------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Run Code Online (Sandbox Code Playgroud)
在 ----> 1 from imblearn.over_sampling import SMOTE
~\anaconda3\lib\site-packages\imblearn_ init _.py in 35 导入类型 36 ---> 37 from . 从 导入联合收割机 38。从 导入 ensemble 39 进口例外
~\anaconda3\lib\site-packages\imblearn\combine_ init _.py in 3 """ 4 ----> 5 from ._smote_enn import SMOTEENN 6 from ._smote_tomek import SMOTETomek 7
〜\anaconda3\lib\site-packages\imblearn\combine_smote_enn.py in 8 from sklearn.utils import check_X_y …
我想同时应用交叉验证和过采样。我从这段代码中得到以下错误:
from sklearn.pipeline import Pipeline, make_pipeline
imba_pipeline = make_pipeline(SMOTE(random_state=42),
LogisticRegression(C=3.4))
cross_val_score(imba_pipeline, X_train_tf, y_train, scoring='f1-weighted', cv=kf)
Run Code Online (Sandbox Code Playgroud)
所有中间步骤应该是转换器并实现拟合和转换,或者是字符串 'passthrough' 'SMOTE(k_neighbors=5, kind='deprecated', m_neighbors='deprecated', n_jobs=1, out_step='deprecated', random_state=42 , ratio=None, sampling_strategy='auto', svm_estimator='deprecated')' (type ) 不
附注。我用得到同样的错误imblearn.over_sampling.RandomOverSampler而非SMOTE。
由于我的数据集不平衡,我正在使用 SMOTE 进行过采样。我收到一个意外的参数错误。但是在文档中,该ratio
参数是为 SMOTE 定义的。有人可以帮助我了解我哪里出错了吗?
代码片段
from imblearn.over_sampling import SMOTE
sm = SMOTE(random_state=42, ratio=0.6)
Run Code Online (Sandbox Code Playgroud)
错误
TypeError: __init__() got an unexpected keyword argument 'ratio'
Run Code Online (Sandbox Code Playgroud) 我的数据有点不平衡,所以我在做逻辑回归模型之前尝试做一个 SMOTE 算法。当我这样做时,我收到错误: KeyError: 'Only the Series name can be used for the key in Series dtype mappings.' 有人可以帮我弄清楚原因吗?这是代码:
X = dummies.loc[:, dummies.columns != 'Count']
y = dummies.loc[:, dummies.columns == 'Count']
#from imblearn.over_sampling import SMOTE
os = SMOTE(random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
columns = X_train.columns
os_data_X,os_data_y=os.fit_sample(X_train, y_train) # here is where it errors
os_data_X = pd.DataFrame(data=os_data_X,columns=columns )
os_data_y= pd.DataFrame(data=os_data_y,columns=['Count'])
Run Code Online (Sandbox Code Playgroud)
谢谢!
我正在尝试使用 SMOTE 平衡 jupyter-notebook 上的数据:
from imblearn import over_sampling
from imblearn.over_sampling import SMOTE
balanced = SMOTE()
x_balanced , y_balanced = balanced.fit_resample(X_train,y_train)
Run Code Online (Sandbox Code Playgroud)
但我在第一行收到以下错误 -
AttributeError: module 'sklearn.metrics._dist_metrics' has no attribute 'DatasetsPair'
Run Code Online (Sandbox Code Playgroud)
为什么我会收到此错误?谢谢。
我正在使用 SMOTE 重新采样一些数据并收到如下错误:
AttributeError:“NoneType”对象没有属性“split”
我的代码:
sm = SMOTE(random_state = 42)
X_train_resampled, y_train_resampled = sm.fit_resample(X_train_final, y_train)
Run Code Online (Sandbox Code Playgroud)
有人可以帮我解决这个问题吗?因为我的数据似乎没有任何问题
完整问题:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-49-9465f7b6ac21> in <module>
1 #resample data using SMOTE
2 sm = SMOTE(random_state = 42)
----> 3 X_train_resampled, y_train_resampled = sm.fit_resample(X_train_final, y_train)
~\AppData\Roaming\Python\Python38\site-packages\imblearn\base.py in fit_resample(self, X, y)
81 )
82
---> 83 output = self._fit_resample(X, y)
84
85 y_ = (
~\AppData\Roaming\Python\Python38\site-packages\imblearn\over_sampling\_smote\base.py in _fit_resample(self, X, y)
322
323 self.nn_k_.fit(X_class)
--> 324 nns = self.nn_k_.kneighbors(X_class, return_distance=False)[:, 1:]
325 …
Run Code Online (Sandbox Code Playgroud) smote ×11
python ×8
pandas ×5
imblearn ×3
scikit-learn ×3
oversampling ×2
jupyter ×1
nonetype ×1
numpy ×1
r ×1
resample ×1
resampling ×1
sampling ×1
split ×1