我试图根据预先指定的标签对数据进行分类。你知道吗
有两列,如下所示:
room_class room_cluster
Standard single sea view Standard
Deluxe twin Single Deluxe
Suite Superior room ocean view Suite
Superior Double twin Superior
Deluxe Double room Deluxe
如上图所示,这组标签中有两个房间。你知道吗
代码段如下:
le = preprocessing.LabelEncoder()
datar = df
#### Separate data into feature and Labels
x = datar.room_class
y = datar.room_cluster
#### Using Label encoder to change string onto 'int'
le.fit(x)
addv = le.transform(x)
asb = addv.reshape(-1,1)
#### Splitting into training and testing set adn then using Knn
x_train,x_test,y_train,y_test=train_test_split(asb,y,test_size=0.40)
classifier=neighbors.KNeighborsClassifier(n_neighbors=3)
classifier.fit(x_train,y_train)
predictions = classifier.predict(x_test)
#### Checking the accuracy
print(accuracy_score(y_test,predictions))
我得到的测试数据的准确率只有78%,是不是代码中有什么错误妨碍了准确度。你知道吗
如何使用此模型预测自定义功能,例如:
输入:“套房单海景”
输出:“Suite”
输入:“高级双人套房”
Output:'高级'
我已经把它粗略地编码了,所以请你放心。你知道吗
参考文献:
相关问题 更多 >
编程相关推荐