训练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.