Python:只使用Numpy按日期范围处理数据



我正在为python数据科学课程做最后的作业,我们应该处理1700年到2019年的太阳黑子数据。这是使用matplotlib处理基本数据并为其开发可视化。我问过老师关于使用Pandas的问题,但是我们只被允许在这个项目中使用Numpy库。我们也没有学习过类,所以我认为这也是禁止的。

有人在下面的链接提问并解决了整个问题。我参考了他们的解决方案以寻求指导(我不需要解决整个问题,我只需要指出正确的方向),但它使用了Pandas。Chegg上发布的作业外部链接

数据以csv格式输入,格式如下:

record,year,sunspots
1,1700,5
2,1701,11
3,1702,16
4,1703,23
...
316,2015,130
317,2016,133
318,2017,127.9
319,2018,144
320,2019,141

根据提示,我认为这个想法是让数据作为一个完整的表读出,看起来像:

Min   Max   Total   Average  Stdev
18th C. 0     154   4544    45.44    35.79
19th C. 0     139   4218    42.18    33.35
20th C. 1     190   6256    61.56    46.28
21st C. 31    229   2451    122.55   53.05

目前我有正确读取的数据(我认为)如下:

# importing libraries
import numpy as np
import matplotlib.pyplot as mpl
# importing file and assigning relevant header information
sunspot_data = np.genfromtxt('project2_dataset.csv', delimiter=',', skip_header=False, dtype=str)
header = sunspot_data[0]
spot_data = sunspot_data[1:]
# indicating the data types and where they begin within the csv
record = spot_data[:, 0].astype(int)
year = spot_data[:, 1].astype(int)
num_spots = spot_data[:, 2].astype(float)
# creating the empty array and creating the arrays for the row and column headers
data_array = np.zeros((5, 6))
row_header = np.array(['','18th C.', '19th C.', '20th C.', '21st C.']).astype(str)
column_header = np.array(['','Minimum', 'Maximum', 'Total', 'Average', 'Standard Dev.']).astype(str)

我遇到的问题是,我正在运行一个"for"循环来获取各种值,但我不能让它们存储为一个数组,以便能够填充np.array。我目前使用的代码如下:

# defining the centuries within the data
cen18 = num_spots[0:100].astype(int)
cen19 = num_spots[100:200].astype(int)
cen20 = num_spots[200:300].astype(int)
cen21 = num_spots[300:].astype(int)
# creates a list of the centuries for processing
century_list = [cen18,cen19, cen20, cen21]
# for loop to get the descriptive statistics 
for lists in century_list:
min_list = np.array(np.min(lists))
max_list = np.array(np.max(lists))
sum_list = np.array(np.sum(lists))
mean_list = np.array(np.mean(lists))
stdev_list = np.array(np.std(lists))

我试图让这个正确打印,但以下是我写的代码和它当前的输出。

:

# attempt to insert the data within the array created above
data_array[1:,1] = min_list
data_array[1:,2] = max_list
data_array[1:,3] = sum_list
data_array[1:,4] = mean_list
data_array[1:,5] = stdev_list
print(data_array)

:

[[   0.           0.           0.           0.           0.       0.        ]
[   0.          33.         229.        2451.         122.55    53.05136662]
[   0.          33.         229.        2451.         122.55    53.05136662]
[   0.          33.         229.        2451.         122.55    53.05136662]
[   0.          33.         229.        2451.         122.55    53.05136662]]

row 0和col 0应该是上面看到的标题,这是一个完全不同的问题要解决…

所以我想我的问题是-我怎样才能得到正确的输出到np。数组,当我继续在十年级别上处理数据时,我如何有效地做到这一点,而不是为每个十年创建一个新变量?

你可以试试:

# your example data
a = np.genfromtxt(io.StringIO("""
1,1700,5
2,1701,11
3,1702,16
4,1703,23
316,2015,130
317,2016,133
318,2017,127.9
319,2018,144
320,2019,141"""), delimiter=',')[:, 1:].copy()
# a kind of groupby -- requires the centuries in the data to be contiguous
century, ix = np.unique(a[:, 0].astype(int) // 100, return_index=True)
out = np.c_[century + 1, [
(v.min(), v.max(), v.sum(), v.mean(), v.std())
for v in np.split(a[:,1], ix[1:])
]]
>>> out.round(3)
array([[ 18.   ,   5.   ,  23.   ,  55.   ,  13.75 ,   6.61 ],
[ 21.   , 127.9  , 144.   , 675.9  , 135.18 ,   6.265]])

(即:在18世纪,min为5,max为23,total为55,average为13.75,stddev为6.61)。

重要:数据需要按年排序(以确保每个世纪组是连续的)。如果不是,则需要先对其进行排序。

灵感来源&由于:关于numpygroupby的答案

最新更新