0
votes

I am trying to use cancer dataset from sklean and it was imported well and all the things seems good but when i try to creat a dataframe it shows error in tracebak "Shape of passed values is (30, 569), indices imply (569, 569)"

from sklearn.datasets import load_breast_cancer
cancer=load_breast_cancer()
cancer.keys()
df_feat = pd.DataFrame(cancer['data'],columns=cancer['target'])

ValueError Traceback (most recent call last) C:\Users\Bilal Pharmacist\Anaconda3\lib\site- packages\pandas\core\internals.py in create_block_manager_from_blocks(blocks, axes) 4293 blocks = [make_block(values=blocks[0], 4294 placement=slice(0, len(axes[0])))] 4295

C:\Users\Bilal Pharmacist\Anaconda3\lib\site- 
packages\pandas\core\internals.py in 
make_block(values, placement, klass, ndim, dtype, fastpath)
2718 
2719     return klass(values, ndim=ndim, fastpath=fastpath, 
placement=placement)
2720 

C:\Users\Bilal Pharmacist\Anaconda3\lib\site- 
packages\pandas\core\internals.py in 
__init__(self, values, placement, ndim, fastpath)
114                              'implies %d' % (len(self.values),
115                                              len(self.mgr_locs)))
116 

ValueError: Wrong number of items passed 30, placement implies 569

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-16-24a03a5e14d7> in <module>()
1 df_feat = pd.DataFrame(cancer['data'],columns
2                        =cancer['target'])

C:\Users\Bilal Pharmacist\Anaconda3\lib\site-packages\pandas\core\frame.py in 
__init__(self, data, index, columns, dtype, copy)
304             else:
305                 mgr = self._init_ndarray(data, index, columns, 
dtype=dtype,
306                                          copy=copy)
307         elif isinstance(data, (list, types.GeneratorType)):
308             if isinstance(data, types.GeneratorType):

C:\Users\Bilal Pharmacist\Anaconda3\lib\site-packages\pandas\core\frame.py in 
_init_ndarray(self, values, index, columns, dtype, copy)
481             values = maybe_infer_to_datetimelike(values)
482 
483         return create_block_manager_from_blocks([values], [columns, 
index])
484 
485     @property

C:\Users\Bilal Pharmacist\Anaconda3\lib\site- 
packages\pandas\core\internals.py in 
create_block_manager_from_blocks(blocks, axes)
4301         blocks = [getattr(b, 'values', b) for b in blocks]
4302         tot_items = sum(b.shape[0] for b in blocks)
4303         construction_error(tot_items, blocks[0].shape[1:], axes, e)
4304 
4305 

C:\Users\Bilal Pharmacist\Anaconda3\lib\site- 
packages\pandas\core\internals.py in 
construction_error(tot_items, block_shape, axes, e)
4278         raise ValueError("Empty data passed with indices specified.")
4279     raise ValueError("Shape of passed values is {0}, indices imply 
{1}".format(
4280         passed, implied))
4281 
4282 
ValueError: Shape of passed values is (30, 569), indices imply (569, 569)
1

1 Answers

1
votes

The error was because the shape of cancer['data'] is (569, 30) (i.e. max 30 column names acceptable), whereas that of cancer['target'] is (569,) (and you were trying to set them as column names). Use cancer['feature_names'] as columns instead. I guess cancer['target'] is literally a target variable (y) and should not be column names, but rather one of columns in the dataframe.

This should work:

import pandas as pd
from sklearn.datasets import load_breast_cancer

cancer = load_breast_cancer()
df_feat = pd.DataFrame(cancer['data'], columns=cancer['feature_names'])
df_feat['target'] = cancer['target']