如何以元素方式将scipy.sparse
矩阵提升为幂?根据{{3}},numpy.power
应该这样做,但它在稀疏矩阵上失败了:
>>> X
<1353x32100 sparse matrix of type '<type 'numpy.float64'>'
with 144875 stored elements in Compressed Sparse Row format>
>>> np.power(X, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../scipy/sparse/base.py", line 347, in __pow__
raise TypeError('matrix is not square')
TypeError: matrix is not square
与X**2
相同的问题。转换为密集阵列有效,但浪费了宝贵的时间。
我遇到np.multiply
同样的问题,我使用稀疏矩阵的multiply
方法解决了这个问题,但似乎没有pow
方法。
答案 0 :(得分:11)
这有点低级,但对于元素操作,您可以直接使用基础数据数组:
>>> import scipy.sparse
>>> X = scipy.sparse.rand(1000,1000, density=0.003)
>>> X = scipy.sparse.csr_matrix(X)
>>> Y = X.copy()
>>> Y.data **= 3
>>>
>>> abs((X.toarray()**3-Y.toarray())).max()
0.0
答案 1 :(得分:8)
我刚遇到同样的问题,发现稀疏矩阵现在支持元素功率。对于上述情况,它应该是:
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new
ViewPagerAdapter(getSupportFragmentManager());
//change the fragmentName as per your need
adapter.addFragment(new FirstFragment(), "OVERVIEW");
adapter.addFragment(new Secondfragment(), "TRAILER");
adapter.addFragment(new thirdFragment(), "ORDERS");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}