在不创建列表的情况下填充numpy数组

时间:2021-02-21 21:24:45

I would like to create a numpy array without creating a list first.
At the moment I've got this:

我想先创建一个numpy数组而不创建列表。现在我有这样的想法:

import pandas as pd
import numpy as np 

dfa = pd.read_csv('csva.csv')
dfb = pd.read_csv('csvb.csv')

pa = np.array(dfa['location'])
pb = np.array(dfb['location'])

ra = [(pa[i+1] - pa[i]) / float(pa[i]) for i in range(9999)]
rb = [(pb[i+1] - pb[i]) / float(pb[i]) for i in range(9999)]

ra = np.array(ra)
rb = np.array(rb)

Is there any elegant way to do in one step the last fill in of this np array without creating the list first ?

有什么优雅的方法可以在一步中完成这个np数组的最后填充,而不需要先创建列表吗?

Thanks

谢谢

3 个解决方案

#1


3  

You can calculate with vectors in numpy, without the need of lists:

可以用numpy中的向量进行计算,不需要列表:

ra = (pa[1:] - pa[:-1]) / pa[:-1]
rb = (pb[1:] - pb[:-1]) / pb[:-1]

#2


2  

The title of your question and what you need to do in your specific case are actually two slighly different things.

你的问题的题目和你在特定情况下需要做的事情实际上是两个有点不同的东西。

To create a numpy array without "casting" a list (or other iterable) you can use one of the several methods defined by numpy itself that returns array:

要创建一个numpy数组而不“强制”列表(或其他可迭代的),您可以使用numpy本身定义的返回数组的几种方法之一:

  • np.empty, np.zeros, np.ones, np.full to create arrays of given size with fixed values
  • np。空,np。0,np。的np。full创建具有固定值的给定大小的数组。
  • np.random.* (where * can be various distributions, like normal, uniform, exponential ...), to create arrays of given size with random values
  • np.random。*(其中*可以是各种分布,如普通分布、均匀分布、指数分布等),用随机值创建给定大小的数组

In general, read this: Array creation routines

一般来说,读这个:数组创建例程。

In your case, you already have numpy arrays (pa and pb) and you don't have to create lists to calculate the new arrays (ra and rb), you can directly operate on the numpy arrays (which is the entire point of numpy: you can do operations on arrays way faster that would be iterating over each element!). Copied from @Daniel's answer:

在你的情况下,你已经numpy数组(pa、pb),您不需要创建列表计算新数组(ra和rb),您可以直接操作numpy数组(这是整个numpy点:你可以操作数组的方式更快,将遍历每个元素!)。抄袭@Daniel的回答是:

ra = (pa[1:] - pa[:-1]) / pa[:-1]
rb = (pb[1:] - pb[:-1]) / pb[:-1]

This will be much faster than you're current implementation, not only because you avoid converting a list to ndarray, but because numpy arrays are order of magnuitude faster for mathematical and batch operations than iteration

这将比您当前的实现快得多,这不仅是因为您避免将列表转换为ndarray,而且因为numpy数组在数学和批处理操作上比迭代快得多

#3


0  

numpy.zeros

numpy.zeros

Return a new array of given shape and type, filled with zeros.

返回一个给定形状和类型的新数组,填充0。

or

numpy.ones

numpy.ones

Return a new array of given shape and type, filled with ones.

返回一个给定形状和类型的新数组,其中填充了一个。

or

numpy.empty

numpy.empty

Return a new array of given shape and type, without initializing entries.

返回给定形状和类型的新数组,而不初始化条目。

#1


3  

You can calculate with vectors in numpy, without the need of lists:

可以用numpy中的向量进行计算,不需要列表:

ra = (pa[1:] - pa[:-1]) / pa[:-1]
rb = (pb[1:] - pb[:-1]) / pb[:-1]

#2


2  

The title of your question and what you need to do in your specific case are actually two slighly different things.

你的问题的题目和你在特定情况下需要做的事情实际上是两个有点不同的东西。

To create a numpy array without "casting" a list (or other iterable) you can use one of the several methods defined by numpy itself that returns array:

要创建一个numpy数组而不“强制”列表(或其他可迭代的),您可以使用numpy本身定义的返回数组的几种方法之一:

  • np.empty, np.zeros, np.ones, np.full to create arrays of given size with fixed values
  • np。空,np。0,np。的np。full创建具有固定值的给定大小的数组。
  • np.random.* (where * can be various distributions, like normal, uniform, exponential ...), to create arrays of given size with random values
  • np.random。*(其中*可以是各种分布,如普通分布、均匀分布、指数分布等),用随机值创建给定大小的数组

In general, read this: Array creation routines

一般来说,读这个:数组创建例程。

In your case, you already have numpy arrays (pa and pb) and you don't have to create lists to calculate the new arrays (ra and rb), you can directly operate on the numpy arrays (which is the entire point of numpy: you can do operations on arrays way faster that would be iterating over each element!). Copied from @Daniel's answer:

在你的情况下,你已经numpy数组(pa、pb),您不需要创建列表计算新数组(ra和rb),您可以直接操作numpy数组(这是整个numpy点:你可以操作数组的方式更快,将遍历每个元素!)。抄袭@Daniel的回答是:

ra = (pa[1:] - pa[:-1]) / pa[:-1]
rb = (pb[1:] - pb[:-1]) / pb[:-1]

This will be much faster than you're current implementation, not only because you avoid converting a list to ndarray, but because numpy arrays are order of magnuitude faster for mathematical and batch operations than iteration

这将比您当前的实现快得多,这不仅是因为您避免将列表转换为ndarray,而且因为numpy数组在数学和批处理操作上比迭代快得多

#3


0  

numpy.zeros

numpy.zeros

Return a new array of given shape and type, filled with zeros.

返回一个给定形状和类型的新数组,填充0。

or

numpy.ones

numpy.ones

Return a new array of given shape and type, filled with ones.

返回一个给定形状和类型的新数组,其中填充了一个。

or

numpy.empty

numpy.empty

Return a new array of given shape and type, without initializing entries.

返回给定形状和类型的新数组,而不初始化条目。