热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

逻辑回归(Logistic+Regression)经典实例

机器学习算法完整版见fenghaootong-github房价预测数据集描述数据共有81个特征SalePrice-theproperty’ssalepriceindollars.T

机器学习算法完整版见fenghaootong-github

房价预测

数据集描述

数据共有81个特征

SalePrice - the property’s sale price in dollars. This is the target variable that you’re trying to predict.
MSSubClass: The building class
MSZoning: The general zoning classification
LotFrontage: Linear feet of street connected to property
LotArea: Lot size in square feet
Street: Type of road access
Alley: Type of alley access
LotShape: General shape of property
LandContour: Flatness of the property
Utilities: Type of utilities available
LotConfig: Lot configuration
LandSlope: Slope of property
….

导入所需模块

import numpy as np
import pandas as pd import matplotlib.pyplot as plt
import seaborn as sns
import math as matfrom scipy import stats
from scipy.stats import norm
from sklearn import preprocessingimport statsmodels.api as sm
from patsy import dmatricesimport warnings
warnings.filterwarnings('ignore')
%matplotlib inlineimport sklearn.linear_model as LinReg
import sklearn.metrics as metrics

导入数据

#loading the data
data_train = pd.read_csv('../DATA/SalePrice_train.csv')
data_test = pd.read_csv('../DATA/SalePrice_test.csv')

数据共有81个特征,为了便于说明只挑选7个特征
OverallQual
GrLivArea
GarageCars
TotalBsmtSF
1stFlrSF
FullBath
YearBuilt
因为这些数据与房子的售卖价格相关性比较大

具体如何选择特征,见数据清理

数据预处理

data_train.shape

(1460, 81)

vars = ['OverallQual', 'GrLivArea', 'GarageCars', 'TotalBsmtSF', 'FullBath','YearBuilt']
Y = data_train[['SalePrice']] #dim (1460, 1)
ID_train = data_train[['Id']] #dim (1460, 1)
ID_test = data_test[['Id']] #dim (1459, 1)
#extract only the relevant feature with cross correlation >0.5 respect to SalePrice
X_matrix = data_train[vars]
X_matrix.shape #dim (1460,6)X_test = data_test[vars]
X_test.shape #dim (1459,6)

(1459, 6)

查看丢失数据

#check for missing data:
#missing data
total = X_matrix.isnull().sum().sort_values(ascending=False)
percent = (X_matrix.isnull().sum()/X_matrix.count()).sort_values(ascending=False)
missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
missing_data.head(20)
#no missing data in this training set

TotalPercent
YearBuilt00.0
FullBath00.0
TotalBsmtSF00.0
GarageCars00.0
GrLivArea00.0
OverallQual00.0

total = X_test.isnull().sum().sort_values(ascending=False)
percent = (X_test.isnull().sum()/X_test.count()).sort_values(ascending=False)
missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
missing_data.head(20)
#missing data in this test set

TotalPercent
TotalBsmtSF10.000686
GarageCars10.000686
YearBuilt00.000000
FullBath00.000000
GrLivArea00.000000
OverallQual00.000000

#help(mat.ceil) #去上限

使用均值代替缺失的数据

#使用均值代替缺失的数据
X_test['TotalBsmtSF'] = X_test['TotalBsmtSF'].fillna(X_test['TotalBsmtSF'].mean())
X_test['GarageCars'] = X_test['GarageCars'].fillna(mat.ceil(X_test['GarageCars'].mean()))total = X_test.isnull().sum().sort_values(ascending=False)
percent = (X_test.isnull().sum()/X_test.count()).sort_values(ascending=False)
missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
missing_data.head(20)

TotalPercent
YearBuilt00.0
FullBath00.0
TotalBsmtSF00.0
GarageCars00.0
GrLivArea00.0
OverallQual00.0

X_test.shape

(1459, 6)

  • 然后预处理模块的特征缩放和均值归一化。 进一步提供了一个实用类StandardScaler,它实现了变换方法来计算训练集上的均值和标准差,以便稍后能够在测试集上重新应用相同的变换。

max_abs_scaler = preprocessing.MaxAbsScaler()
X_train_maxabs = max_abs_scaler.fit_transform(X_matrix)
print(X_train_maxabs)

[[ 0.7 0.30308401 0.5 0.1400982 0.66666667 0.99651741] [ 0.6 0.22367955 0.5 0.20654664 0.66666667 0.98308458] [ 0.7 0.31655441 0.5 0.15057283 0.66666667 0.99552239] ..., [ 0.7 0.41474654 0.25 0.18854337 0.66666667 0.96567164][ 0.5 0.191067 0.25 0.17643208 0.33333333 0.97014925] [ 0.5 0.22261609 0.25 0.20556465 0.33333333 0.97761194]]

X_test_maxabs = max_abs_scaler.fit_transform(X_test)
print(X_test_maxabs)

[[ 0.5 0.17585868 0.2 0.17311089 0.25 0.97562189] [ 0.6 0.26084396 0.2 0.26084396 0.25 0.97412935] [ 0.5 0.31972522 0.4 0.18213935 0.5 0.99353234] ..., [ 0.5 0.24023553 0.4 0.24023553 0.25 0.97512438] [ 0.5 0.19038273 0. 0.17899902 0.25 0.99104478][ 0.7 0.39254171 0.6 0.19548577 0.5 0.99154229]]

模型训练

lr=LinReg.LinearRegression().fit(X_train_maxabs,Y)

模型预测

Y_pred_train = lr.predict(X_train_maxabs)
print("Los Reg performance evaluation on Y_pred_train")
print("R-squared =", metrics.r2_score(Y, Y_pred_train))

Los Reg performance evaluation on Y_pred_train
R-squared = 0.768647335422

Y_pred_test = lr.predict(X_test_maxabs)
print("Lin Reg performance evaluation on X_test")
#print("R-squared =", metrics.r2_score(Y, Y_pred_test))
print("Coefficients =", lr.coef_)

Lin Reg performance evaluation on X_test
Coefficients = [[ 205199.68775757 305095.8264889 58585.26325362 178302.68126933-16511.92112734 676458.9666186 ]]

Logistic Regression

导入模块

#导入模块
import pandas as pd
import numpy as np

数据预处理

#创建特征列表表头
column_names = ['Sample code number','Clump Thickness','Uniformity of Cell Size','Uniformity of Cell Shape','Marginal Adhesion','Single Epithelial Cell Size','Bare Nuclei','Bland Chromatin','Normal Nucleoli','Mitoses','Class']
#使用pandas.read_csv函数从网上读取数据集
data = pd.read_csv('DATA/data.csv',names=column_names)
#将?替换为标准缺失值表示
data = data.replace(to_replace='?',value = np.nan)
#丢弃带有缺失值的数据(只要有一个维度有缺失便丢弃)
data = data.dropna(how='any')
#查看data的数据量和维度
data.shape

(683, 11)

data.head(10)

Sample code numberClump ThicknessUniformity of Cell SizeUniformity of Cell ShapeMarginal AdhesionSingle Epithelial Cell SizeBare NucleiBland ChromatinNormal NucleoliMitosesClass
010000255111213112
1100294554457103212
210154253111223112
310162776881343712
410170234113213112
510171228101087109714
6101809911112103112
710185612121213112
810330782111211152
910330784211212112

由于原始数据没有提供对应的测试样本用于评估模型性能,这里对带标记的数据进行分割,25%作为测试集,其余作为训练集

#使用sklearn.cross_validation里的train_test_split模块分割数据集
from sklearn.cross_validation import train_test_split
#随机采样25%的数据用于测试,剩下的75%用于构建训练集
X_train,X_test,y_train,y_test = train_test_split(data[column_names[1:10]],data[column_names[10]],test_size = 0.25,random_state = 33)
#查看训练样本的数量和类别分布
y_train.value_counts()

2 344
4 168
Name: Class, dtype: int64

#查看测试样本的数量和类别分布
y_test.value_counts()

2 100
4 71
Name: Class, dtype: int64

建立模型,预测数据

#从sklearn.preprocessing导入StandardScaler
from sklearn.preprocessing import StandardScaler
#从sklearn.linear_model导入LogisticRegression(逻辑斯蒂回归)
from sklearn.linear_model import LogisticRegression
#从sklearn.linear_model导入SGDClassifier(随机梯度参数)
from sklearn.linear_model import SGDClassifier

ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)

lr = LogisticRegression()
#调用逻辑斯蒂回归,使用fit函数训练模型参数
lr.fit(X_train,y_train)
lr_y_predict = lr.predict(X_test)
#调用随机梯度的fit函数训练模型

lr_y_predict

array([2, 2, 4, 4, 2, 2, 2, 4, 2, 2, 2, 2, 4, 2, 4, 4, 4, 4, 4, 2, 2, 4, 4,2, 4, 4, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 2, 4, 2, 2,4, 2, 2, 4, 4, 2, 2, 2, 4, 2, 2, 2, 2, 2, 4, 4, 2, 2, 2, 4, 2, 2, 2,2, 4, 2, 2, 2, 2, 2, 2, 4, 4, 2, 2, 2, 4, 2, 2, 2, 4, 2, 4, 2, 4, 4,2, 2, 2, 2, 4, 4, 2, 2, 2, 4, 2, 2, 4, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4, 2, 2, 4, 4, 2, 4, 2, 2, 2, 4, 2, 2, 4, 4, 2, 4, 4, 2, 2, 2,2, 4, 2, 4, 2, 4, 2, 2, 2, 2, 2, 4, 4, 2, 4, 4, 2, 4, 2, 2, 2, 2, 4,4, 4, 2, 4, 2, 2, 4, 2, 4, 4], dtype=int64)

使用线性分类模型进行良/恶性肿瘤预测任务的性能分析

#从sklearn.metrics导入classification_report
from sklearn.metrics import classification_report#使用逻辑斯蒂回归模型自带的评分函数score获得模型在测试集上的准确性结果
print('Accuracy of LR Classifier:',lr.score(X_test,y_test))
#使用classification_report模块获得逻辑斯蒂模型其他三个指标的结果(召回率,精确率,调和平均数)
print(classification_report(y_test,lr_y_predict,target_names=['Benign','Malignant']))

Accuracy of LR Classifier: 0.988304093567precision recall f1-score supportBenign 0.99 0.99 0.99 100Malignant 0.99 0.99 0.99 71avg / total 0.99 0.99 0.99 171


转:https://www.cnblogs.com/htfeng/p/9931758.html



推荐阅读
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文将介绍如何使用 Go 语言编写和运行一个简单的“Hello, World!”程序。内容涵盖开发环境配置、代码结构解析及执行步骤。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文详细探讨了Java中的24种设计模式及其应用,并介绍了七大面向对象设计原则。通过创建型、结构型和行为型模式的分类,帮助开发者更好地理解和应用这些模式,提升代码质量和可维护性。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文详细介绍了Java中org.w3c.dom.Text类的splitText()方法,通过多个代码示例展示了其实际应用。该方法用于将文本节点在指定位置拆分为两个节点,并保持在文档树中。 ... [详细]
  • C++实现经典排序算法
    本文详细介绍了七种经典的排序算法及其性能分析。每种算法的平均、最坏和最好情况的时间复杂度、辅助空间需求以及稳定性都被列出,帮助读者全面了解这些排序方法的特点。 ... [详细]
  • 本文介绍如何利用动态规划算法解决经典的0-1背包问题。通过具体实例和代码实现,详细解释了在给定容量的背包中选择若干物品以最大化总价值的过程。 ... [详细]
  • CentOS7源码编译安装MySQL5.6
    2019独角兽企业重金招聘Python工程师标准一、先在cmake官网下个最新的cmake源码包cmake官网:https:www.cmake.org如此时最新 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • ImmutableX Poised to Pioneer Web3 Gaming Revolution
    ImmutableX is set to spearhead the evolution of Web3 gaming, with its innovative technologies and strategic partnerships driving significant advancements in the industry. ... [详细]
  • 本文介绍了如何在C#中启动一个应用程序,并通过枚举窗口来获取其主窗口句柄。当使用Process类启动程序时,我们通常只能获得进程的句柄,而主窗口句柄可能为0。因此,我们需要使用API函数和回调机制来准确获取主窗口句柄。 ... [详细]
author-avatar
帅帅考拉_955
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有