小编mak*_*kis的帖子

四舍五入的小熊猫无效

有人可以解释一下我发生了什么,或者我错过了一些非常明显的东西?

import pandas as pd
df = pd.DataFrame([[0.0094909865]])
df = df.round(9)
print(df)
Run Code Online (Sandbox Code Playgroud)

这给了我0.009490986.

我期待圆形值0.009490987

有人可以帮我理解吗?我正在使用Pandas'0.20.3'

python numpy rounding python-2.7 pandas

4
推荐指数
1
解决办法
650
查看次数

如何在scikit-learn(sklearn)的管道中应用StandardScaler?

在下面的示例中,

pipe = Pipeline([
        ('scale', StandardScaler()),
        ('reduce_dims', PCA(n_components=4)),
        ('clf', SVC(kernel = 'linear', C = 1))])

param_grid = dict(reduce_dims__n_components=[4,6,8],
                  clf__C=np.logspace(-4, 1, 6),
                  clf__kernel=['rbf','linear'])

grid = GridSearchCV(pipe, param_grid=param_grid, cv=3, n_jobs=1, verbose=2)
grid.fit(X_train, y_train)
print(grid.score(X_test, y_test))
Run Code Online (Sandbox Code Playgroud)

我正在使用StandardScaler(),这也是将其应用于测试集的正确方法吗?

python scikit-learn

4
推荐指数
1
解决办法
4472
查看次数

结合 GridSearchCV 和 StackingClassifier

我想使用 StackingClassifier 组合一些分类器,然后使用 GridSearchCV 来优化参数:

clf1 = RandomForestClassifier()
clf2 = LogisticRegression()
dt = DecisionTreeClassifier()
sclf = StackingClassifier(estimators=[clf1, clf2],final_estimator=dt)

params = {'randomforestclassifier__n_estimators': [10, 50],
          'logisticregression__C': [1,2,3]}

grid = GridSearchCV(estimator=sclf, param_grid=params, cv=5)

grid.fit(x, y)

Run Code Online (Sandbox Code Playgroud)

但这结果是一个错误:

'RandomForestClassifier' object has no attribute 'estimators_'
Run Code Online (Sandbox Code Playgroud)

我用过n_estimators。为什么它警告我没有estimators_

通常 GridSearchCV 应用于单个模型,所以我只需要在 dict 中写入单个模型的参数名称。

我参考此页面https://groups.google.com/d/topic/mlxtend/5GhZNwgmtSg但它使用早期版本的参数。即使我更改了新参数,它也不起作用。

顺便说一句,我在哪里可以了解这些参数的命名规则的详细信息?

python scikit-learn gridsearchcv

4
推荐指数
1
解决办法
1795
查看次数

没有名为'sklearn.lda'的模块

当我classifier.py在openface demos目录中运行时使用:

classifier.py train ./generated-embeddings/

我收到以下错误消息:

- >来自sklearn.lda import LDA

ModuleNotFoundError:没有名为'sklearn.lda'的模块.

我想要正确安装sklearn.

这个消息可能是什么原因?

python lda scikit-learn

3
推荐指数
2
解决办法
9203
查看次数

K-means算法的种子值如何放置?

我试图根据某个给定的数据集对客户进行分组,其中包含出生日期、性别、州、pincode、transaction_id、promocode 等属性。

每次运行该算法时,聚类的轮廓分数与前一个聚类的轮廓分数都会有巨大差异,即结果不一致。这可能是因为数据集的随机种子。这是将属性传递给算法的行。

km1 = KMeans(n_clusters=6, n_init=25, max_iter = 600)
Run Code Online (Sandbox Code Playgroud)

有没有什么方法可以分配集群或优化,以便每次运行程序后,分数都一致且更好?

我正在使用 Python 3 和 scikit-learn。

python cluster-analysis machine-learning k-means scikit-learn

3
推荐指数
1
解决办法
2万
查看次数

如何使用MinMaxScaler sklearn规范化训练和测试数据

所以,我怀疑并一直在寻找答案.所以问题是我何时使用,

from sklearn import preprocessing
min_max_scaler = preprocessing.MinMaxScaler()

df = pd.DataFrame({'A':[1,2,3,7,9,15,16,1,5,6,2,4,8,9],'B':[15,12,10,11,8,14,17,20,4,12,4,5,17,19],'C':['Y','Y','Y','Y','N','N','N','Y','N','Y','N','N','Y','Y']})

df[['A','B']] = min_max_scaler.fit_transform(df[['A','B']])
df['C'] = df['C'].apply(lambda x: 0 if x.strip()=='N' else 1)
Run Code Online (Sandbox Code Playgroud)

这之后,我将训练和测试模型(A,B作为特征,C如标签),并得到一些准确度得分.现在我的疑问是,当我必须预测新数据集的标签时会发生什么.说,

df = pd.DataFrame({'A':[25,67,24,76,23],'B':[2,54,22,75,19]})
Run Code Online (Sandbox Code Playgroud)

因为当我对列进行标准化时A,B将根据新数据更改值的值,而不是根据模型将要训练的数据.那么,现在我的数据准备步骤之后的数据如下所示.

data[['A','B']] = min_max_scaler.fit_transform(data[['A','B']])
Run Code Online (Sandbox Code Playgroud)

价值AB将相对于Max和的Min价值而变化df[['A','B']].的准备数据df[['A','B']]是相对于Min Maxdf[['A','B']].

关于不同的数字,数据准备如何有效?我不明白这里的预测是否正确.

python machine-learning normalization scikit-learn sklearn-pandas

3
推荐指数
1
解决办法
9007
查看次数

为 DecisionTreeClassifier 绘制多类 ROC 曲线

我试图用文档中提供的 svm.SVC 以外的分类器绘制 ROC 曲线。我的代码适用于 svm.SVC;然而,在我切换到 KNeighborsClassifier、MultinomialNB 和 DecisionTreeClassifier 后,系统一直告诉我check_consistent_length(y_true, y_score)Found input variables with inconsistent numbers of samples: [26632, 53264] 我的 CSV 文件看起来像这样

这是我的代码

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from itertools import cycle
import sys
from sklearn import svm, datasets
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import label_binarize
from sklearn.multiclass import OneVsRestClassifier
from scipy import interp
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import MultinomialNB
from …
Run Code Online (Sandbox Code Playgroud)

python machine-learning roc scikit-learn

3
推荐指数
2
解决办法
6917
查看次数

无法使用 3D 遮罩和索引为 numpy 数组赋值

我有一个 3D 数组,它是一个掩码。此外,我有一些索引,用于编码应保存某些值的位置(数组位置)。

一切似乎都工作正常,除了将值分配到所需位置后输出矩阵仍然为空。

我看不到我在这里缺少什么。我也试过numpy.put没有运气。

import numpy as np

# Initialize output matrix (here the results will be stored)
results = np.zeros((67, 67, 45))

# define the mask - where to put the calculated values in the results array
mask = np.random.randint(2, size=(67, 67, 45)).astype(bool)

# store the results only in these positions
index_keep = range(0, 13732)

values = np.ones((13732,))

results[mask][index_keep] = values.copy()

# the results array is still empty
print(results.sum())
#0
Run Code Online (Sandbox Code Playgroud)

python numpy mask masking matrix-indexing

3
推荐指数
2
解决办法
559
查看次数

Sklearn PCA 解释方差和解释方差比差异

我正在尝试从特征向量中获取方差。

之间有什么区别explained_variance_ratio_explained_variance_PCA

python covariance pca scikit-learn

3
推荐指数
1
解决办法
6513
查看次数

在获得最佳 TPOT 管道后获得 feature_importances_?

我已经通读了几页,但需要有人帮助解释如何进行这项工作。

我正在使用TPOTRegressor()以获得最佳管道,但从那里我希望能够绘制.feature_importances_它返回的管道:

best_model = TPOTRegressor(cv=folds, generations=2, population_size=10, verbosity=2, random_state=seed) #memory='./PipelineCache',       memory='auto',
best_model.fit(X_train, Y_train)
feature_importance = best_model.fitted_pipeline_.steps[-1][1].feature_importances_
Run Code Online (Sandbox Code Playgroud)

我在 Github 上的一个现已关闭的问题中看到了这种设置,但目前我收到错误消息:

Best pipeline: LassoLarsCV(input_matrix, normalize=True)

Traceback (most recent call last):
  File "main2.py", line 313, in <module>
    feature_importance = best_model.fitted_pipeline_.steps[-1][1].feature_importances_
AttributeError: 'LassoLarsCV' object has no attribute 'feature_importances_'
Run Code Online (Sandbox Code Playgroud)

那么,我如何从最佳管道中获得这些特征重要性,而不管它落在哪个管道上?或者这甚至可能吗?或者有人有更好的方法来尝试从 TPOT 运行中绘制特征重要性吗?

谢谢!

更新

为澄清起见,特征重要性的含义是确定数据集的每个特征 (X) 在确定预测 (Y) 标签方面的重要性,使用条形图绘制每个特征在得出其预测时的重要性级别。TPOT 不直接执行此操作(我不认为),所以我想我会抓住它提出的管道,在训练数据上重新运行它,然后以某种方式使用 a.feature_imprtances_然后能够绘制图形特征重要性,因为这些都是我正在使用的 sklearn 回归器?

python pipeline regression scikit-learn tpot

3
推荐指数
1
解决办法
1561
查看次数