预测 [[50000, 5, 0.5]] 女神对我的态度 # 导入pandas处理数据 import pandas as pd # 定义目标值名称 target_names = ["didntLike", "smallDoses", "largeDoses"] def main(): # 1.获取原始数据集 pd.read_csv() 读取数据之后会返回一个DataFream数据类型 dating = pd.read_csv("./dating.txt") # dataDream支持像字典一个读取数据, 也支持切片操作 print(dating) # 2. 确定特征值与目标值 x = dating[["milage", "Liters", "Consumtime"]] y = dating["target"] print("x : ",x[:1]) return 0 main()帮我写完这个代码
时间: 2023-07-19 07:27:59 浏览: 122
好的,以下是完整的代码:
```python
# 导入pandas处理数据
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
# 定义目标值名称
target_names = ["didntLike", "smallDoses", "largeDoses"]
def main():
# 1.获取原始数据集
dating = pd.read_csv("./dating.txt")
# 2. 确定特征值与目标值
x = dating[["milage", "Liters", "Consumtime"]]
y = dating["target"]
print("x : ",x[:1])
# 3. 数据集划分
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
# 4. 特征工程
std = StandardScaler()
x_train = std.fit_transform(x_train)
x_test = std.transform(x_test)
# 5. 建立模型
knn = KNeighborsClassifier(n_neighbors=5)
# 6. 模型训练
knn.fit(x_train, y_train)
# 7. 模型预测
y_predict = knn.predict(x_test)
# 8. 输出评估结果
print("预测结果:", target_names[int(knn.predict([[50000, 5, 0.5]])[0])])
print("准确率:", knn.score(x_test, y_test))
return 0
if __name__ == '__main__':
main()
```
注意需要导入sklearn库中的KNeighborsClassifier和train_test_split函数,以及sklearn.preprocessing库中的StandardScaler函数。此外,代码中的特征值和目标值需要根据实际数据集进行修改。
阅读全文