MS python学习(22)(完结)

MS系列最后一课 数据的可视化

visualize with matplotlib

Visualization(matplotlib)

Both linear and scatterplot can show how data is distributed and what the correlation between them

concept

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# 用到matplotlib
import matplotlib.pyplot as plt

# 先加载CSV
defer_df = pd.read_csv('Data/Lots_of_flight_data.csv')
defer_df.dropna(inplace=True)

# 要建立departure delay 和 arrive delay关系
x = defer_df.loc[:,['DEP_DELAY']]
y = defer_df.loc[:, ['ARR_DELAY']]

# train 两者model
x_train,x_test,y_train,y_test = train_test_split(x, y, test_size=0.3, random_state=42)
regressor = LinearRegression()
regressor.fit(x_train,y_train)
# 得到y_predict
y_pred = regressor.predict(x_test)

# 定义图的x,y,title信息
plt.xlabel('Departure Delay(miniutes)')
plt.ylabel('Arrive Delay(miniutes)')
plt.title('Correlation between Departure delay and Arrive Delay')
# 利用x_test, y_pred画一个直线关系图
plt.plot(x_test, y_pred, color='RED', linewidth=2)
# 画departure delay 和 arrive delay的散点图,alpha表示点的颜色深浅,范围0~1,1表示完全黑点,用浅色更能表现出数据严重程度的高低
plt.scatter(x, y, color='blue', alpha=0.3)

# 两个图同时显示
plt.show()

corr

附上另一种画散点图的方法:

defer_df.plot(
    	kind='scatter', 
    	x='DEP_DELAY', 
    	y='ARR_DELAY', 
    	color='blue', 
    	alpha=0.3, 
    	title='Correlation between distance and arrive delay')
plt.show()

Moving to next Chapter…

TO BE CONTINUED…

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值