当前位置:网站首页>机器学习实战-逻辑回归-19
机器学习实战-逻辑回归-19
2022-07-28 11:51:00 【gemoumou】
机器学习实战-逻辑回归-用户流失预测



import numpy as np
train_data = np.genfromtxt('Churn-Modelling.csv',delimiter=',',dtype=np.str)
test_data = np.genfromtxt('Churn-Modelling-Test-Data.csv',delimiter=',',dtype=np.str)
x_train = train_data[1:,:-1]
y_train = train_data[1:,-1].astype(int)
x_test = test_data[1:,:-1]
y_test = test_data[1:,-1].astype(int)
x_train = np.delete(x_train,[0,1,2],axis=1)
x_test = np.delete(x_test,[0,1,2],axis=1)
x_train[:5]

y_train[:5]

# x_train[x_train=='Female'] = 0
# x_train[x_train=='Male'] = 1
from sklearn.preprocessing import LabelEncoder
labelencoder1 = LabelEncoder()
x_train[:,1] = labelencoder1.fit_transform(x_train[:,1])
x_test[:,1] = labelencoder1.transform(x_test[:,1])
labelencoder2 = LabelEncoder()
x_train[:,2] = labelencoder2.fit_transform(x_train[:,2])
x_test[:,2] = labelencoder2.transform(x_test[:,2])

x_train = x_train.astype(np.float32)
x_test = x_test.astype(np.float32)
y_train = y_train.astype(np.float32)
y_test = y_test.astype(np.float32)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)

from sklearn.linear_model import LinearRegression
from sklearn.metrics import classification
LR = LinearRegression()
LR.fit(x_train,y_train)
predictions = LR.predict(x_test)
print(classification_report(y_test, predictions))

机器学习实战-逻辑回归-糖尿病预测模型


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# 载入数据
diabetes_data = pd.read_csv('diabetes.csv')
diabetes_data.head()

# 数据信息
diabetes_data.info(verbose=True)

# 数据描述
diabetes_data.describe()

# 数据形状
diabetes_data.shape

# 查看标签分布
print(diabetes_data.Outcome.value_counts())
# 使用柱状图的方式画出标签个数统计
p=diabetes_data.Outcome.value_counts().plot(kind="bar")
plt.show()

# 可视化数据分布
p=sns.pairplot(diabetes_data, hue = 'Outcome')
plt.show()

这里画的图主要是两种类型,直方图和散点图。单一特征对比的时候用的是直方图,不同特征对比的时候用的是散点图,显示两个特征的之间的关系。观察数据分布我们可以发现一些异常值,比如Glucose葡萄糖,BloodPressure血压,SkinThickness皮肤厚度,Insulin胰岛素,BMI身体质量指数这些特征应该是不可能出现0值的。
# 把葡萄糖,血压,皮肤厚度,胰岛素,身体质量指数中的0替换为nan
colume = ['Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI']
diabetes_data[colume] = diabetes_data[colume].replace(0,np.nan)
# pip install missingno
import missingno as msno
p=msno.bar(diabetes_data)
plt.show()

# 设定阀值
thresh_count = diabetes_data.shape[0]*0.8
# 若某一列数据缺失的数量超过20%就会被删除
diabetes_data = diabetes_data.dropna(thresh=thresh_count, axis=1)
p=msno.bar(diabetes_data)
plt.show()

# 导入插补库
from sklearn.preprocessing import Imputer
# 对数值型变量的缺失值,我们采用均值插补的方法来填充缺失值
imr = Imputer(missing_values='NaN', strategy='mean', axis=0)
colume = ['Glucose', 'BloodPressure', 'BMI']
# 进行插补
diabetes_data[colume] = imr.fit_transform(diabetes_data[colume])
p=msno.bar(diabetes_data)
plt.show()

plt.figure(figsize=(12,10))
# 画热力图,数值为两个变量之间的相关系数
p=sns.heatmap(diabetes_data.corr(), annot=True)
plt.show()

# 把数据切分为特征x和标签y
x = diabetes_data.drop("Outcome",axis = 1)
y = diabetes_data.Outcome
from sklearn.model_selection import train_test_split
# 切分数据集,stratify=y表示切分后训练集和测试集中的数据类型的比例跟切分前y中的比例一致
# 比如切分前y中0和1的比例为1:2,切分后y_train和y_test中0和1的比例也都是1:2
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.3, stratify=y)
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
LR = LogisticRegression()
LR.fit(x_train,y_train)
predictions = LR.predict(x_test)
print(classification_report(y_test, predictions))

边栏推荐
- C语言项目中使用json
- Sub database and sub table may not be suitable for your system. Let's talk about how to choose sub database and sub table and newsql
- Developing NES games with C language (cc65) 04. Complete background
- Kafaka丢消息吗
- LeetCode84 柱状图中最大的矩形
- Interface control telerik UI for WPF - how to use radspreadsheet to record or comment
- SuperMap arsurvey license module division
- Marketing play is changeable, and understanding the rules is the key!
- Communication example between upper computer and Mitsubishi fn2x
- C# 泛型是什么、泛型缓存、泛型约束
猜你喜欢
随机推荐
Four authentic postures after suffering and trauma, Zizek
公司在什么情况下可以开除员工
Use json.stringify() to format data
Multiple items on a computer share a public-private key pair to pull the Gerrit server code
1331. 数组序号转换 : 简单模拟题
C语言项目中使用json
与元素类型 “item” 相关联的 “name” 属性值不能包含'<” 字符解决办法
卸载 Navicat:正版 MySQL 官方客户端,真香!
机器学习实战-神经网络-21
Jinshanyun rushes to the dual main listing of Hong Kong stocks: the annual revenue of 9billion is a project supported by Lei Jun
界面控件Telerik UI for WPF - 如何使用RadSpreadsheet记录或评论
Unity installs the device simulator
How to build knowledge management system in enterprises and institutions
[Nuxt 3] (十二) 项目目录结构 3
03 pyechars 直角坐标系图表(示例代码+效果图)
奥浦迈生物通过注册:半年营收1.47亿 国寿成达与达晨是股东
JSP自定义标签之自定义分页标签02
AI制药的数据之困,分子建模能解吗?
leetcode 376. Wiggle Subsequence
LeetCode 42.接雨水








