我是新手和python,并尝试使用机器学习。但是我的代码不起作用。我的数据集非常简单,它包含七个图像(没有固定大小)。我尝试进行细分并运行adaboost算法但得到此错误。
Error:
array = np.array(array, dtype=dtype, order=order, copy=copy)
**ValueError: setting an array element with a sequence.**
我的代码
from skimage import data
import glob
import numpy as np
from sklearn import cluster
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import AdaBoostClassifier
initDataset = []
samplesListFiles = glob.glob("/home/bv/Downloads/sampleimage/*.*")
i = 0
for fileC in samplesListFiles:
img = data.imread(fileC, as_grey=True)
array = np.reshape(img, (np.product(img.shape), 1))
model = cluster.KMeans(n_clusters=3, random_state=25)
results = model.fit(array[::1])
X = array[::1]
X = results.predict(X)
initDataset.append(X)
dataset = np.array(initDataset)
C = 1.0
X=dataset
y=[[0.],
[0.],
[1.],
[0.],
[1.],
[1.],
[0.]]
rez = []
for i in range(0, len(y)):
rez.append(y[i][0])
svc = GaussianNB()
clf = AdaBoostClassifier(n_estimators=100,
base_estimator=svc,learning_rate=1)
X = X.reshape((7,-1))
clf.fit(X,rez)
print "TEST"
cancerListFiles = glob.glob("/home/bv/Downloads/cancerimage/*.*")
for fileC in samplesListFiles:
img = data.imread(fileC, as_grey=True)
array = np.reshape(img, (np.product(img.shape), 1))
model = cluster.KMeans(n_clusters=3, random_state=25)
results = model.fit(array[::1])
X = array[::1]
X = results.predict(X)
print clf.predict(X)
Befor call .fit X
是
[[array([0, 0, 0, ..., 0, 0, 0], dtype=int32)]
[array([0, 0, 0, ..., 0, 0, 0], dtype=int32)]
[array([1, 1, 1, ..., 1, 1, 1], dtype=int32)]
[array([0, 0, 0, ..., 0, 0, 0], dtype=int32)]
[array([2, 2, 2, ..., 2, 2, 2], dtype=int32)]
[array([0, 0, 0, ..., 0, 0, 0], dtype=int32)]
[array([2, 2, 0, ..., 0, 0, 0], dtype=int32)]]