飞道的博客

全网最全python实现数据挖掘,数据分析(matlablib,pandas,numpy,量化分析)(附源代码)

358人阅读  评论(0)

全网最全python实现数据挖掘,数据分析(matlablib,pandas,numpy,量化分析)(附源代码)

个人公众号:yk 坤帝
后台回复数据挖掘1 获取源代码

1.横直方图电影票房

2.散点图3月与10月每天的天气

3.条形图电影票房

4.条形图三天票房

5.用条形图绘制出直方图

6.折线图10点到12点气温

7.折线图调整x轴的刻度

8.折线图调整x轴的刻度

9.折线图设置中文(气温)

10.直方图 250部电影的时长分布

11.page15

12.读取外部数据

13.bool索引与缺失值的处理

14.dataFrame的创建

15.dataFrame的描述信息

16.dataFrame的索引

17.series的了解


1.横直方图电影票房

# 大二
# 2021年2月28日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

from matplotlib import pyplot as plt

plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

a = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊",]

b=[56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]

plt.figure(figsize= (20,8),dpi = 80)

plt.barh(range(len(a)),b,height = 0.3,color = 'orange')

plt.yticks(range(len(a)),a)

plt.grid(alpha = 0.3)

#plt.savefig('./movie.png')

plt.show()


2.散点图3月与10月每天的天气

# 大二
# 2021年2月28日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

from matplotlib import pyplot as plt

plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

y_3 = [11,17,16,11,12,11,12,6,6,7,8,9,12,15,14,17,18,21,16,17,20,14,15,15,15,19,21,22,22,22,23]
y_10 = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,17,10,11,13,12,13,6]

x_3 = range(1,32)
x_10 = range(51,82)

plt.figure(figsize=(18,8), dpi = 80)

plt.scatter(x_3,y_3,label = '3月份')
plt.scatter(x_10,y_10,label = '10月份')

_x = list(x_3) + list(x_10)
x_tick_labels = ['3月{}'.format(i) for i in x_3]
x_tick_labels += ['10月{}'.format(i-50) for i in x_10]

plt.xticks(_x[::3],x_tick_labels[::3],rotation = 45)

plt.legend(loc = 'upper left')

plt.xlabel('时间')
plt.ylabel('温度')
plt.title('标题')

# plt.plot(x_3,y_3)
# plt.plot(x_10,y_10)

plt.show()


3.条形图电影票房

# 大二
# 2021年2月28日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

from matplotlib import pyplot as plt

plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

a = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊",]

b=[56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]

plt.figure(figsize= (20,8),dpi = 80)

plt.bar(range(len(a)),b,width= 0.3)

plt.xticks(range(len(a)),a,rotation = 90)

plt.savefig('./movie.png')

plt.show()


4.条形图三天票房

# 大二
# 2021年2月28日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

from matplotlib import pyplot as plt

plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

a = ["猩球崛起3:终极之战","敦刻尔克","蜘蛛侠:英雄归来","战狼2"]
b_16 = [15746,312,4497,319]
b_15 = [12357,156,2045,168]
b_14 = [2358,399,2358,362]

bar_width = 0.3
x_14 = list(range(len(a)))
x_15 = [i+bar_width for i in x_14]
x_16 = [i+bar_width*2 for i in x_14]

plt.figure(figsize=(20,8),dpi = 80)

plt.bar(x_14,b_14,width=bar_width,label = '9月14日')
plt.bar(x_15,b_15,width=bar_width,label = '9月15日')
plt.bar(x_16,b_16,width=bar_width,label = '9月16日')

plt.xticks(x_15,a)

plt.legend(loc = 'upper left')

plt.savefig('./直方图.png')
plt.show()


5.用条形图绘制出直方图

# 大二
# 2021年2月28日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

from matplotlib import pyplot as plt 

plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

interval = [0,5,10,15,20,25,30,35,40,45,60,90]
width = [5,5,5,5,5,5,5,5,5,15,30,60]
quantity = [836,2737,3723,3926,3596,1438,3273,642,824,613,215,47]

plt.figure(figsize=(20,8),dpi = 80)

plt.bar(range(len(interval)),quantity,width = 1)

_x = [i-0.5 for i in range(len(interval)+1)]
_xtick_labels = interval + [150]

plt.xticks(_x,_xtick_labels)

plt.grid(alpha = 0.4)
plt.show()


6.折线图10点到12点气温

# 大二
# 2021年2月28日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

from matplotlib import pyplot as plt
import random 

x = range(0,120)

y = [random.randint(20,35) for i in range(120)]
plt.figure(figsize=(20,8),dpi = 80)

plt.plot(x,y)


plt.show()


7.折线图调整x轴的刻度

# 大二
# 2021年2月28日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

from matplotlib import pyplot as plt 

x = range(2,26,2)

y = [15,13,14,10,2,12,13,11,10,12,11,8]

plt.figure(figsize=(20,8),dpi = 80)

plt.plot(x,y)
#plt.xticks(range(0,25,2))
_xtick_lables = [i/2 for i in range(4,49)]
plt.xticks(_xtick_lables)
#plt.xticks(range(25,50))

plt.yticks(range(min(y),max(y)+1))

plt.savefig('./sig_size.png')
plt.savefig('./sig_size.svg')
plt.show()


8.折线图绘制多次图形

# 大二
# 2021年2月28日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

from matplotlib import pyplot as plt 


plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

# plt.rcParams['font.family'] = ['sans-serif']
# plt.rcParams['font.sans-serif'] = ['SimHei']


y_1 = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
y_2 = [1,0,3,1,2,2,3,3,2,1 ,2,1,1,1,1,1,1,1,1,1]



x = range(11,31)

plt.figure(figsize= (20,8), dpi = 80)

plt.plot(x,y_1,label = '自己',color = 'orange',linestyle = ':')
plt.plot(x,y_2,label = '同桌',color = '#DB7093',linestyle = '--')

_xtick_labels = ['{}岁'.format(i) for i in x]
plt.xticks(x,_xtick_labels)

plt.grid(alpha = 0.4)

plt.legend(loc = 'upper left')

plt.show()


9.折线图设置中文(气温)

# 大二
# 2021年2月28日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

from matplotlib import pyplot as plt
import random 

#中文字体
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

x = range(0,120)

y = [random.randint(20,35) for i in range(120)]
plt.figure(figsize=(20,8),dpi = 80)

plt.plot(x,y)

_xtick_labels = ['10点{}分'.format(i) for i in range(60)]
_xtick_labels += ['11点{}分'.format(i) for i in range(60)]

plt.xticks(list(x)[::3],_xtick_labels[::3],rotation = 45)
#rint(list(x)[::3])

plt.xlabel('时间')
plt.ylabel('温度 单位(℃)')
plt.title('10点到12点每分钟的气温变化情况')

plt.show()


10.直方图 250部电影的时长分布

# 大二
# 2021年2月28日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

from matplotlib import pyplot as plt

plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

a=[131,  98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115,  99, 136, 126, 134,  95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117,  86,  95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123,  86, 101,  99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140,  83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144,  83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137,  92,121, 112, 146,  97, 137, 105,  98, 117, 112,  81,  97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112,  83,  94, 146, 133, 101,131, 116, 111,  84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]

d = 3
num_bins = (max(a)-min(a))//d

plt.figure(figsize=(20,8),dpi = 80)

#参数density显示频率
plt.hist(a,num_bins,density=True)
plt.xticks(range(min(a),max(a)+d,d))

plt.grid()
plt.savefig('./电影时长.png')

plt.show()


11.page15

# 大二
# 2021年2月28日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

from matplotlib import pyplot as plt 

x = range(2,26,2)

y = [15,13,14,10,2,12,13,11,10,12,11,8]

plt.figure(figsize=(20,8),dpi = 80)

plt.plot(x,y)
plt.show()


12.读取外部数据

# 大二
# 2021年3月3日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

import pandas as pd 
import numpy as np

df = pd.read_csv('dogNames2.csv')
print(df)

from pymongo import MongoClient

client = MongoClient()
collection = client['douban']['tv1']
data = list(collection.find())

t1 = data[0]
t1 = pd.Series(t1)
print(t1)


13.bool索引与缺失值的处理


# 大二
# 2021年3月3日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

import pandas as pd 
import numpy as np

df = pd.read_csv('dogNames2.csv')
print(df.info())

print(df[(df['Count_AnimalName'] > 1 & df['Count_AnimalName'] < 3)])

14.dataFrame的创建

# 大二
# 2021年3月3日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

import pandas as pd 
import numpy as np

t = pd.DataFrame(np.arange(12).reshape(3,4))
print(t)

t = pd.DataFrame(np.arange(12).reshape(3,4),index = list('abc'),columns = list('wxyz'))
print(t)

t = pd.DataFrame(np.arange(12).reshape(3,4),index = list('abc'),columns = list('qrst'))
print(t)

d1 = {
   'name':['xiaoming','xiaogang'],'age':[20,32],'tel':[10086,10010]}
t1 = pd.DataFrame(d1)
print(t1)

d2 = [{
   'name':'xiaoming','age':20,'tel':'10086'},
{
   'name':'xiaogang','age':33,'tel':'10010'},
{
   'name':'xiaowang','age':32}]
print(d2)
t1 = pd.DataFrame(d2)
print(t1)

print(t1.info())
print(t.head(1))
print(t.tail(2))
print(t.describe())

15.dataFrame的描述信息

# 大二
# 2021年3月3日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

import pandas as pd 
import numpy as np

df = pd.read_csv('dogNames2.csv')

print(df.head())
print(df.info())

print(df.head(1))
print(df.tail(2))
print(df.describe())

df = df.sort_values(by ='Count_AnimalName',ascending=False)
print(df.head(5))

16.dataFrame的索引

# 大二
# 2021年3月3日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

import pandas as pd 
import numpy as np

df = pd.read_csv('dogNames2.csv')

df = df.sort_values(by ='Count_AnimalName',ascending=False)
print(df.head(5))

print(df[:20])
print(df['Row_Labels'])
print(type(df['Row_Labels']))


t3 = pd.DataFrame(np.arange(12).reshape(3,4),index = list('abc'),columns = list('wxyz'))
print(t3)
print(t3.loc['a','z'])
print(type(t3.loc['a','z']))

print(t3.loc['a',:])

print(t3.iloc[1])

17.series的了解

# 大二
# 2021年3月3日
# 寒假开学时间3月7日
# 个人公众号:yk 坤帝
# 后台回复数据挖掘1 获取源代码

import pandas as pd 
import numpy as np

print(pd.Series([1,2,3,1,3,333,4]))
t = pd.Series([1,2,31,12,3,4])
print(type(t))

t2 = pd.Series(np.arange(6),index = list('abcdef'))
print(t2)

temp_dict = {
   'name':'xiaohong','age':30,'tel':'10086'}
t3 = pd.Series(temp_dict)
print(t3)

print(t3.dtype)
print(t2.dtype)

t2 = t2.astype(float)
print(t2)

个人公众号:yk 坤帝
后台回复数据挖掘1 获取源代码


转载:https://blog.csdn.net/qq_45803923/article/details/116768021
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场