WillKoehrsen / Data-Analysis

Data Science Using Python
https://medium.com/@williamkoehrsen/
MIT License
5.12k stars 3.63k forks source link

not working on python 3.7 #47

Closed deep5050 closed 4 years ago

deep5050 commented 4 years ago

error while calling linear_regression() `python

ValueError Traceback (most recent call last) ~\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in create_block_manager_from_blocks(blocks, axes) 1650 blocks = [make_block(values=blocks[0], -> 1651 placement=slice(0, len(axes[0])))] 1652

~\Anaconda3\lib\site-packages\pandas\core\internals\blocks.py in make_block(values, placement, klass, ndim, dtype, fastpath) 3094 -> 3095 return klass(values, ndim=ndim, placement=placement) 3096

~\Anaconda3\lib\site-packages\pandas\core\internals\blocks.py in init(self, values, placement, ndim) 86 'Wrong number of items passed {val}, placement implies ' ---> 87 '{mgr}'.format(val=len(self.values), mgr=len(self.mgr_locs))) 88

ValueError: Wrong number of items passed 2, placement implies 14998

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)

in 1 # Run the by hand implementation ----> 2 by_hand_coefs = linear_regression(X, y) 3 print('Intercept calculated by hand:', by_hand_coefs[0]) 4 print('Slope calculated by hand: ', by_hand_coefs[1]) in linear_regression(X, y) 3 def linear_regression(X, y): 4 # Equation for linear regression coefficients ----> 5 beta = np.matmul(np.matmul(np.linalg.inv(np.matmul(X.T, X)), X.T), y) 6 return beta ~\Anaconda3\lib\site-packages\pandas\core\generic.py in __array_wrap__(self, result, context) 1907 def __array_wrap__(self, result, context=None): 1908 d = self._construct_axes_dict(self._AXIS_ORDERS, copy=False) -> 1909 return self._constructor(result, **d).__finalize__(self) 1910 1911 # ideally we would define this to avoid the getattr checks, but ~\Anaconda3\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy) 422 else: 423 mgr = init_ndarray(data, index, columns, dtype=dtype, --> 424 copy=copy) 425 426 # For data is list-like, or Iterable (will consume into list) ~\Anaconda3\lib\site-packages\pandas\core\internals\construction.py in init_ndarray(values, index, columns, dtype, copy) 165 values = maybe_infer_to_datetimelike(values) 166 --> 167 return create_block_manager_from_blocks([values], [columns, index]) 168 169 ~\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in create_block_manager_from_blocks(blocks, axes) 1658 blocks = [getattr(b, 'values', b) for b in blocks] 1659 tot_items = sum(b.shape[0] for b in blocks) -> 1660 construction_error(tot_items, blocks[0].shape[1:], axes, e) 1661 1662 ~\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in construction_error(tot_items, block_shape, axes, e) 1689 raise ValueError("Empty data passed with indices specified.") 1690 raise ValueError("Shape of passed values is {0}, indices imply {1}".format( -> 1691 passed, implied)) 1692 1693 ValueError: Shape of passed values is (2, 2), indices imply (2, 14998) `
deep5050 commented 4 years ago

by_hand_coefs = linear_regression(X.values, y.values) worked for me :)