python sklearn ValueError:使用序列设置数组元素

问题描述 投票:0回答:1

训练 sklearn sgd 分类器。根据数组中的姓名和年龄,获得颜色。乙 sgdclassifier 的 .fit() 错误。错误:“使用序列设置数组元素。”意思是?这是否意味着 sklearn 中的 sgd 分类器不能将数组的数组作为输入?

但是,如果名称和年龄不是数组(而只是单个元素),则不会出现错误。

from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np`

a=np.array([0, 2, 5, 2])
b=np.array( [0, 5, 0, 2])
c=np.array([2,2,0,0])
d=np.array([5,2,5,0])


age_a=np.array([5, 10, 7, 6])
age_b=np.array([3, 7, 11,8])
age_c=np.array([15, 10, 17, 2])
age_d=np.array([2, 8, 12,7])

color_a=np.array([0,2,1,1])
color_b=np.array([1,12,0,1])
color_c=np.array([0,1,1,0])
color_d=np.array([1,0,0,1])

#data2={'name':[a,b,c,d],'age':[age_a, age_b, age_c, age_d],'color':   [color_a,color_b,color_c,color_d]}

data2={'name':[a,b,c,d],'age':[age_a, age_b, age_c, age_d],'color':[0,1,0,1]}
new2 = pd.DataFrame.from_dict(data2)

print(new2)

x = new2.loc[:, new2.columns != 'color']
y = new2.loc[:, 'color']

x=np.array(x,dtype=object)
y=np.array(y)



x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=42)



from sklearn.linear_model import SGDClassifier

sgd_clf=SGDClassifier(random_state=42)
sgd_clf.fit(x_train, y_train)
sgd_clf.predict(x_test)`

` TypeError Traceback(最近一次调用最后一次) 类型错误:float() 参数必须是字符串或实数,而不是“列表”

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
Cell In[117], line 25
     21 print(y_test)
     24 clf = SGDClassifier(loss="hinge", penalty="l2", max_iter=5)
     25 clf.fit(x_train, y_train)
     26 #SGDClassifier(max_iter=100)
     28 clf.predict([[2., 2.]])


ValueError: setting an array element with a sequence.
python machine-learning scikit-learn
1个回答
0
投票

你可以像这样展平你的数组:

from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np

a = np.array([0, 2, 5, 2])
b = np.array([0, 5, 0, 2])
c = np.array([2, 2, 0, 0])
d = np.array([5, 2, 5, 0])

age_a = np.array([5, 10, 7, 6])
age_b = np.array([3, 7, 11, 8])
age_c = np.array([15, 10, 17, 2])
age_d = np.array([2, 8, 12, 7])

color_a = np.array([0, 2, 1, 1])
color_b = np.array([1, 12, 0, 1])
color_c = np.array([0, 1, 1, 0])
color_d = np.array([1, 0, 0, 1])

data2 = {'name': [a, b, c, d], 'age': [age_a, age_b, age_c, age_d], 'color': [0, 1, 0, 1]}
new2 = pd.DataFrame.from_dict(data2)

print(new2)

x = new2.loc[:, new2.columns != 'color']
y = new2.loc[:, 'color']

x_flattened = np.array([np.concatenate((row['name'], row['age'])) for _, row in x.iterrows()])
y = np.array(y)

x_train, x_test, y_train, y_test = train_test_split(x_flattened, y, test_size=0.25, random_state=42)

from sklearn.linear_model import SGDClassifier

sgd_clf = SGDClassifier(random_state=42)
sgd_clf.fit(x_train, y_train)
sgd_clf.predict(x_test)

要做出这样的输出,这似乎是正确的:

           name              age  color
0  [0, 2, 5, 2]    [5, 10, 7, 6]      0
1  [0, 5, 0, 2]    [3, 7, 11, 8]      1
2  [2, 2, 0, 0]  [15, 10, 17, 2]      0
3  [5, 2, 5, 0]    [2, 8, 12, 7]      1
© www.soinside.com 2019 - 2024. All rights reserved.