对于数字范围从列表x到y可能包含NaN,如何可以在0和1之间的归一化,而忽略NaN的值(它们保持为NaN).
通常我会使用MinMaxScaler(ref page)sklearn.preprocessing,但是这不能处理NaN和建议根据平均值或中位数等来输入值.它不提供忽略所有NaN值的选项.
我知道DataFrame.sample(),但是我怎么能这样做并从数据集中删除样本?(注意:AFAIK这与更换取样无关)
例如,这是我想要实现的本质,这实际上并不起作用:
len(df) # 1000
df_subset = df.sample(300)
len(df_subset) # 300
df = df.remove(df_subset)
len(df) # 700
Run Code Online (Sandbox Code Playgroud) 我需要一个几秒钟的日期时间列,到处(包括文档)说我应该使用Series.dt.total_seconds()但它找不到该功能.我假设我有错误的版本,但我不...
pip freeze | grep pandas
pandas==0.20.3
python --version
Python 3.5.3
Run Code Online (Sandbox Code Playgroud)
这一切都在一个长期无故障运行的虚拟环境中,其他Series.dt功能都有效.这是代码:
from pandas import Series
from datetime import datetime
s = Series([datetime.now() for _ in range(10)])
0 2017-08-25 15:55:25.079495
1 2017-08-25 15:55:25.079504
2 2017-08-25 15:55:25.079506
3 2017-08-25 15:55:25.079508
4 2017-08-25 15:55:25.079509
5 2017-08-25 15:55:25.079510
6 2017-08-25 15:55:25.079512
7 2017-08-25 15:55:25.079513
8 2017-08-25 15:55:25.079514
9 2017-08-25 15:55:25.079516
dtype: datetime64[ns]
s.dt
<pandas.core.indexes.accessors.DatetimeProperties object at 0x7f5a686507b8>
s.dt.minute
0 55
1 55
2 55
3 55
4 …Run Code Online (Sandbox Code Playgroud) 我正在使用Fragments创建一个联系人列表应用程序,其中一个frag是联系人列表中的名称列表,另一个是剩余的详细信息.
这是显示名称列表的类
public class MyListFragment extends ListFragment {
private ContactStorage contactStorage = new ContactStorage();
public final static String TAG = "FRAGMENTS";
private MainActivity parent;
ArrayAdapter<String> adapter;
ArrayList<String> entries = new ArrayList<String>();
String array[];
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.list_layout, null);
parent = (MainActivity) getActivity();
entries = contactStorage.getContactListNames();
adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1, entries);
setListAdapter(adapter);
Log.d(TAG, "Adapter created");
array = contactStorage.getContactDetails();
return v;
}
@Override
public void onResume() {
super.onResume();
entries = contactStorage.getContactListNames();
adapter.notifyDataSetChanged(); …Run Code Online (Sandbox Code Playgroud) 我正在为我的软件编写API,以便更容易访问mongodb.
我有这条线:
def update(self, recid):
self.collection.find_and_modify(query={"recid":recid}, update={{ "$set": {"creation_date":str( datetime.now() ) }}} )
Run Code Online (Sandbox Code Playgroud)
哪个投掷TypeError: Unhashable type: 'dict'.
此函数仅用于查找recid与参数匹配的文档并更新其creation_date字段.
为什么会出现这个错误?
我看过这个问题,但它并没有给我任何答案.
基本上,我如何确定是否存在强相关np.correlate?我期望得到与matlab相同的输出,我可以理解xcorr的coeff选项(1是滞后的强相关,l0是滞后没有相关l),但np.correlate产生大于1的值,即使输入向量已经归一化0和1.
示例输入
import numpy as np
x = np.random.rand(10)
y = np.random.rand(10)
np.correlate(x, y, 'full')
Run Code Online (Sandbox Code Playgroud)
这给出了以下输出:
array([ 0.15711279, 0.24562736, 0.48078652, 0.69477838, 1.07376669,
1.28020871, 1.39717118, 1.78545567, 1.85084435, 1.89776181,
1.92940874, 2.05102884, 1.35671247, 1.54329503, 0.8892999 ,
0.67574802, 0.90464743, 0.20475408, 0.33001517])
Run Code Online (Sandbox Code Playgroud)
如果我不知道最大可能的相关值是什么,我怎么能分辨出什么是强相关性以及什么是弱相关?
另一个例子:
In [10]: x = [0,1,2,1,0,0]
In [11]: y = [0,0,1,2,1,0]
In [12]: np.correlate(x, y, 'full')
Out[12]: array([0, 0, 1, 4, 6, 4, 1, 0, 0, 0, …Run Code Online (Sandbox Code Playgroud) 我有一个更新数据库(MySQL)中的条目的函数,它需要尽可能动态。
该函数允许 API 用户传递记录的 id,然后使用 kwargs 来处理要更改的值(可以传递的值数量没有限制)。
假设我从 kwargs 那里得到了这本字典
{'hello':'world', 'foo':'bar'}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以在更新语句中使用它。
UPDATE myTable
SET key_n = val_n, key_n+1 = val_n+1, ...
WHERE id = the_id_passed_to_the_function
Run Code Online (Sandbox Code Playgroud)
提前致谢。
使用python 2.7.4
让我们说我有一个清单
list = ['abc', 'def']
Run Code Online (Sandbox Code Playgroud)
我想找到它是否包含某些东西.所以我尝试:
[IN:] 'abc' in list
[OUT:] True
[IN:] 'def' in list
[OUT:] True
[IN:] 'abc' and 'def' in list
[OUT:] True
Run Code Online (Sandbox Code Playgroud)
但是当我list.pop(0)并重复上一次测试:
[IN:] 'abc' and 'def in list
[OUT:] True
Run Code Online (Sandbox Code Playgroud)
即使:
list = ['def']
Run Code Online (Sandbox Code Playgroud)
谁知道为什么?