安装相关的包
pip install scikit-learn
线性回归
逻辑回归
K-最近邻(KNN)
支持向量机(SVM)
决策树
随机森林
朴素贝叶斯
K-均值聚类
主成分分析(PCA)
和梯度提升(Gradient Boosting)
1 线性回归通常用于根据连续变量估计实际数值(房价、呼叫次数、总销售额等)。我们通过拟合最佳直线来建立自变量和因变量的关系。这条最佳直线叫做回归线,并且用 Y= a *X + b 这条线性等式来表示。
import numpy as np
from sklearn.linear_model import LogisticRegression
# 生成示例数据
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([0, 0, 0, 1, 1])
# 创建逻辑回归模型并拟合数据
model = LogisticRegression()
model.fit(X, y)
# 预测
y_pred = model.predict(X)
print("Predictions:", y_pred)
# 结果
# Predictions: [0 0 0 1 1]
2 将数据拟合进一个逻辑函数来预估一个事件出现的概率。因此,它也被叫做逻辑回归。
import numpy as np
from sklearn.linear_model import LogisticRegression
# 生成示例数据
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([0, 0, 0, 1, 1])
# 创建逻辑回归模型并拟合数据
model = LogisticRegression()
model.fit(X, y)
# 预测
y_pred = model.predict(X)
print("Predictions:", y_pred)
# 结果
# Predictions: [0 0 0 1 1]
3 通过周围k个案例中的大多数情况划分新的案例。根据一个距离函数,新案例会被分配到它的 K 个近邻中最普遍的类别中去。
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
# 生成示例数据
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([0, 0, 0, 1, 1])
# 创建KNN模型并拟合数据
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X, y)
# 预测
y_pred = model.predict(X)
print("Predictions:", y_pred)
# 结果
# Predictions: [0 0 0 1 1]
4 SVM 将每个数据在N维空间中用点标出(N是你所有的特征总数),每个特征的值是一个坐标的值。
import numpy as np
from sklearn.svm import SVC
# 生成示例数据
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([0, 0, 0, 1, 1])
# 创建SVM模型并拟合数据
model = SVC()
model.fit(X, y)
# 预测
y_pred = model.predict(X)
print("Predictions:", y_pred)
# 结果
# Predictions: [0 0 0 1 1]
5 将总体分成两个或更多的同类群。这是根据最重要的属性或者自变量来分成尽可能不同的组别。
import numpy as np
from sklearn.tree import DecisionTreeClassifier
# 生成示例数据
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([0, 0, 0, 1, 1])
# 创建决策树模型并拟合数据
model = DecisionTreeClassifier()
model.fit(X, y)
# 预测
y_pred = model.predict(X)
print("Predictions:", y_pred)
# 结果
# Predictions: [0 0 0 1 1]
6 在随机森林算法中,我们有一系列的决策树(因此又名“森林”)。为了根据一个新对象的属性将其分类,每一个决策树有一个分类,称之为这个决策树“投票”给该分类。这个森林选择获得森林里(在所有树中)获得票数最多的分类。
import numpy as np
from sklearn.ensemble import RandomForestClassifier
# 生成示例数据
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([0, 0, 0, 1, 1])
# 创建随机森林模型并拟合数据
model = RandomForestClassifier(n_estimators=10)
model.fit(X, y)
# 预测
y_pred = model.predict(X)
print("Predictions:", y_pred)
# 结果
# Predictions: [0 0 0 1 1]
7 就是一个分类器。 一个朴素贝叶斯分类器假设一个分类的特性与该分类的其它特性不相关。举个例子,如果一个水果又圆又红,并且直径大约是 3 英寸,那么这个水果可能会是苹果。即便这些特性互相依赖,或者依赖于别的特性的存在,朴素贝叶斯分类器还是会假设这些特性分别独立地暗示这个水果是个苹果。
import numpy as np
from sklearn.naive_bayes import GaussianNB
# 生成示例数据
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([0, 0, 0, 1, 1])
# 创建朴素贝叶斯模型并拟合数据
model = GaussianNB()
model.fit(X, y)
# 预测
y_pred = model.predict(X)
print("Predictions:", y_pred)
# 结果
# Predictions: [0 0 0 1 1]
8 使用 K – 均值算法来将一个数据归入一定数量的集群(假设有 k 个集群)的过程是简单的。一个集群内的数据点是均匀齐次的,并且异于别的集群。
import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
# 生成示例数据
X = np.array([[1], [2], [3], [4], [5]])
# 创建K-Means模型并拟合数据
model = KMeans(n_clusters=2)
model.fit(X)
# 预测
y_pred = model.predict(X)
print("Predictions:", y_pred)
# 结果
# Predictions: [1 1 1 0 0]
# 绘制结果
#plt.scatter(X, np.zeros_like(X), c=y_pred, cmap='viridis')
#plt.title("K-Means Clustering Example")
#plt.xlabel("X")
#plt.show()
9 PCA, Principal Component Analysis)是一种常用的数据降维技术,旨在将一组可能相关的变量转换为一组线性不相关的变量,称为主成分,同时尽可能多地保留原始数据集的信息。
import numpy as np
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
# 生成示例数据
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
# 创建PCA模型并拟合数据
pca = PCA(n_components=2)
X_r = pca.fit_transform(X)
print("Predictions:", X_r[:, 1])
# 结果
# Predictions: [-0. 0. 0. 0.]
# 绘制结果
#plt.scatter(X_r[:, 0], X_r[:, 1])
#plt.title("PCA Example")
#plt.xlabel("Principal Component 1")
#plt.ylabel("Principal Component 2")
#plt.show()
10 通过迭代地训练多个弱学习器(通常是决策树),并将它们组合成一个强学习器。梯度提升(Gradient Boosting)模型可以通过分析特征重要性来帮助我们理解数据中各个特征的相对重要程度。
import numpy as np
from sklearn.ensemble import GradientBoostingClassifier
# 生成示例数据
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([0, 0, 0, 1, 1])
# 创建梯度提升模型并拟合数据
model = GradientBoostingClassifier(n_estimators=10)
model.fit(X, y)
# 预测
y_pred = model.predict(X)
print("Predictions:", y_pred)
# 结果
# Predictions: [0 0 0 1 1]