[Python] [scikit-learn] k-nearest neighbor method introductory memo

Scikit-learn learning notes

scikit-learn naming convention

letter meaning
X data
y label

train_test_split function

A function that divides data (X, y) for training and evaluation

Shuffle the dataset with pseudo-random numbers before splitting.

The data points are sorted by label, so if you put the last 25% in the test set, you don't want all the data points to be label 2 (one value).

Data split with train_test_split function Random number seed

jupyter_notebook.ipynb



train_test_split(First argument:Feature matrix X,Second argument:Objective variable y, test_size(=0.3):Percentage of data size for testing, random_state= :Random number seed value when dividing data)

random_state=A value of 0 makes the output deterministic and always gives the same result.(For study)

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
    iris_dataset['data'], iris_dataset['target'], random_state=0)

pandas.DataFrame

pandas.DataFrame

pandas.DataFrame


import pandas as pd

#reference
pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)

#Example
iris_dataframe = pd.DataFrame(X_train, columns=iris_dataset.feature_names)

Output result スクリーンショット 2020-08-06 15.17.20.png

pandas.plotting.scatter_matrix

pandas.plotting.scatter_matrix

python:pandas.plotting.scatter_matrix


#Official reference
pandas.plotting.scatter_matrix(frame, alpha=0.5, figsize=None, ax=None, grid=False, diagonal='hist', marker='.', density_kwds=None, hist_kwds=None, range_padding=0.05, **kwargs)

#iris example
iris_dataframe = pd.DataFrame(X_train, columns=iris_dataset.feature_names)
grr = pd.plotting.scatter_matrix(iris_dataframe, c=y_train, figsize=(8, 8), marker='o',hist_kwds={'bins' : 20}, s=60, alpha=.8)

Output result スクリーンショット 2020-08-06 15.26.52.png

scikit-learn is always a two-dimensional array

scikit-learn


X_new = np.array([[5, 2.9, 1, 0.2]])

sklearn.neighbors.KNeighborsClassifier

Classification by k-nearest neighbor method

sklearn.neighbors.KNeighborsClassifier

neighbors.KNeighborsClassifier


#Important method

.fit(X, y)
#Fit the model with X as the training data and y as the target value

.predict(X)
#Predict the class label of the data provided.

.score(X, y)
#Returns the average precision of the given test data and label.

Recommended Posts

[Python] [scikit-learn] k-nearest neighbor method introductory memo
2. Multivariate analysis spelled out in Python 8-1. K-nearest neighbor method (scikit-learn)
K-nearest neighbor method (multiclass classification)
Implemented k-nearest neighbor method in python from scikit learn
A simple Python implementation of the k-nearest neighbor method (k-NN)
2. Multivariate analysis spelled out in Python 8-3. K-nearest neighbor method [cross-validation]
Python memo
python memo
Python memo
python memo
Python memo
Python memo
Python memo
2. Multivariate analysis spelled out in Python 8-2. K-nearest neighbor method [Weighting method] [Regression model]
[Python] Memo dictionary
python beginner memo (9.2-10)
python beginner memo (9.1)
★ Memo ★ Python Iroha
[Python] EDA memo
Python 3 operator memo
[My memo] python
Python3 metaclass memo
[Python] Basemap memo
Python beginner memo (2)
Johnson method (python)
[Python] Numpy memo
[Python] Semi-Lagrange method
Machine learning #k-nearest neighbor method and its implementation and various
Python class (Python learning memo ⑦)
My python environment memo
python openCV installation (memo)
Visualization memo by Python
Kernel Method with Python
Python installation method Windows
Python test package memo
[Python] Memo about functions
python regular expression memo
Binary search (python2.7) memo
[My memo] python -v / python -V
Python3 List / dictionary memo
[Memo] Python3 list sort
Python Tips (my memo)
[Python] Memo about errors
DynamoDB Script Memo (Python)
Simplex method (simplex method) in Python
Python basic memo --Part 2
python recipe book Memo
Private method in python
Basic Python command memo
Python OpenCV tutorial memo
Python basic grammar memo
[Machine learning] Write the k-nearest neighbor method (k-nearest neighbor method) in python by yourself and recognize handwritten numbers.
TensorFlow API memo (Python)
python useful memo links
Python decorator operation memo
Python basic memo --Part 1
Effective Python Memo Item 3
Divisor enumeration Python memo