数据挖掘与机器学习-实验一


数据挖掘与机器学习-实验一

pandas

简介

三种数据类型

Series

DataFrame

方法

date_range

1
2
3
4
5
>>>date_time_index = pd.date_range(start="2018-06-29", end="2018-07-02")
>>>date_time_index = pd.date_range(start="2018-06-29", periods=4)
>>>date_time_index = pd.date_range(periods=4, end="2018-07-02")
>>>date_time_index = pd.date_range(start="2018-01-01", end="2018-07-01", freq='M')
>>>date_time_index = pd.date_range(start="2018-01-01", end="2018-07-01", freq='D')

numpy

简介

方法

loadtxt

1
2
input_file = r"xxx.txt"
data = np.loadtxt(input_file, delimiter=',')

matplotlib

实验代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

###STEP2###
def convert_data_to_timeseries(input_file, column, verbose=False):
# 导入数据
data = np.loadtxt(input_file, delimiter=',')
# 确定索引的开始与结束时间
start_date = str(int(data[0,0])) + '-' + str(int(data[0,1]))
end_date = str(int(data[-1,0] + 1)) + '-' + str(int(data[-1,1] % 12 + 1))
###问题一:完善函数###
###提示:通过pandas的date_range函数获取索引###

date_time_index = pd.date_range(start=start_date, end=end_date, freq='M')
data_timeseries = pd.Series(data[:,column], index=date_time_index)
return data_timeseries



###STEP3###
# 文件路径
input_file = r'C:\Users\Ty\Desktop\实验手册-实验二pandas\实验手册-实验二pandas\data.txt'
column_num = 2
data_timeseries = convert_data_to_timeseries(input_file, column_num)
# Plot方法成图
data_timeseries.plot()
plt.title('Input data')
# # 图像太密集了,我们换一个时间范围
start = '2007-2'
end = '2007-11'
plt.figure()
data_timeseries[start:end].plot()
plt.title('Data from ' + start + ' to ' + end)
plt.show()


# ###STEP4###
# ###问题二:将数据的第三,四列转换为二维数据结构dataframe,索引为时间###
data1 = convert_data_to_timeseries(input_file, 2)
data2 = convert_data_to_timeseries(input_file, 3)
# # 将两组数据同时成图
pd.dataframe['1955':'1960'].plot()
plt.title('Data overlapped on top of each other')


# ###STEP5###
# # 查看两组数据是否有线性关联
plt.figure()
difference = pd.dataframe['1952':'1955']['first'] - pd.dataframe['1952':'1955']['second']
difference.plot()
plt.title('Difference (first - second)')
plt.show()

###STEP6###
###问题三:请统计两组数组的最大最小值和均值###
###问题三:计算数据的相关系数,调用corr函数###
print('\nMaximum:\n', pd.dataframe.max())
print('\nMinimum:\n', pd.dataframe.min())
print('\nMean:\n', pd.dataframe.mean())
print('\nCorrelation coefficients:\n', pd.dataframe.corr())


###STEP7###
# 打印两组数据相关性
plt.figure()
pd.rolling_corr(pd.dataframe['first'], pd.dataframe['second'], window=60).plot()
pd.dataframe['first'].rolling(60).corr(pd.dataframe['second']).plot()
plt.show()

本文标题:数据挖掘与机器学习-实验一

文章作者:TTYONG

发布时间:2020年09月23日 - 18:09

最后更新:2022年05月05日 - 10:05

原始链接:http://tianyong.fun/%E6%95%B0%E6%8D%AE%E6%8C%96%E6%8E%98%E6%8A%80%E6%9C%AF%E4%B8%8E%E5%BA%94%E7%94%A8-%E5%AE%9E%E9%AA%8C%E4%B8%80.html

许可协议: 转载请保留原文链接及作者。

多少都是爱
0%