KNN
一个简单的用knn做癌症分型的题目
import numpy as np
import pandas as pd
from sklearn.datasets import load_breast_cancer
# 载入cancer, 是一个dict
cancer = load_breast_cancer()
# 分类X输入,y输出
X = cancerdf[cancer['feature_names']]
y = cancerdf['target']
# 样本选取
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 0)
# KNN 分类器
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors = 1)
knn.fit(X_train, y_train)
# 分类test group
predict_ = knn.predict(X_test)
score = knn.score(X_test, y_test)
KNN Regression
n_neighbors is the variable that decides how many neighbors to consider
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import train_test_split
knnreg = KNeighborsRegressor(n_neighbors = 5).fit(X_train, y_train)
predict = knnreg.predict(X_test)
### To plot the prediction based on different n_neighbors
fig, subaxes = plt.subplots(5, 1, figsize=(5,20))
X_predict_input = np.linspace(-3, 3, 500).reshape(-1,1)
X_train, X_test, y_train, y_test = train_test_split(X_R1, y_R1,
random_state = 0)
for thisaxis, K in zip(subaxes, [1, 3, 7, 15, 55]):
knnreg = KNeighborsRegressor(n_neighbors = K).fit(X_train, y_train)
y_predict_output = knnreg.predict(X_predict_input)
train_score = knnreg.score(X_train, y_train)
test_score = knnreg.score(X_test, y_test)
thisaxis.plot(X_predict_input, y_predict_output)
thisaxis.plot(X_train, y_train, 'o', alpha=0.9, label='Train')
thisaxis.plot(X_test, y_test, '^', alpha=0.9, label='Test')
thisaxis.set_xlabel('Input feature')
thisaxis.set_ylabel('Target value')
thisaxis.set_title('KNN Regression (K={})\n\
Train $R^2 = {:.3f}$, Test $R^2 = {:.3f}$'
.format(K, train_score, test_score))
thisaxis.legend()
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
Linear Regression
from sklearn.linear_model import LinearRegression
linreg = LinearRegression().fit(X_train, y_train)
linreg.coef_
linreg.intercept_
### Plot the line and the scatter plots
plt.figure(figsize=(5,4))
plt.scatter(X_R1, y_R1, marker= 'o', s=50, alpha=0.8)
plt.plot(X_R1, linreg.coef_ * X_R1 + linreg.intercept_, 'r-')
plt.title('Least-squares linear regression')
plt.xlabel('Feature value (x)')
plt.ylabel('Target value (y)')
plt.show()
Ridge/Lasso Regression
Feature normalization is important to ensure each feature is weight equally in the model. Alpha is used to adjust the degree of regularization. Higher Alpha means more regularized model.
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
from sklearn.linear_model import Ridge
X_train, X_test, y_train, y_test = train_test_split(X_crime, y_crime,
random_state = 0)
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
linridge = Ridge(alpha=20.0).fit(X_train_scaled, y_train)
from sklearn.linear_model import Lasso
from sklearn.preprocessing import MinMaxScaler
linlasso = Lasso(alpha=2.0, max_iter = 10000).fit(X_train_scaled, y_train)
Polynomial Regression
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(degree=2)
X_F1_poly = poly.fit_transform(X_F1)
X_train, X_test, y_train, y_test = train_test_split(X_F1_poly, y_F1,
random_state = 0)
linreg = LinearRegression().fit(X_train, y_train)
Logistic Regression
C is the degree of regularization. Larger C means less regularization (more specific).
from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(C=100).fit(X_train, y_train)
Support Vector Machine
from sklearn.svm import SVC
clf = SVC(kernel = 'linear', C=this_C).fit(X_train, y_train)
from sklearn.svm import LinearSVC
clf = LinearSVC(C=this_C).fit(X_train, y_train)
Kernelized
Gamma is a variable in kernelized mode. Larger gamma means points have to be very close to be similar.
clf = SVC(kernel = 'rbf', gamma=this_gamma).fit(X_train, y_train)
Cross-Validation
Use different training sets to train the data
from sklearn.model_selection import cross_val_score
clf = KNeighborsClassifier(n_neighbors = 5)
# Default validation sets is 3
cv_scores = cross_val_score(clf, X, y)
from sklearn.model_selection import validation_curve
train_scores, test_scores = validation_curve(SVC(), X, y,
param_name='gamma',
param_range=param_range, cv=3)
Decision Tree
Can control the tree’s depth and nodes by adjusting the parameter.
clf2 = DecisionTreeClassifier(max_depth = 3).fit(X_train, y_train)