赞
踩
目录
1.2 逻辑回归(Logistic Regression):
1.4 支持向量机(Support Vector Machine):
2.3 K均值聚类(K-means Clustering):
3.3 AdaBoost(Adaptive Boosting):
- 线性回归(Linear Regression):用于建模连续变量之间的线性关系。示例:预测房屋价格。
- 逻辑回归(Logistic Regression):用于建模二分类问题。示例:判断一封电子邮件是垃圾邮件还是正常邮件。
- 决策树(Decision Tree):通过构建树形结构进行分类或回归。示例:预测购买某个产品的用户。
- 支持向量机(Support Vector Machine):通过找到一个最优的超平面将数据分类。示例:预测肿瘤是否为恶性。
- 随机森林(Random Forest):基于多个决策树的集成算法,通过投票方式进行预测。示例:预测客户是否流失。
详解:线性回归用于建立连续变量之间的线性关系模型。
示例代码:
- from sklearn.linear_model import LinearRegression
-
- # 准备训练数据
- X_train = [[1], [2], [3], [4], [5]] # 自变量的训练数据
- y_train = [2, 4, 6, 8, 10] # 因变量的训练数据
-
- # 创建模型对象
- model = LinearRegression()
-
- # 拟合模型
- model.fit(X_train, y_train)
-
- # 预测
- X_test = [[6], [7], [8]] # 自变量的测试数据
- y_pred = model.predict(X_test) # 预测因变量
-
- # 输出预测结果
- print("预测结果:", y_pred)

- from sklearn.linear_model import LogisticRegression
-
- # 准备训练数据
- X_train = [[1, 2], [2, 1], [2, 3], [4, 5]] # 自变量的训练数据
- y_train = [0, 0, 1, 1] # 因变量的训练数据
-
- # 创建模型对象
- model = LogisticRegression()
-
- # 拟合模型
- model.fit(X_train, y_train)
-
- # 预测
- X_test = [[3, 4], [1, 1], [5, 6]] # 自变量的测试数据
- y_pred = model.predict(X_test) # 预测因变量
-
- # 输出预测结果
- print("预测结果:", y_pred)

- from sklearn.tree import DecisionTreeClassifier
-
- # 准备训练数据
- X_train = [[1, 2], [2, 1], [2, 3], [4, 5]] # 自变量的训练数据
- y_train = [0, 0, 1, 1] # 因变量的训练数据
-
- # 创建模型对象
- model = DecisionTreeClassifier()
-
- # 拟合模型
- model.fit(X_train, y_train)
-
- # 预测
- X_test = [[3, 4], [1, 1], [5, 6]] # 自变量的测试数据
- y_pred = model.predict(X_test) # 预测因变量
-
- # 输出预测结果
- print("预测结果:", y_pred)

- from sklearn.svm import SVC
-
- # 准备训练数据
- X_train = [[1, 2], [2, 1], [2, 3], [4, 5]] # 自变量的训练数据
- y_train = [0, 0, 1, 1] # 因变量的训练数据
-
- # 创建模型对象
- model = SVC()
-
- # 拟合模型
- model.fit(X_train, y_train)
-
- # 预测
- X_test = [[3, 4], [1, 1], [5, 6]] # 自变量的测试数据
- y_pred = model.predict(X_test) # 预测因变量
-
- # 输出预测结果
- print("预测结果:", y_pred)

随机森林(Random Forest)是一种基于决策树的集成学习算法,它通过训练多个决策树并集成它们的预测结果来提高模型的准确性和泛化能力。
- 随机森林由多颗决策树组成,每颗树都是独立而相互不相关的。
- 每颗决策树在构建时,会从原始训练集中进行有放回地随机采样(bootstrap),形成一个新的训练集,并使用该训练集构建决策树。
- 在构建决策树的过程中,对于每个节点的特征选择,随机森林会从所有特征中随机选取一部分特征。
- 最后,随机森林通过集成所有决策树的预测结果,使用投票或平均的方式进行分类或回归。
示例代码:
- from sklearn.ensemble import RandomForestClassifier
- from sklearn.datasets import load_iris
- from sklearn.model_selection import train_test_split
- from sklearn.metrics import accuracy_score
-
- # 加载数据集
- data = load_iris()
- X = data.data
- y = data.target
-
- # 划分训练集和测试集
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
-
- # 创建随机森林模型
- model = RandomForestClassifier(n_estimators=100, random_state=42)
-
- # 训练模型
- model.fit(X_train, y_train)
-
- # 预测
- y_pred = model.predict(X_test)
-
- # 评估模型
- accuracy = accuracy_score(y_test, y_pred)
- print("准确度:", accuracy)

- 聚类算法(Clustering):将相似的数据点聚集到一起。示例:对市场细分进行分析。
- 主成分分析(PCA):通过线性变换将高维数据降维至低维空间。示例:图像识别中的特征提取。
- K均值聚类(K-means Clustering):将数据分成K个簇,使得每个数据点属于最近的簇。示例:对顾客进行细分。
- from sklearn.cluster import KMeans
- from sklearn.datasets import make_blobs
-
- # 创建示例数据
- X, y = make_blobs(n_samples=100, centers=3, random_state=42)
-
- # 创建聚类模型
- model = KMeans(n_clusters=3, random_state=42)
-
- # 进行聚类
- y_pred = model.fit_predict(X)
-
- # 输出聚类结果
- print("聚类结果:", y_pred)
- from sklearn.decomposition import PCA
- from sklearn.datasets import load_iris
-
- # 加载鸢尾花数据集
- data = load_iris()
- X = data.data
-
- # 创建PCA模型
- model = PCA(n_components=2)
-
- # 进行主成分分析
- X_pca = model.fit_transform(X)
-
- # 输出降维后的数据
- print("降维后的数据:", X_pca)
- from sklearn.cluster import KMeans
- from sklearn.datasets import make_blobs
-
- # 创建示例数据
- X, y_true = make_blobs(n_samples=300, centers=4, random_state=42)
-
- # 创建K均值聚类模型
- model = KMeans(n_clusters=4, random_state=42)
-
- # 进行聚类
- model.fit(X)
-
- # 获取聚类标签
- y_pred = model.labels_
-
- # 输出聚类结果
- print("聚类结果:", y_pred)

随机森林(Random Forest):通过多个决策树的集成进行分类或回归。示例:预测房价
梯度提升树(Gradient Boosting):通过训练多个弱模型,并逐步拟合残差来优化整体模型。示例:预测销售额。
AdaBoost(Adaptive Boosting):通过逐步调整样本权重,训练多个弱分类器并加权投票。示例:人脸识别。
- from sklearn.ensemble import RandomForestRegressor
- from sklearn.datasets import fetch_california_housing
- from sklearn.model_selection import train_test_split
- from sklearn.metrics import mean_squared_error
-
- # 加载数据集
- data = fetch_california_housing()
- X = data.data
- y = data.target
-
- # 划分训练集和测试集
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
-
- # 创建随机森林模型
- model = RandomForestRegressor(n_estimators=100, random_state=42)
-
- # 训练模型
- model.fit(X_train, y_train)
-
- # 预测
- y_pred = model.predict(X_test)
-
- # 评估模型
- mse = mean_squared_error(y_test, y_pred)
- print("均方误差:", mse)

- from sklearn.ensemble import GradientBoostingRegressor
- from sklearn.datasets import fetch_california_housing
- from sklearn.model_selection import train_test_split
- from sklearn.metrics import mean_squared_error
-
- # 加载数据集
- data = fetch_california_housing()
- X = data.data
- y = data.target
-
- # 划分训练集和测试集
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
-
- # 创建梯度提升树模型
- model = GradientBoostingRegressor(n_estimators=100, random_state=42)
-
- # 训练模型
- model.fit(X_train, y_train)
-
- # 预测
- y_pred = model.predict(X_test)
-
- # 评估模型
- mse = mean_squared_error(y_test, y_pred)
- print("均方误差:", mse)

- from sklearn.ensemble import AdaBoostClassifier
- from sklearn.datasets import fetch_olivetti_faces
- from sklearn.model_selection import train_test_split
- from sklearn.metrics import accuracy_score
-
- # 加载数据集
- data = fetch_olivetti_faces()
- X = data.data
- y = data.target
-
- # 划分训练集和测试集
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
-
- # 创建AdaBoost模型
- model = AdaBoostClassifier(n_estimators=100, random_state=42)
-
- # 训练模型
- model.fit(X_train, y_train)
-
- # 预测
- y_pred = model.predict(X_test)
-
- # 评估模型
- accuracy = accuracy_score(y_test, y_pred)
- print("准确度:", accuracy)

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。