You can multiply numpy arrays by scalars and it just works.
>>> import numpy as np >>> np.array([1, 2, 3]) * 2 array([2, 4, 6]) >>> np.array([[1, 2, 3], [4, 5, 6]]) * 2 array([[ 2, 4, 6], [ 8, 10, 12]])
This is also a very fast and efficient operation. With your example:
>>> a_1 = np.array([1.0, 2.0, 3.0]) >>> a_2 = np.array([[1., 2.], [3., 4.]]) >>> b = 2.0 >>> a_1 * b array([2., 4., 6.]) >>> a_2 * b array([[2., 4.], [6., 8.]])
Related Posts:
- ImportError: numpy.core.multiarray failed to import
- Unable to plot Double Bar, Bar plot using pyplot for ndarray
- ImportError: numpy.core.multiarray failed to import
- ImportError: cannot import name NUMPY_MKL
- TypeError: ‘int’ object is not subscriptable
- ImportError: DLL load failed: The specified module could not be found
- ValueError: setting an array element with a sequence
- TypeError: ‘int’ object is not callable
- What exactly does numpy.exp() do? [closed]
- TypeError: ‘int’ object is not callable
- Python ‘If not’ syntax [duplicate]
- Converting dictionary to JSON
- numpy: Invalid value encountered in true_divide
- python numpy ValueError: operands could not be broadcast together with shapes
- How to fix IndexError: invalid index to scalar variable
- Could not install packages due to a “Environment error :[error 13]: permission denied : ‘usr/local/bin/f2py'”
- How does numpy.newaxis work and when to use it?
- sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’)
- Should I use np.absolute or np.abs?
- ln (Natural Log) in Python
- Convert pandas dataframe to NumPy array
- Cannot find module cv2 when using OpenCV
- Singular matrix issue with Numpy
- ‘DataFrame’ object has no attribute ‘sort’
- ValueError: all the input arrays must have same number of dimensions
- TypeError: cannot unpack non-iterable int objec
- Why are Python’s ‘private’ methods not actually private?
- ValueError: setting an array element with a sequence
- How to raise a numpy array to a power? (corresponding to repeated matrix multiplications, not elementwise)
- ‘End of statement expected’ in pycharm
- How to import files in python using sys.path.append?
- What does numpy.gradient do?
- Change figure size and figure format in matplotlib
- ‘list’ object has no attribute ‘shape’
- load csv into 2D matrix with numpy for plotting
- how does \r (carriage return) work in Python
- What is Python buffer type for?
- IndexError: index 1 is out of bounds for axis 0 with size 1/ForwardEuler
- Overcome ValueError for empty array
- TypeError: unsupported operand type(s) for -: ‘list’ and ‘list’
- AttributeError: ‘datetime’ module has no attribute ‘strptime’
- Python add item to the tuple
- index 1 is out of bounds for axis 0 with size 1
- Pytorch reshape tensor dimension
- Most efficient way to reverse a numpy array
- How to remove specific elements in a numpy array
- Overflow / math range error for log or exp
- Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?
- Overflow Error in Python’s numpy.exp function
- Converting binary to decimal integer output
- How can I read pdf in python?
- No module named setuptools
- Understanding NumPy’s einsum
- For loop and ‘numpy.float64’ object is not iterable error
- What are the causes of overflow encountered in double_scalars besides division by zero?
- RuntimeWarning: divide by zero encountered in log
- How to fix: “UnicodeDecodeError: ‘ascii’ codec can’t decode byte”
- Python – ‘ascii’ codec can’t decode byte
- RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility
- Error NameError: name ‘np’ is not defined
- What is the difference between json.load() and json.loads() functions
- Numpy.dot TypeError: Cannot cast array data from dtype(‘float64’) to dtype(‘S32’) according to the rule ‘safe’
- NumPy array is not JSON serializable
- How to XOR two strings in Python
- Does Numpy automatically detect and use GPU?
- How can I check whether a numpy array is empty or not?
- Copy a list of list by value and not reference
- Moving average or running mean
- How to start from second index for for-loop
- Split a python list into other “sublists” i.e smaller lists
- Official abbreviation for: import scipy as sp/sc
- scrapy run spider from script
- python error: TypeError: an integer is required
- 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
- If list index exists, do X
- How to detect key presses?
- Building multi-regression model throws error: `Pandas data cast to numpy dtype of object. Check input data with np.asarray(data).`
- ImportError in importing from sklearn: cannot import name check_build
- Root mean square of a function in python
- What are the differences between numpy arrays and matrices? Which one should I use?
- ‘virtualenv’ is not recognized as an internal or external command, operable program or batch file
- mean, nanmean and warning: Mean of empty slice
- Split string using a newline delimiter with Python
- No module named urllib3
- Replacing Pandas or Numpy Nan with a None to use with MysqlDB
- AttributeError: ‘tuple’ object has no attribute
- How to normalize a NumPy array to within a certain range?
- How to implement the ReLU function in Numpy
- Sorting arrays in NumPy by column
- How to open html file?
- “Python version 2.7 required, which was not found in the registry” error when attempting to install netCDF4 on Windows 8
- Does python have header files like C/C++?
- numpy : calculate the derivative of the softmax function
- Python 2.7 mixing iteration and read methods would lose data
- Conditional indexing with Numpy ndarray
- What does an ‘r’ represent before a string in python?
- python numpy machine epsilon
- Python: OSError: [Errno 2] No such file or directory: ”