【发布时间】:2021-07-26 02:57:51
【问题描述】:
预处理要花很多时间去理解,tuple,list,float,array结构。 我的数据看起来像
<bound method NDFrame.head of X Y
0 [1.9902, 1.9902, 1.9902, 1.9902, 1.9902, 0.034... [0.097, 0.097, 0.097, 0.094]
1 [1.9902, 0.034, 0.034, 0.034, 0.034, 0.034, 0.... [0.094, 0.094, 0.094, 0.094]
2 [0.034, 0.034, 0.097, 0.097, 0.097, 0.097, 0.0... [1.0882, 1.0882, 1.0882, 1.0882]
3 [0.097, 0.097, 0.097, 0.094, 0.094, 0.094, 0.0... [1.0882, 1.2382, 1.2382, 1.2382]
4 [0.094, 0.094, 0.094, 0.094, 1.0882, 1.0882, 1... [1.2382, 1.2382, 1.2182, 1.2182]
... ... ...
3395 [0.136, 0.286, 0.286, 0.286, 0.286, 0.286, 0.2... [0.1276, 0.1276, 0.1276, 0.1276]
3396 [0.286, 0.286, 0.266, 0.266, 0.266, 0.266, 0.2... [1.1423, 1.2923, 1.2723, 3.672]
3397 [0.266, 0.266, 0.266, 0.1276, 0.1276, 0.1276, ... [3.672, 3.672, 3.772, 3.772]
3398 [0.1276, 0.1276, 0.1276, 0.1276, 1.1423, 1.292... [3.772, 3.802, 3.802, 3.802]
3399 [1.1423, 1.2923, 1.2723, 3.672, 3.672, 3.672, ... [1.021, 1.021, 1.021, 1.021]
我正在使用
进行数据拆分x=csv_data['X']
y=csv_data['Y']
#print(x)
x_train, x_test, y_train, y_test = train_test_split(x,y)
拟合 KNN 模型
K = []
training = []
test = []
scores = {}
for k in range(2, 21):
clf = KNeighborsClassifier(n_neighbors = k)
clf.fit(x_train, y_train)
training_score = clf.score(x_train, y_train)
test_score = clf.score(x_test, y_test)
K.append(k)
training.append(training_score)
test.append(test_score)
scores[k] = [training_score, test_score]
遇到错误
TypeError Traceback (most recent call last)
TypeError: float() argument must be a string or a number, not 'list'
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
<ipython-input-93-906aa771beda> in <module>()
6 for k in range(2, 21):
7 clf = KNeighborsClassifier(n_neighbors = k)
----> 8 clf.fit(x_train, y_train)
9
10 training_score = clf.score(x_train, y_train)
7 frames
/usr/local/lib/python3.7/dist-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
81
82 """
---> 83 return array(a, dtype, copy=False, order=order)
84
85
ValueError: setting an array element with a sequence.
我一直在尝试一些方法,例如 preprocessing 或 StandardScaler dint 对我有用。
请帮助运行 KNN。
谢谢
【问题讨论】:
标签: python dataframe data-preprocessing