训练sklearn sgd分类器.根据数组name和age,获得 colored颜色 .B sgdclassifier()错误.错误:'使用序列设置数组元素."什么意思?这是否意味着sklearn中的sgd分类器不能使用数组作为输入?

但是,如果name和age不是数组(并且只是单个元素),则不会有错误.

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(最近的呼叫最后一次) TypeError:float()参数必须是字符串或实数,而不是'list'

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.

推荐答案

您可以按如下方式展平数组:

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

Python相关问答推荐

Python:在类对象内的字典中更改所有键的索引,而不是仅更改一个键

Python在tuple上操作不会通过整个单词匹配

scikit-learn导入无法导入名称METRIC_MAPPING64'

无法使用DBFS File API路径附加到CSV In Datricks(OSError Errno 95操作不支持)

pandas在第1列的id,第2列的标题,第3列的值,第3列的值?

计算分布的标准差

删除marplotlib条形图上的底边

将pandas导出到CSV数据,但在此之前,将日期按最小到最大排序

如何在FastAPI中为我上传的json文件提供索引ID?

在不同的帧B中判断帧A中的子字符串,每个帧的大小不同

导入错误:无法导入名称';操作';

OpenGL仅渲染第二个三角形,第一个三角形不可见

如何删除重复的文字翻拍?

Python—在嵌套列表中添加相同索引的元素,然后计算平均值

按条件添加小计列

Regex用于匹配Python中逗号分隔的AWS区域

文本溢出了Kivy的视区

如何将列表从a迭代到z-以抓取数据并将其转换为DataFrame?

在不中断格式的情况下在文件的特定部分插入XML标签

如何在Polars中处理用户自定义函数的多行结果?