解决 Python bug:make_moons() takes from 0 to 1 positional arguments but 3 were given

最近学sml,在跑老师给的knn算法时,原代码如下:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn import neighbors, datasets

np.random.seed(2017) # Set random seed so results are repeatable

N=[100,500,1000,5000]
n = 100 # number of training points
k = 5 # number of neighbors to consider 

## Generate a simple 2D dataset
X, y = datasets.make_moons(n,'True',0.3)
Xtest, ytest = datasets.make_moons(n,'True',0.3)
## Create instance of KNN classifier
classifier = neighbors.KNeighborsClassifier(k,'uniform')
classifier.fit(X, y)

python3.7环境下执行报错:

$ python3.7 knn-example.py
Traceback (most recent call last):
  File "knn-example.py", line 13, in 
    X, y = datasets.make_moons(n,'True',0.3)
TypeError: make_moons() takes from 0 to 1 positional arguments but 3 were given

 

make_moons()是sklearn里的dataset自带的函数,查看make_moons()函数声明也可以看到arguments数量可以接受三个。

尝试加上sklearn,如:

X, y = sklearn.datasets.make_moons(n,'True',0.3)

仍然报相同的错,搜索没看到合适的解决方案,后面试着显示传入参数解决了,即:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn import neighbors, datasets

np.random.seed(2017) # Set random seed so results are repeatable

N = [100,500,1000,5000]
n = 100 # number of training points
k = 1 # number of neighbors to consider 


## Generate a simple 2D dataset
X,y = datasets.make_moons(n_samples=n,shuffle=True,noise=0.3)#'True',0.3
## Create instance of KNN classifier
classifier = neighbors.KNeighborsClassifier(n_neighbors=k,weights='uniform')
classifier.fit(X, y)

不知道为什么在自己的环境下需要显示传入参数才行,同样的代码在别人的环境是可以直接运行的,记录一种解放方法~ 

你可能感兴趣的:(python,bug,开发语言,机器学习,sklearn)