当前位置: 首页 > wzjs >正文

3000元网站建设三年申京效率值联盟第一

3000元网站建设三年,申京效率值联盟第一,软件技术 网站建设教程,手机体验网站模型名称特点适用场景sklearn 类逻辑回归 (Logistic Regression)线性分类,输出概率,可正则化防止过拟合二分类/多分类,特征线性可分linear_model.LogisticRegression感知机 (Perceptron)简单线性分类器,无概率输出线性可分数据lin…
模型名称特点适用场景sklearn 类
逻辑回归 (Logistic Regression)线性分类,输出概率,可正则化防止过拟合二分类/多分类,特征线性可分linear_model.LogisticRegression
感知机 (Perceptron)简单线性分类器,无概率输出线性可分数据linear_model.Perceptron

一.逻辑回归

# 导入逻辑回归模型
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, f1_score, roc_auc_score, confusion_matrix, classification_report
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score, KFold
from sklearn.preprocessing import StandardScaler
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from scipy import stats# 设置显示选项
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
pd.set_option('display.max_rows', None)    # 显示最大行数
pd.set_option('display.max_columns', None)  # 显示最大列数
pd.set_option('display.max_colwidth', None)  # 显示的最大列宽
pd.set_option('display.width', None)  # 显示的最宽度# 导入数据
data = pd.read_excel("股票客户流失.xlsx")# 数据预处理
# 4.1 使用均值填写缺失值
print("缺失值统计:\n", data.isnull().sum())
data = data.apply(lambda col: col.fillna(col.mean()), axis=0)# 4.2 处理异常值
numeric_data = data.select_dtypes(include=[np.number])
z_scores = np.abs(stats.zscore(data.select_dtypes(include=[np.number])))  # 仅对数值型数据计算 Z-score
threshold = 3  # Z-score 阈值 3个标准差
outliers = (z_scores > threshold).any(axis=1)  # 检测异常值
print("检测到的异常值行索引:\n", data[outliers].index.tolist())  # 输出异常值的行索引
print(data[outliers])
data = data[~outliers]  # 移除异常值# 划分特征和目标变量
X = data.drop("是否流失", axis=1)
y = data["是否流失"]# 4.3 将数据划分为训练集与测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)# 4.4 创建标准化训练集与测试集
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)# 建立逻辑回归模型
model = LogisticRegression(max_iter=1000, random_state=42)# 参数网格
param_grid = {'C': [0.01, 0.1, 1, 10, 100],  # 正则化强度的倒数'penalty': ['l1', 'l2'],  # 正则化类型'solver': ['liblinear']  # 适用于小数据集的优化算法
}# 网格搜索
grid_search = GridSearchCV(estimator=model,param_grid=param_grid,cv=KFold(n_splits=5, random_state=42, shuffle=True),scoring='accuracy',  # 使用准确率作为评估指标n_jobs=-1
)
grid_search.fit(X_train, y_train)# 输出最佳参数组合
print("最佳参数组合:", grid_search.best_params_)# 使用最佳模型进行预测
best_model = grid_search.best_estimator_
y_pred = best_model.predict(X_test)# 输出测试集的评估指标
print("测试集准确率:", accuracy_score(y_test, y_pred))
print("测试集 F1 Score:", f1_score(y_test, y_pred))
print("测试集 ROC AUC Score:", roc_auc_score(y_test, y_pred))
print("混淆矩阵:\n", confusion_matrix(y_test, y_pred))
print("分类报告:\n", classification_report(y_test, y_pred))# 交叉验证
cv_scores = cross_val_score(best_model, X_train, y_train, cv=KFold(n_splits=5, random_state=42, shuffle=True), scoring='accuracy')
print(f"5折交叉验证准确率: {np.mean(cv_scores):.4f} (±{np.std(cv_scores):.4f})")

二.感知机

# 导入感知机模型
from sklearn.linear_model import Perceptron
from sklearn.metrics import accuracy_score, f1_score, roc_auc_score, confusion_matrix, classification_report
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score, KFold
from sklearn.preprocessing import StandardScaler
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from scipy import stats# 设置显示选项
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
pd.set_option('display.max_rows', None)    # 显示最大行数
pd.set_option('display.max_columns', None)  # 显示最大列数
pd.set_option('display.max_colwidth', None)  # 显示的最大列宽
pd.set_option('display.width', None)  # 显示的最宽度# 导入数据
data = pd.read_excel("股票客户流失.xlsx")# 数据预处理
# 4.1 使用均值填写缺失值
print("缺失值统计:\n", data.isnull().sum())
data = data.apply(lambda col: col.fillna(col.mean()), axis=0)# 4.2 处理异常值
numeric_data = data.select_dtypes(include=[np.number])
z_scores = np.abs(stats.zscore(data.select_dtypes(include=[np.number])))  # 仅对数值型数据计算 Z-score
threshold = 3  # Z-score 阈值 3个标准差
outliers = (z_scores > threshold).any(axis=1)  # 检测异常值
print("检测到的异常值行索引:\n", data[outliers].index.tolist())  # 输出异常值的行索引
print(data[outliers])
data = data[~outliers]  # 移除异常值# 划分特征和目标变量
X = data.drop("是否流失", axis=1)
y = data["是否流失"]# 4.3 将数据划分为训练集与测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)# 4.4 创建标准化训练集与测试集
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)# 建立感知机模型
model = Perceptron(random_state=42)# 参数网格
param_grid = {'alpha': [0.0001, 0.001, 0.01, 0.1],  # 正则化参数'max_iter': [100, 500, 1000],  # 最大迭代次数'eta0': [0.01, 0.1, 1.0],  # 学习率'penalty': ['l1', 'l2', 'elasticnet', None]  # 正则化类型
}# 网格搜索
grid_search = GridSearchCV(estimator=model,param_grid=param_grid,cv=KFold(n_splits=5, random_state=42, shuffle=True),scoring='accuracy',  # 使用准确率作为评估指标n_jobs=-1
)
grid_search.fit(X_train, y_train)# 输出最佳参数组合
print("最佳参数组合:", grid_search.best_params_)# 使用最佳模型进行预测
best_model = grid_search.best_estimator_
y_pred = best_model.predict(X_test)# 输出测试集的评估指标
print("测试集准确率:", accuracy_score(y_test, y_pred))
print("测试集 F1 Score:", f1_score(y_test, y_pred))
print("测试集 ROC AUC Score:", roc_auc_score(y_test, y_pred))
print("混淆矩阵:\n", confusion_matrix(y_test, y_pred))
print("分类报告:\n", classification_report(y_test, y_pred))# 交叉验证
cv_scores = cross_val_score(best_model, X_train, y_train, cv=KFold(n_splits=5, random_state=42, shuffle=True), scoring='accuracy')
print(f"5折交叉验证准确率: {np.mean(cv_scores):.4f} (±{np.std(cv_scores):.4f})")

http://www.dtcms.com/wzjs/513942.html

相关文章:

  • 企业新网站seo推广识图
  • 自己的网站做app河南郑州网站推广优化外包
  • 南京电商网站建设公司排名免费发布产品的网站
  • 怎样免费建一个网站今天热搜榜前十名
  • 网站建设怎么用在线资源搜索引擎
  • 新闻网站备案的前置条件女生学电子商务好吗
  • 手机网站 英文搜索引擎的优化方法有哪些
  • 兄弟网络(西安网站建设制作公司)爱站网长尾关键词挖掘工具
  • wordpress建站购买营销策划的六个步骤
  • 武汉站哪家设计公司什么是网络推广
  • 建视频网站我想做网络推广找谁
  • 平度网站建设公司做销售找客户渠道
  • 如何使用家里电脑做网站服务器销售网站
  • 公司做网站费用记到哪个科目购物网站制作
  • ui培训班多少钱公众号排名优化
  • 电商网站开发总结与感受软文代写服务
  • 做视频网站需要什么空间吗网络广告有哪些形式
  • 广州开发区人才交流服务中心刷关键词优化排名
  • 可以用腾讯企业邮箱域名做网站百度seo优化工具
  • 重庆网站备案大厅搜狗网页版入口
  • 电子网站搜索引擎怎么做关键词优化排名哪家好
  • 做html网站模板下载地址网站推广公司
  • 网站建设的技术难点西安网站建设方案优化
  • wordpress网站迁移教程北京搜索引擎推广服务
  • 网站建设的价值是什么seo优化的主要内容
  • 代理注册公司收费标准淘宝关键词优化怎么弄
  • 餐饮网站建设需求分析免费软文发布平台有哪些
  • 做lgoo的网站一般有哪些各大引擎搜索入口
  • 香港做批发的网站肇庆seo外包公司
  • 网站建设外包包含内容十大免费网站推广平台有哪些