The Numpythonic approach: (using numpy.dot
in order to get the dot product of two matrices)
In [1]: import numpy as np In [3]: np.dot([1,0,0,1,0,0], [[0,1],[1,1],[1,0],[1,0],[1,1],[0,1]]) Out[3]: array([1, 1])
The Pythonic approach:
The length of your second for
loop is len(v)
and you attempt to indexing v
based on that so you got index Error . As a more pythonic way you can use zip
function to get the columns of a list then use starmap
and mul
within a list comprehension:
In [13]: first,second=[1,0,0,1,0,0], [[0,1],[1,1],[1,0],[1,0],[1,1],[0,1]] In [14]: from itertools import starmap In [15]: from operator import mul In [16]: [sum(starmap(mul, zip(first, col))) for col in zip(*second)] Out[16]: [1, 1]
Related Posts:
- numpy matrix vector multiplication
- TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- Import Error: No module named numpy
- data type not understood
- Singular matrix issue with Numpy
- ‘End of statement expected’ in pycharm
- How to get element-wise matrix multiplication (Hadamard product) in numpy?
- Inverse of a matrix using numpy
- For loop and ‘numpy.float64’ object is not iterable error
- How to start from second index for for-loop
- How to fix ‘RuntimeWarning: divide by zero encountered in double_scalars’
- Root mean square of a function in python
- What are the differences between numpy arrays and matrices? Which one should I use?
- built-in range or numpy.arange: which is more efficient?
- Convert a 1D array to a 2D array in numpy
- How to install NumPy for Python 3.6
- Conditional indexing with Numpy ndarray
- ImportError: DLL load failed: The specified module could not be found
- What is the difference between rw+ and r+
- Python – TypeError: ‘int’ object is not iterable
- What exactly does numpy.exp() do? [closed]
- Best way to convert string to bytes in Python 3?
- PermissionError: [Errno 13] Permission denied
- What is setup.py?
- numpy: Invalid value encountered in true_divide
- Invalid character in identifier
- ValueError: Unknown label type: ‘continuous’
- Transpose/Unzip Function (inverse of zip)?
- TypeError: unhashable type: ‘numpy.ndarray’
- Convert bytes to a string
- Should I use np.absolute or np.abs?
- How to find all occurrences of an element in a list
- TypeError: ‘numpy.float64’ object is not callable
- ‘str’ object has no attribute ‘decode’. Python 3 error?
- How to find all the indexes of a recurring item in a list?
- ‘DataFrame’ object has no attribute ‘sort’
- ValueError: all the input arrays must have same number of dimensions
- How do you use subprocess.check_output() in Python?
- ValueError: setting an array element with a sequence
- filename.whl is not a supported wheel on this platform
- (Help) TypeError: ‘str’ object cannot be interpreted as an integer
- How do order of operations go on Python?
- Why KeyError: 0
- Convert a tensor to numpy array in Tensorflow?
- How to raise a numpy array to a power? (corresponding to repeated matrix multiplications, not elementwise)
- Microsoft Visual C++ 14.0 is required (Unable to find vcvarsall.bat)
- Pinging servers in Python
- Import error: No module name urllib2
- What does numpy.gradient do?
- ‘list’ object has no attribute ‘shape’
- load csv into 2D matrix with numpy for plotting
- Purpose of `numpy.log1p( )`?
- How to take column-slices of dataframe in pandas
- TypeError(“‘bool’ object is not iterable”,) when trying to return a Boolean
- Get ZeroDivisionError: float division in python
- index 1 is out of bounds for axis 0 with size 1
- Most efficient way to reverse a numpy array
- How to delete last item in list?
- Don’t understand this SyntaxError: illegal target for annotation
- Using Queue in python
- How to normalize a 2-dimensional numpy array in python less verbose?
- “for line in…” results in UnicodeDecodeError: ‘utf-8’ codec can’t decode byte
- SyntaxError invalid token
- How do I format a string using a dictionary in python-3.x?
- Understanding NumPy’s einsum
- What are the causes of overflow encountered in double_scalars besides division by zero?
- python mpl_toolkits installation issue
- RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility
- TypeError: ‘dict_keys’ object does not support indexing
- How to strip all whitespace from string
- Python Binomial Coefficient
- TypeError: Invalid dimensions for image data when plotting array with imshow()
- pythonw.exe or python.exe?
- How to convert ‘binary string’ to normal string in Python3?
- python3 TypeError: ‘function’ object is not iterable
- Moving average or running mean
- ModuleNotFoundError: No module named ‘seaborn’ in Python IDE
- Is there a ceiling equivalent of // operator in Python?
- How to plot an array in python?
- How to create a numpy array of all True or all False?
- Removing nan values from an array
- LinAlgError: Last 2 dimensions of the array must be square
- Building multi-regression model throws error: `Pandas data cast to numpy dtype of object. Check input data with np.asarray(data).`
- TypeError: ‘set’ object does not support indexing
- How to fix “Can’t find a default Python” error
- TypeError: ‘newline’ is an invalid keyword argument for this function
- Print empty line?
- Checking to see if a string is an integer or float
- mean, nanmean and warning: Mean of empty slice
- How to know/change current directory in Python shell?
- How to embed image or picture in jupyter notebook, either from a local machine or from a web resource?
- Mean Squared Error in Numpy?
- How to implement the ReLU function in Numpy
- How to use cmp() in Python 3?
- “E: Unable to locate package python3-pip”
- Error: No module named ‘fcntl’
- Python can’t find file
- Transposing a 1D NumPy array
- numpy.float64 object is not iterable…but I’m NOT trying to
- Accessing dict_keys element by index in Python3