K-近邻算法(KNN)预测电影类型

目录

一:数据集准备

二:电影类型预测 详细步骤

三:完整源码分享 


一:数据集准备

下面博主先分享本节文章使用的数据集,

命名为classify.csv

搞笑镜头,拥抱镜头,打斗镜头,label
45,2,9,1
21,17,5,1
54,9,11,1
39,0,,1
5,2,57,2
3,2,65,2
6,4,21,2
7,46,4,3
9,39,8,3
9,38,17,3
2,3,55,2

二:电影类型预测 详细步骤

1 加载数据

# 搞笑镜头、拥抱镜头、打斗镜头
columns = ['a', 'b', 'c', 'label']
# 1加载数据
df = pd.read_csv("classify.csv", sep=',', names=columns, header=0)

2 缺失值处理 找到缺失值

# 2缺失值处理 找到缺失值
print(df.isnull().sum())
a        0
b        0
c        1
label    0
dtype: int64

3 缺失值处理 均值填充 替换原来的数据

# 替换原来数据inplace
df['c'].fillna(df.c.mean(), inplace=True)
print(df.describe())

4 数据打乱

# 4数据打乱
df = shuffle(df)

5 索引重新排列

# 5索引重新排序
df.reset_index(drop=True, inplace=True)
print(df.head(10), df.shape)
    a   b     c  label
0   3   2  65.0      2
1   5   2  57.0      2
2   2   3  55.0      2
3   7  46   4.0      3
4  54   9  11.0      1
5   9  38  17.0      3
6   6   4  21.0      2
7   9  39   8.0      3
8  45   2   9.0      1
9  21  17   5.0      1 (11, 4)

6 数据集划分

6-1 特征数据集划分

# 6数据划分
# 6-1特征训练集 + 特征测试集
x = df.iloc[:, :-1].values
print(x, x.shape, type(x))
[[ 5.   2.  57. ]
 [ 7.  46.   4. ]
 [ 2.   3.  55. ]
 [ 3.   2.  65. ]
 [21.  17.   5. ]
 [45.   2.   9. ]
 [ 9.  38.  17. ]
 [39.   0.  25.2]
 [ 9.  39.   8. ]
 [ 6.   4.  21. ]
 [54.   9.  11. ]] (11, 3) <class 'numpy.ndarray'>

 6-2 标签数据集划分 

# 6-2标签训练集 + 标签测试集
y = np.ravel(df.iloc[:, -1:].values)
print(y, y.shape, type(y))

7 训练集+测试集 比例划分

# 7训练集+测试集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=6)

8 创建算法

# 8创建算法 K近邻算法
knn_model = KNeighborsClassifier(n_neighbors=3)

9 创建模型

# 9创建基于训练集的模型
knn_model.fit(x_train, y_train)

10 模型评分

# 10模型评分
score = knn_model.score(x_test, y_test)
print(score)
0.6666666666666666

11 模型预测

# 11模型预测
y_predict = knn_model.predict(x_test)
print(y_predict == y_test)
[ True  True False]

三:完整源码分享 

完整源码分享,需要自取

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import pandas as pd
import numpy as np
from sklearn.utils import shuffle

# 搞笑镜头、拥抱镜头、打斗镜头
columns = ['a', 'b', 'c', 'label']
# 1加载数据
df = pd.read_csv("classify.csv", sep=',', names=columns, header=0)
# 2缺失值处理 找到缺失值
# print(df.isnull().sum())
# 替换原来数据inplace
df['c'].fillna(df.c.mean(), inplace=True)
# print(df.describe())
# 3数据打乱
df = shuffle(df)
# 5索引重新排序
df.reset_index(drop=True, inplace=True)
# print(df.head(10), df.shape)
# 6数据划分
# 6-1特征训练集 + 特征测试集
x = df.iloc[:, :-1].values
# print(x, x.shape, type(x))
# 6-2标签训练集 + 标签测试集
y = np.ravel(df.iloc[:, -1:].values)
# print(y, y.shape, type(y))
# 7训练集+测试集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=6)
# 8创建算法 K近邻算法
knn_model = KNeighborsClassifier(n_neighbors=3)
# 9创建基于训练集的模型
knn_model.fit(x_train, y_train)
# 10模型评分
score = knn_model.score(x_test, y_test)
print(score)
# 11模型预测
y_predict = knn_model.predict(x_test)
print(y_predict == y_test)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenruhan_QAQ_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值