【问题标题】:TypeError: __init__() got multiple values for argument 'n_splits' in the cancer datasetTypeError: __init__() 在癌症数据集中获得了参数“n_splits”的多个值
【发布时间】:2020-10-07 09:32:36
【问题描述】:

数据集

Id,Cl.thickness,Cell.size,Cell.shape,Marg.adhesion,Epith.c.size,Bare.nuclei,Bl.cromatin,Normal.nucleoli,Mitoses,Class
1000025,5,1,1,1,2,1,3,1,1,benign
1002945,5,4,4,5,7,10,3,2,1,benign

代码如下

import math
import numpy as np
import pandas as pd
#from sklearn.grid_search import GridSearchCV
from sklearn.model_selection import learning_curve,GridSearchCV
from sklearn.linear_model import LogisticRegressionCV
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import cross_val_score, cross_val_predict, StratifiedKFold 
from sklearn import preprocessing, metrics, svm, ensemble
from sklearn.metrics import accuracy_score, classification_report
import tabpy_client 
# Breast Cancer dataset
# Citation: Dr. William H. Wolberg, University of Wisconsin Hospitals, Madison 
# https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+(Original)

# Read the dataset (Note that the CSV provided for this demo has rows with the missing data removed)
df =  pd.read_csv('breastcancer.csv', header=0)

# Take a look at the structure of the file
df.head(n=4)
# Drop Id column not used in analysis
df.drop(['Id'], 1, inplace=True)

# Use LabelEncoder to convert textual classifications to numeric. 
# We will use the same encoder later to convert them back.
encoder = preprocessing.LabelEncoder()
df['Class'] = encoder.fit_transform(df['Class'])

# You could also do this manually in the following way:
# df['Class'] = df['Class'].map( {'benign': 0, 'malignant': 1} ).astype(int)

# Check the result of the transform
df.head(n=6)
# Split columns into independent/predictor variables vs dependent/response/outcome variable
X = np.array(df.drop(['Class'], 1))
y = np.array(df['Class'])

# Scale the data. We will use the same scaler later for scoring function
scaler = preprocessing.StandardScaler().fit(X)
X = scaler.transform(X)

# 10 fold stratified cross validation
kf = StratifiedKFold(y,n_splits=10, random_state=None, shuffle=True)

# Define the parameter grid to use for tuning the Support Vector Machine
parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],
                     'C': [1, 10, 100, 1000]},
                    {'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]

# Pick the goal you're optimizing for e.g. precision if you prefer fewer false-positives
# recall if you prefer fewer false-negatives. For demonstration purposes let's pick several
# Note that the final model selection will be based on the last item in the list
scoringmethods = ['f1','accuracy','precision', 'recall','roc_auc']

为什么n_splits 抛出错误

TypeError: __init__() got multiple values for argument 'n_splits'. 

n_splits是gridsearch中的参数

【问题讨论】:

  • 错误到底是在哪里弹出的?请包括完整的跟踪。
  • 查看scikit-learn.org/stable/modules/generated/…,您不应该在启动分层kfol 实例时传递y 数组。而且因为 n splits 应该是第一个参数,所以它会抛出该错误。
  • @kubataytekin 很可能,就是这样。我建议您将其发布为答案(或者可能等到 OP 发布实际跟踪)

标签: python scikit-learn


【解决方案1】:

您不会在构造函数中将数据传递给 sklearn 模型实例。 这是https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.StratifiedKFold.html:的签名

StratifiedKFold(n_splits=5, *, shuffle=False, random_state=None)

您收到了该特定错误,因为 python 将 y 数组解释为 n_splits 参数。 至于拆分,请查看文档中的方法。

【讨论】:

  • kf = StratifiedKFold(n_splits=5, *, shuffle=False, random_state=None) ^ SyntaxError: invalid syntax
猜你喜欢
  • 2019-07-05
  • 1970-01-01
  • 1970-01-01
  • 2022-08-22
  • 2018-07-28
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
  • 2019-01-03
相关资源
最近更新 更多