K-NeastNeighobr
算法思想:近朱者赤,近墨者黑。

1.加载数据

  1. import pandas as pd
  2. df=pd.read_csv('myData.csv')
  3. df.head()

image.png

2.预处理

清洗

  1. myResult=df.drop(['yingyu'],axis=1)
  2. myResult.head()

image.png

筛选

测试集

  1. train_x=df.iloc[0:80,2:4]
  2. train_x.head()

image.png

  1. train_y=df.iloc[0:80,4].values
  2. train_y[0:5]

array([1, 2, 3, 3, 1], dtype=int64)

3.训练集

  1. test_x=df.iloc[80:100,2:4]
  2. test_x.head()

image.png

4.建模

  1. from sklearn import neighbors
  2. model=neighbors.KNeighborsClassifier()
  1. model.fit(train_x,train_y)
  1. 输出:
  2. KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
  3. metric_params=None, n_jobs=1, n_neighbors=5, p=2,
  4. weights='uniform')
  5. N默认为5

5.预测

  1. model.predict(test_x)
  1. 输出:
  2. array([2, 3, 3, 3, 1, 1, 3, 2, 3, 1, 3, 1, 1, 2, 3, 2, 2, 1, 2, 1],
  3. dtype=int64)
  1. train_p=model.predict(train_x)
  2. train_p
  1. 输出:
  2. array([1, 2, 3, 3, 2, 3, 2, 3, 1, 2, 3, 3, 2, 3, 3, 1, 3, 1, 1, 3, 3, 1,
  3. 1, 3, 1, 1, 2, 3, 2, 3, 2, 2, 3, 2, 1, 3, 3, 2, 3, 2, 2, 2, 2, 1,
  4. 2, 1, 1, 3, 2, 3, 1, 2, 1, 3, 3, 3, 1, 2, 2, 3, 3, 3, 1, 1, 1, 3,
  5. 2, 3, 3, 1, 3, 2, 1, 3, 3, 1, 2, 2, 1, 1], dtype=int64)
  1. train_y