python相关性分析特征过滤_Python相关性分析

导言机器学习的步骤

1)提出问题 :一切的机器学习目标都是为了解决生活或工作的实际问题

2)理解数据 :

采集数据(根据研究问题采集相关数据)

导入数据(数据从Excel、数据库、网络中导入到Phython的数据结构中)

查看数据集信息(包括描述统计信息,从整体上理解数据)

3)数据清洗(预处理):提取出我们想要的特征的信息

4)构建模型:用训练数据来构建,将第三步提取的特征放入机器学习算法中构建模型。机器学习中最核心的就是机器学习算法。

5)评估:对模型进行评估,通过测试数据来评估模型的准确性,来测试下模型的预测效果如何。机器学习中两个重要的概念

1)特征:数据的属性

2)标签:对数据的预测结果Python中用于机器学习的包:sklearn(包括了机器学习常用的算法)

Part I 简单线性回归

本部分需要了解的概念

实例

步骤1:导入工具包和数据集

#导入工具包

from collections import OrderedDict

import pandas as pd

#数据集

examDict={

'学习时间':[0.50,0.75,1.00,1.25,1.50,1.75,1.75,2.00,2.25,2.50,2.75,

3.00,3.25,3.50,4.00,4.25,4.50,4.75,5.00,5.50],

'分数':[10, 22, 13, 43, 20, 22, 33, 50, 62,48, 55,

75, 62, 73, 81, 76, 64, 82, 90, 93]

}

examOrderDict=OrderedDict(examDict)

examDf=pd.DataFrame(examOrderDict)

examDf.head() #查看

步骤2.提取特征和标签

#特征features

exam_X=examDf.loc[:,'学习时间']

#标签labes

exam_y=examDf.loc[:,'分数']

步骤3.绘制散点图

#绘制散点图

import matplotlib.pyplot as plt

#散点图

plt.scatter(exam_X, exam_y, color="blue", label="exam data")

#添加图标标签

plt.xlabel("Hours")

plt.ylabel("Score")

#显示图像

plt.show()

步骤4.建立训练数据和测试数据

#选取数据--利用train_test_split函数,随机从数据中按比例抽取训练数据和测试数据

from sklearn.model_selection import train_test_split

#建立训练数据和测试数据

X_train,X_test,y_train,y_test=train_test_split(exam_X,

exam_y,

train_size=.8)

'''代码解释:训练数据标签,测试数据标签,训练数据特征,测试数据特征=train_test_split函数(样本特征,样本标签,训练数据占比)'''

#输出数据大小

print('原始数据特征',exam_X.shape,

'训练数据特征',X_train.shape,

'测试数据特征',X_test.shape)

print('原始数据标签',exam_X.shape,

'训练数据标签',y_train.shape,

'测试数据标签',y_test.shape)

步骤5.绘制散点图

#导入工具包

import matplotlib.pyplot as plt

#散点图

plt.scatter(X_train, y_train, color="blue", label="train data")

plt.scatter(X_test, y_test, color="red", label="test data")

#添加图标标签

plt.legend(loc=2) #loc--location 图例的位置 ,loc=2就是upper left

plt.xlabel("Hours")

plt.ylabel("Score")

#显示图像

plt.show()

#计算相关系数:corr返回结果是一个数据框,存放的是相关系数矩阵

rDf=examDf.corr()

print('相关系数矩阵:')

rDf

!!! 运行报错问题解决

#导入线性回归

from sklearn.linear_model import LinearRegression

#构建模型:线性回归

model=LinearRegression()

# 训练模型

model.fit(X_train,y_train)

'''

ValueError: Expected 2D array, got 1D array instead:

array=[1.25 1.75 2.25 4. 1.5 3. 0.75 5. 2. 2.75 3.5 1. 4.5 2.5

4.25 0.5 ].

Reshape your data either using array.reshape(-1, 1) if your data has a single feature

or array.reshape(1, -1) if it contains a single sample.

'''

翻译:如果你输入的数据只有1个特征,需要用array.reshape(-1, 1)来改变数组的形状

#什么是arry.reshape(-1,1)

##定义2行*3列的数组

import numpy as np

aArr = np.array([

[1, 2, 3],

[5, 6, 7]

])

aArr.shape

输出结果: (2, 3)

##改变数组成为3行*2列

bArr=aArr.reshape(3,2)

bArr.shape

输出结果: (3, 2)

'''

reshape后面的参数是什么?

如果列的参数是-1,写成reshape(行数,-1),则会根据所给的行数,自动按照原始数组的大小形成一个新的数组,

如果列的参数是-1,写成reshape(-1,列数),则会根据所给的列数,自动按照原始数组的大小形成一个新的数组

'''

dArr=aArr.reshape(3,-1)

dArr.shape

dArr

输出结果:

array([[1, 2],

[3, 5],

[6, 7]])

dArr=aArr.reshape(-1,3)

dArr.shape

dArr

输出结果:

array([[1, 2, 3],

[5, 6, 7]])

!!!调整错误代码

#将训练数据特征转换成二维数组XX行*1列

X_train=X_train.values.reshape(-1,1)

#将测试数据特征转换成二维数组行数*1列

X_test=X_test.values.reshape(-1,1)

步骤6.训练模型-最佳拟合线

训练模型:训练数据的特征和标签--输入-->模型:线性回归--输出-->回归结果

'''

训练模型

'''

#第1步:导入线性回归

from sklearn.linear_model import LinearRegression

# 第2步:创建模型:线性回归

model = LinearRegression()

# 第3步:训练模型

model.fit(X_train,y_train)

'''

回归方程:y=a+bx

'''

#截距

a=model.intercept_

#回归系数

b=model.coef_

print('最佳拟合线:截距a=',a,'回归系数b=',b)

输出结果:

最佳拟合线:截距a= 5.936950904392781 回归系数b= [16.90129199]

'''

绘图

'''

import matplotlib.pyplot as plt

#训练数据散点图

plt.scatter(X_train, y_train, color='blue', label="train data")

plt.scatter(X_test, y_test, color='red', label="test data")

#训练数据的预测值

y_train_pred = model.predict(X_train)

#绘制最佳拟合线

plt.plot(X_train, y_train_pred, color='black', linewidth=3, label="best line")

#添加图标标签

plt.legend(loc=2)

plt.xlabel("Hours")

plt.ylabel("Score")

#显示图像

plt.show()

步骤7.模型评估

测试数据--输入-->模型:逻辑回归算法--输出--预测结果

#模型精准度评估--决定系数R²

model.score(X_test,y_test)

线性回归模型的决定系数R²≈97.6%,准确度很高。

Part II 逻辑回归逻辑回归其实不是回归算法,是一个分类算法,是一个用于二分分类的算法。二分分类是指我们的分类结果标签只有两个。

逻辑函数是逻辑回归的关键。逻辑函数y表示当分类的结果等于1时x对应的概率值。决策面的规则是当逻辑函数计算出的概率在0.5-1之间,则标签等于1;如果计算出的概率在0-0.5之间,则标签为0.

一般的,将原数据集随机分为80%的训练数据和20%的测试数据,简单的Python实现顺序如下:

训练数据的特征和标签--输入-->机器学习算法--输出-->决策面

测试数据--输入-->机器学习模型--输出-->预测结果---->正确率=正确分类个数/数据总数

练习:

步骤1 建立数据集

#导入工具包

from collections import OrderedDict

import pandas as pd

#数据集

examDict={

'学习时间':[0.50,0.75,1.00,1.25,1.50,1.75,1.75,2.00,2.25,2.50,

2.75,3.00,3.25,3.50,4.00,4.25,4.50,4.75,5.00,5.50],

'通过考试':[0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1]

}

examOrderDict=OrderedDict(examDict)

examDf=pd.DataFrame(examOrderDict)

examDf.head()

步骤2.二分分类--逻辑回归Python实现

2.1为什么不用线性回归

本案例的标签值只有0和1,属于二分分类,若用线性回归就会得到结果

逻辑回归的核心--逻辑函数

逻辑函数值得y的含义,如:

2.2Python实现

#提取特征和标签

#特征features

exam_X=examDf.loc[:,'学习时间']

#标签labes

exam_y=examDf.loc[:,'通过考试']

#查看图形

import matplotlib.pyplot as plt

#散点图

plt.scatter(exam_X, exam_y, color="b", label="exam data")

#添加图标标签

plt.xlabel("Hours")

plt.ylabel("Pass")

#显示图像

plt.show()

from sklearn.model_selection import train_test_split

#建立训练数据和测试数据

X_train , X_test , y_train , y_test = train_test_split(exam_X ,

exam_y ,

train_size = .8)

#输出数据大小

print('原始数据特征:',exam_X.shape,

',训练数据特征:', X_train.shape,

',测试数据特征:',X_test.shape )

print('原始数据标签:',exam_y.shape ,

'训练数据标签:', y_train.shape ,

'测试数据标签:' ,y_test.shape)

#绘制图像

import matplotlib.pyplot as plt

#散点图

plt.scatter(X_train, y_train, color="blue", label="train data")

plt.scatter(X_test, y_test, color="red", label="test data")

#添加图标标签

plt.legend(loc=2)

plt.xlabel("Hours")

plt.ylabel("Pass")

#显示图像

plt.show()

'''

和在线性回回归中一样,如果直接引用逻辑回归模型,会出现运行报错,

因为这里输入的特征只有1个,需要引用reshape改变数组形状

'''

import numpy as np

#将训练数据特征转换成二维数组XX行*1列

X_train=X_train.values.reshape(-1,1)

#将测试数据特征转换成二维数组行数*1列

X_test=X_test.values.reshape(-1,1)

#使用训练数据-->训练模型

#第1步:导入逻辑回归

from sklearn.linear_model import LogisticRegression

# 第2步:创建模型:逻辑回归

model = LogisticRegression()

#第3步:训练模型

model.fit(X_train , y_train)

步骤3.模型评估--准确率

model.score(X_test , y_test)

步骤4.预测应用

# 1.可以用model的predict_proba方法预测给定学习时间通过考试的概率

model.predict_proba(3)

# 2.使用模型的predict方法可以进行预测

pred=model.predict([[3]])

print(pred)

# 3.预测的概率值

##回归方程:z= + x 得到回归方程的z值

a=model.intercept_ #截距

b=model.coef_ #回归系数

x=3

z=a+b*x

##将z值带入逻辑回归函数中,得到概率值

y_pred=1/(1+np.exp(-z))

print('预测的概率值:',y_pred)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值