Sum matrix elements group by indices in Python
I have two matrix (same row and column): one with float values, which are grouped by indices in the other matrix. As a result, I want a dictionary or a list with the sums of the elements for each index. Indices always start at 0.
A = np.array([[0.52,0.25,-0.45,0.13],[-0.14,-0.41,0.31,-0.41]])
B = np.array([[1,3,1,2],[3,0,2,2]])
RESULT = {0: -0.41, 1: 0.07, 2: 0.03, 3: 0.11}
I found this solution, but I'm searching for a faster one. I'm working with matrix with 784 x 300 cells and this algorithm takes ~28ms to complete.
import numpy as np
def matrix_sum_by_indices(indices,matrix):
a = np.hstack(indices)
b = np.hstack(matrix)
sidx = a.argsort()
split_idx = np.flatnonzero(np.diff(a[sidx])>0)+1
out = np.split(b[sidx], split_idx)
return [sum(x) for x in out]
If you can help me find a better and plain solution to this problem, I'll be grateful!
EDIT: I made a mistake, time to complete is ~8ms in a 300*10 matrix, but ~28ms in a 784x300.
EDIT2: My A
elements are float64, so bincount
give me ValueError.
3 answers
-
answered 2018-07-10 15:16
Eelco Hoogendoorn
The numpy_indexed package has efficient and simple solutions to this problem (disclaimer: I am its author):
import numpy_indexed as npi keys, values = npi.group_by(B.flatten()).sum(A.flatten())
-
answered 2018-07-10 15:16
user3483203
You can make use of
bincount
here:a = np.array([[0.52,0.25,-0.45,0.13],[-0.14,-0.41,0.31,-0.41]]) b = np.array([[1,3,1,2],[3,0,2,2]]) N = b.max() + 1 id = b + (N*np.arange(b.shape[0]))[:, None] # since you can't apply bincount to a 2D array np.sum(np.bincount(id.ravel(), a.ravel()).reshape(a.shape[0], -1), axis=0)
Output:
array([-0.41, 0.07, 0.03, 0.11])
As a function:
def using_bincount(indices, matrx): N = indices.max() + 1 id = indices + (N*np.arange(indices.shape[0]))[:, None] # since you can't apply bincount to a 2D array return np.sum(np.bincount(id.ravel(), matrx.ravel()).reshape(matrx.shape[0], -1), axis=0)
Timings on this sample:
In [5]: %timeit using_bincount(b, a) 31.1 µs ± 1.74 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each) In [6]: %timeit matrix_sum_by_indices(b, a) 61.3 µs ± 2.62 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each) In [88]: %timeit scipy.ndimage.sum(a, b, index=[0,1,2,3]) 54 µs ± 218 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
(
scipy.ndimage.sum
should be faster on much larger samples) -
answered 2018-07-10 15:40
Thomas Baruchel
The following solution, relying on scipy.ndimage.sum is highly optimized for speed:
import numpy as np A = np.array([[0.52,0.25,-0.45,0.13], [-0.14,-0.41,0.31,-0.41]]) B = np.array([[1,3,1,2], [3,0,2,2]]) import scipy.ndimage print(scipy.ndimage.sum(A, B, index=[0,1,2,3]))
You may have to work a little for having the
index
parameter be exactly what you want. It is the list of the indices you want to get in the result. Maybe the following will be a good starting point:print(scipy.ndimage.sum(A,B, index=np.unique(B)))
but if you know by advance the list of all indices, it will be more efficient to hard-code it here.
See also questions close to this topic
-
Why is my if statement is always triggering in python
I'm new to Python and I decided to do a project by myself. In my project there is a if statement that always triggers. Also I'm still learning PEP 8, so tell me if I violated it.
yn = input('Do you need me to explain the rules. Y/N: ').lower() if yn == 'y' or 'yes': print('I will think of a number between 1 - 100.') print('You will guess a number and I will tell you if it is higher or lower than my number.') print('This repeats until you guess my number.')
-
Dialogflow python client versioning
I am using python client for accessing dialogflow's functionality.
My question is: doesimport dialogflow
and
import dialogflow_v2 as dialogflow
have any difference?
As per my experience, all the methods are the same. In the samples given by Google,import dialogflow_v2 as dialogflow
has been used and I could not see any difference between the two.Please note that here I am talking about version v2 in python client, and not the dialogflow API version.
-
Which device does .in_waiting and .out_waiting refer to in Pyserial?
I have a computer that is connected to a serial device at
/dev/ttyUSB0
via a wire with USB2 and microUSB2 connectors.My script writes:
ser = serial.Serial('/dev/ttyUSB0') in_buffer = ser.in_waiting in_data = ser.read( in_buffer ) out_buffer = ser.out_waiting out_data = ser.read( out_buffer )
Output:
ser = {'is_open': True, 'portstr': '/dev/ttyUSB0', 'name': '/dev/ttyUSB0', '_port': '/dev/ttyUSB0', '_baudrate': 9600, '_bytesize': 8, '_parity': 'N', '_stopbits': 1, '_timeout': None, '_write_timeout': None, '_xonxoff': False, '_rtscts': False, '_dsrdtr': False, '_inter_byte_timeout': None, '_rs485_mode': None, '_rts_state': True, '_dtr_state': True, '_break_state': False, '_exclusive': None, 'fd': 6, 'pipe_abort_read_r': 7, 'pipe_abort_read_w': 8, 'pipe_abort_write_r': 9, 'pipe_abort_write_w': 10} in_buffer = 0 <class 'int'> in_data = b'' <class 'bytes'> out_buffer = 0 <class 'int'> out_data = b'' <class 'bytes'>
Does
in_buffer
andout_buffer
refer to the no. of bytes in the buffer in the UART chip of the computer and the device/dev/ttyUSB0
, respectively? Why do they have zero byte size? -
How to compare all the first element of tuple with corresponding second element in the same tuple
i have a list of tuples as following:
terms = [('cat', 'cat'), ('cat', 'bat'), ('cat', 'cat'), ('cat', 'cat'), ('cat', 'bat'), ('cat', 'No Data'), ('cat', 'bat'), ('cat', 'No Data'), ('bat', 'cat'), ('bat', 'bat'), ('bat', 'cat'), ('bat', 'cat'), ('bat', 'bat'), ('bat', 'No Data'), ('bat', 'bat'), ('bat', 'No Data'), ('cat', 'cat'), ('cat', 'bat'), ('cat', 'cat'), ('cat', 'cat'), ('cat', 'bat'), ('cat', 'No Data'), ('cat', 'bat'), ('cat', 'No Data'), ('cat', 'cat'), ('cat', 'bat'), ('cat', 'cat'), ('cat', 'cat'), ('cat', 'bat'), ('cat', 'No Data'), ('cat', 'bat'), ('cat', 'No Data'), ('bat', 'cat'), ('bat', 'bat'), ('bat', 'cat'), ('bat', 'cat'), ('bat', 'bat'), ('bat', 'No Data'), ('bat', 'bat'), ('bat', 'No Data'), ('No Data', 'cat'), ('No Data', 'bat'), ('No Data', 'cat'), ('No Data', 'cat'), ('No Data', 'bat'), ('No Data', 'No Data'), ('No Data', 'bat'), ('No Data', 'No Data'), ('bat', 'cat'), ('bat', 'bat'), ('bat', 'cat'), ('bat', 'cat'), ('bat', 'bat'), ('bat', 'No Data'), ('bat', 'bat'), ('bat', 'No Data'), ('No Data', 'cat'), ('No Data', 'bat'), ('No Data', 'cat'), ('No Data', 'cat'), ('No Data', 'bat'), ('No Data', 'No Data'), ('No Data', 'bat'), ('No Data', 'No Data')]
I would like to compare the first element in the tuple with the second element in the tuple and use a method in fuzzywuzzy package (https://github.com/seatgeek/fuzzywuzzy).
from fuzzywuzzy import fuzz from fuzzywuzzy import process print([fuzz.token_set_ratio(ter1[0], ter1[1]) for ter1 in terms])
Is there any other way of using without a
for
loop(in the above statement) in list comprehension and make it faster.I would like to expect the result should be like:
[100, 28, 31, 23, 32, 41, 28, 38, 41, 31, 36, 26, 22, 35, 39, 39, 52, 52, 38, 30, 40, 35, 44, 44, 35, 39, 24, 32, 32, 55, 42, 37, 50, 34, 46, 30, 24, 30, 47, 26, 38, 58, 38, 29, 38, 38, 57, 47, 26, 40, 38, 40, 55, 25, 38, 62, 38, 38, 46, 44, 47, 56, 39, 57, 52, 55, 40, 48, 47, 55, 40, 30, 22, 55, 38, 55, 38, 26, 47, 55, 47, 50, 52, 47, 44, 45, 49, 52, 52, 43, 60, 38, 55, 52, 31, 56, 39, 46, 46, 50, 28, 100, 41, 48, 21, 25, 31, 86, 38, 34, 44, 29, 45, 28, 26, 26, 33, 38, 40, 27, 28, 40, 36, 36, 32, 41, 30, 30, 30, 33, 33, 40, 36, 32, 32, 32, 30, 33, 24, 24, 36, 41, 30, 39, 36, 34, 26, 24, 39, 23, 41, 33, 16, 30, 35, 34, 36, 35, 15, 36, 49, 29, 30, 26, 38, 28, 36, 33, 38, 16, 24, 27, 33, 33, 30, 16, 32, 31, 29, 16, 20, 28, 38, 24, 27, 47, 30, 38, 38, 30, 24, 36, 16, 38, 40, 29, 31, 15, 15, 24, 31, 41, 100, 31, 24, 28, 22, 27, 29, 38, 24, 39, 43, 30, 28, 28, 37, 35, 38, 22, 20, 35, 33, 33, 41, 39, 27, 27, 27, 30, 36, 37, 28, 46, 23, 38, 24, 22, 33, 32, 24, 32, 38, 43, 38, 32, 23, 13, 53, 25, 31, 40, 27, 33, 42, 44, 38, 33, 17, 28, 29, 32, 31, 23, 26, 24, 40, 36, 41, 27, 27, 44, 44, 42, 37, 27, 43, 35, 37, 27, 21, 30, 26, 13, 22, 35, 22, 26, 26, 27, 27, 29, 27, 26, 44, 32, 33, 17, 17, 27, 23, 48, 31, 100, 40, 28, 22, 27, 29, 38, 42, 26, 27, 25, 33, 33, 22, 26, 27, 22, 20, 19, 22, 22, 35, 33, 20, 38, 38, 24, 24, 44, 33, 34, 40, 30, 24, 37, 33, 26, 24, 39, 32, 29, 30, 32, 40, 20, 32, 30, 31, 35, 36, 33, 33, 44, 29, 33, 17, 33, 35, 16, 27, 40, 26, 18, 20, 30, 24, 36, 27, 30, 40, 24, 23, 36, 30, 17, 42, 36, 21, 40, 26, 20, 30, 35, 22, 26, 26, 27, 20, 29, 36, 26, 25, 16, 28, 17, 17, 27, 32, 21, 24, 40, 100, 43, 34, 22, 30, 32, 31, 27, 33, 31, 34, 34, 31, 27, 22, 31, 26, 20, 29, 29, 36, 23, 28, 33, 33, 31, 31, 38, 29, 29, 35, 26, 24, 15, 28, 20, 24, 40, 28, 30, 22, 25, 29, 28, 22, 31, 32, 26, 38, 35, 24, 32, 29, 23, 27, 23, 24, 25, 28, 29, 27, 31, 28, 38, 30, 38, 28, 31, 32, 19, 24, 38, 26, 27, 38, 38, 32, 31, 27, 28, 23, 36, 22, 27, 27, 28, 34, 20, 38, 27, 26, 25, 34, 27, 27, 29, 41, 25, 28, 28, 43, 100, 36, 20, 43, 34, 28, 24, 25, 23, 36, 36, 33, 23, 40, 27, 33, 29, 41, 41, 38, 36, 26, 35, 35, 22, 22, 53, 36, 32, 26, 36, 34, 40, 30, 29, 27, 47, 35, 39, 32, 27, 37, 30, 34, 33, 28, 23, 40, 30, 30, 34, 31, 28, 31, 36, 32, 29, 37, 37, 31, 39, 30, 39, 32, 40, 30, 40, 29, 33, 35, 40, 36, 23, 34, 40, 29, 33, 31, 30, 27, 42, 35, 31, 31, 35, 30, 27, 40, 31, 40, 29, 31, 31, 31, 24, 28, 31, 22, 22, 34, 36, 100, 34, 32, 39, 37, 29, 26, 32, 35, 35, 38, 24, 26, 38, 32, 29, 39, 39, 36, 30, 30, 30, 30, 28, 28, 32, 39, 27, 49, 35, 37, 27, 50, 34, 31, 44, 30, 32, 32, 27, 31, 35, 29, 32, 22, 32, 38, 35, 30, 38, 27, 25, 30, 30, 32, 40, 33, 31, 42, 33, 25, 33, 36, 38, 40, 32, 29, 37, 34, 38, 32, 30, 38, 38, 33, 40, 42, 35, 38, 40, 38, 42, 42, 30, 30, 31, 38, 42, 33, 40, 30, 30, 30, 25, 38, 86, 27, 27, 22, 20, 34, 100, 31, 27, 32, 33, 46, 31, 34, 34, 26, 18, 42, 32, 31, 41, 26, 26, 27, 43, 39, 29, 29, 50, 45, 21, 43, 22, 43, 28, 23, 26, 39, 29, 53, 33, 54, 26, 38, 45, 43, 44, 24, 39, 38, 43, 50, 34, 52, 42, 45, 46, 46, 43, 58, 44, 42, 43, 46, 50, 24, 55, 49, 50, 44, 37, 29, 50, 37, 50, 41, 29, 41, 50, 41, 39, 46, 44, 35, 39, 46, 46, 46, 46, 34, 50, 50, 46, 37, 44, 55, 46, 46, 50, 41, 38, 29, 29, 30, 43, 32, 31, 100, 29, 34, 31, 44, 25, 27, 27, 29, 26, 36, 23, 33, 33, 36, 36, 38, 36, 34, 31, 31, 34, 34, 34, 23, 28, 37, 29, 28, 34, 37, 21, 28, 36, 31, 28, 33, 38, 33, 26, 30, 25, 35, 33, 27, 19, 31, 35, 36, 39, 32, 36, 43, 24, 41, 33, 26, 39, 32, 29, 38, 27, 32, 34, 42, 29, 27, 27, 29, 19, 35, 27, 35, 29, 26, 26, 29, 38, 31, 26, 26, 27, 42, 29, 27, 26, 35, 24, 45, 32, 32, 27, 31, 34, 38, 38, 32, 34, 39, 27, 29, 100, 36, 32, 32, 35, 39, 39, 37, 26, 43, 22, 40, 23, 44, 44, 41, 44, 35, 38, 38, 30, 30, 37, 33, 34, 34, 30, 28, 30, 20, 32, 29, 39, 32, 43, 26, 21, 29, 33, 32, 35, 31, 30, 36, 17, 23, 38, 24, 22, 17, 28, 24, 40, 27, 29, 17, 24, 20, 30, 29, 36, 27, 22, 31, 42, 28, 36, 26, 26, 32, 36, 21, 35, 17, 33, 22, 35, 38, 17, 17, 32, 33, 24, 36, 17, 31, 40, 22, 17, 17, 18, 36, 44, 24, 42, 31, 28, 37, 32, 34, 36, 100, 32, 27, 30, 33, 33, 29, 20, 23, 35, 38, 27, 37, 37, 34, 28, 21, 32, 32, 25, 25, 35, 33, 33, 48, 33, 35, 41, 32, 26, 37, 37, 32, 34, 33, 38, 33, 32, 31, 38, 24, 34, 28, 26, 36, 31, 37, 39, 27, 33, 39, 25, 31, 33, 20, 30, 32, 30, 24, 28, 32, 24, 38, 30, 36, 28, 33, 40, 36, 28, 27, 38, 20, 32, 35, 38, 36, 20, 20, 27, 32, 42, 28, 20, 36, 25, 28, 27, 27, 28, 26, 29, 39, 26, 27, 24, 29, 33, 31, 32, 32, 100, 43, 36, 39, 39, 38, 29, 38, 19, 22, 32, 34, 34, 36, 34, 32, 33, 33, 37, 37, 31, 39, 35, 30, 35, 29, 38, 23, 33, 26, 28, 33, 42, 31, 33, 30, 17, 33, 22, 26, 31, 15, 28, 38, 43, 43, 34, 21, 34, 31, 33, 29, 30, 29, 32, 23, 32, 31, 15, 23, 25, 48, 37, 38, 15, 35, 29, 28, 15, 37, 31, 29, 17, 19, 22, 29, 29, 29, 24, 23, 26, 15, 29, 27, 33, 34, 21, 21, 22, 22, 45, 43, 27, 33, 25, 26, 46, 44, 32, 27, 43, 100, 35, 43, 43, 37, 24, 67, 32, 35, 67, 38, 38, 40, 69, 67, 46, 46, 36, 36, 32, 26, 30, 35, 41, 23, 32, 34, 33, 30, 33, 33, 36, 31, 33, 26, 39, 33, 24, 32, 35, 30, 34, 37, 42, 30, 34, 29, 34, 36, 33, 32, 26, 29, 27, 24, 36, 31, 30, 24, 32, 36, 41, 37, 30, 34, 24, 33, 30, 24, 35, 29, 39, 26, 31, 25, 29, 29, 42, 24, 38, 30, 29, 33, 33, 43, 29, 29, 18, 35, 28, 30, 25, 31, 23, 32, 31, 25, 35, 30, 36, 35, 100, 65, 65, 88, 32, 39, 53, 59, 30, 65, 65, 69, 36, 37, 67, 67, 38, 38, 29, 36, 41, 29, 33, 38, 29, 23, 36, 29, 31, 27, 33, 33, 34, 33, 23, 31, 37, 25, 33, 22, 26, 39, 35, 32, 32, 16, 32, 25, 31, 40, 33, 22, 26, 32, 30, 33, 22, 27, 34, 34, 34, 42, 22, 39, 27, 38, 22, 27, 30, 22, 23, 24, 37, 31, 22, 22, 27, 27, 25, 22, 22, 30, 31, 44, 16, 16, 28, 39, 26, 28, 33, 34, 36, 35, 34, 27, 39, 33, 39, 43, 65, 100, 100, 100, 36, 43, 53, 65, 32, 70, 70, 69, 43, 33, 65, 65, 33, 33, 43, 39, 31, 31, 32, 33, 27, 30, 39, 27, 49, 34, 42, 32, 36, 31, 30, 38, 36, 33, 40, 31, 29, 38, 38, 35, 34, 18, 30, 27, 34, 43, 31, ...........]
-
Reading vector information as a multidimensional array from VTU files using 'vtkXMLUnstructuredGridReader' in python
I'm trying to read a vector field information from a VTU file in python using vtkXMLUnstructuredGridReader. The vector field to be read is an array of N*3 dimension, where N is the number of cells and 3 the number of components of the vector. The VTU file looks like this (without the XML data),
<?xml version="1.0"?> <VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian" header_type="UInt32" compressor="vtkZLibDataCompressor"> <UnstructuredGrid> <FieldData> <DataArray type="Float64" Name="timeInPs" NumberOfTuples="1" format="appended" RangeMin="600" RangeMax="600" offset="0" /> </FieldData> <Piece NumberOfPoints="145705" NumberOfCells="838547" > <PointData Scalars="Material" Vectors="Magnetization"> <DataArray type="Float64" Name="Magnetization" NumberOfComponents="3" format="appended" RangeMin="1" RangeMax="1" offset="48" /> <DataArray type="Int32" Name="Material" format="appended" RangeMin="0" RangeMax="0" offset="4455172" /> </PointData> <CellData> </CellData> <Points> <DataArray type="Float32" Name="Points" NumberOfComponents="3" format="appended" RangeMin="1.0415804282e-12" RangeMax="10.00000052" offset="4456528" > <InformationKey name="L2_NORM_RANGE" location="vtkDataArray" length="2"> <Value index="0"> 1.0415804282e-12 </Value> <Value index="1"> 10.00000052 </Value> </InformationKey> </DataArray> </Points> <Cells> <DataArray type="Int64" Name="connectivity" format="appended" RangeMin="" RangeMax="" offset="6589768" /> <DataArray type="Int64" Name="offsets" format="appended" RangeMin="" RangeMax="" offset="20080856" /> <DataArray type="UInt8" Name="types" format="appended" RangeMin="" RangeMax="" offset="21531024" /> </Cells> </Piece> </UnstructuredGrid> <AppendedData encoding="base64">
For this I did some online search, even though I could not find a proper documentation on this, I found a thread on Stack here (Reading data from a raw VTK (.vtu) file) I tried using the code provided here
import vtk import numpy filname = trial.vtu reader = vtk.vtkXMLUnstructuredGridReader() reader.SetFileName(filename) reader.Update() output = reader.GetOutput() potential = output.GetPointData().GetArray("Magnetization") print potential
But as the output, instead of getting the N*3 array I'm getting the following.
vtkDoubleArray (0x28567a0) Debug: Off Modified Time: 389 Reference Count: 2 Registered Events: (none) Name: Magnetization Data type: double Size: 24519 MaxId: 24518 NumberOfComponents: 3 Information: 0x1fbab50 Debug: Off Modified Time: 388 Reference Count: 1 Registered Events: (none) Name: Magnetization Number Of Components: 3 Number Of Tuples: 8173 Size: 24519 MaxId: 24518 LookupTable: (none)
This contains all the information about the field, except the vector components as an N*3 array.
I have two questions,
1) What is missing in the code ?
2) Is there any proper documentation regarding this?
-
TypeError: bad operand type for unary ~: 'float' while groupby and apply a function
I get a TypeError while using groupby and a function to remove outliers:
def is_outlier(s): lower_limit = s.median() - (s.std() * 3) upper_limit = s.median() + (s.std() * 3) return ~s.between(lower_limit, upper_limit) df1 = df[~df.groupby('objectName')['price'].apply(is_outlier)] print(df1)
I have filtered NaN rows in
price
column with:df = df[np.isfinite(df['price'])]
, what I should do to make it return correctly? Thanks for helps at advance.File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1143, in __invert__ arr = operator.inv(com._values_from_object(self)) TypeError: bad operand type for unary ~: 'float'
This is what I get with
df['price'].describe()
, seems quite normal.count 10755.000000 mean 7.135314 std 3.844756 min 1.000000 25% 4.700000 50% 6.000000 75% 8.500000 max 49.000000
References related:
-
Rotate object around a certain point, even if the original pivot is different?
How can you rotate an object (let's say, its positions are at (5, 5, 0)) with the pivot point at let's say (3, 4, 0), even if the original point of rotation is (0, 0, 0)?
Here is a graphical explanation:
I want to rotate the object in relation to the custom pivot point. The object is made in blender in such a way, that the object is away from the origin (at the point (5, 5, 0)).
How can we use matrices to solve this?
-
r - Make adjacency matrix with tcrossprod capturing only positive values
I need to create an adjacency matrix from a dataframe using
tcrossprod
, but the resulting matrix needs to obey a restriction that I will explain below. Consider the following dataframe:z <- data.frame(Person = c("a","b","c","d"), Man_United = c(1,0,1,0)) z Person Man_United 1 a 1 2 b 0 3 c 1 4 d 0
I make an adjacency matrix from
z
usingtcrossprod
.x <- tcrossprod(table(z)) diag(x) <- 0 x Person Person a b c d a 0 0 1 0 b 0 0 0 1 c 1 0 0 0 d 0 1 0 0
I need the resulting adjacency matrix to indicate a tie (here signaled with the number 1), only when both persons have value 1 in the original dataframe (i.e. are fans of Manchester United, in this example). For example, persons "a" and "c" of dataframe
z
are fans, so in the resulting adjacency matrix I want their intersecting cell to be valued 1. That works fine here. However, persons "b" and "d" are not fans, and the fact that both have value 0 in the original dataframe does not mean that they are connected in any meaningful way.tcrossprod
, however, produces a matrix that suggests that they are in fact connected.How to use
tcrossprod
in a way that it caputures only the positve values of dataframes in producing adjacency matrices? -
How to create a matrix of plots with R and ggplot2
I am trying to arrange n consecutive plots into one single matrix of plots. I get the plots in first place by running a for-loop, but I can't figure out how to arrange those into a 'plot of plots'. I have used par(mfrow=c(num.row,num.col)) but it does not work. Also multiplot(plotlist = p, cols = 4) and plot_grid(plotlist = p)
#import dataset Survey<-read_excel('datasets/Survey_Key_and_Complete_Responses_excel.xlsx', sheet = 2) #Investigate how the dataset looks like glimpse(Survey)#library dplyr #change data types Survey$brand <- as.factor(Survey$brand) Survey$zipcode <- as.factor(Survey$zipcode) Survey$elevel <- as.factor(Survey$elevel) Survey$car <- as.numeric(Survey$car) #Relation brand-variables p = list() for(i in 1:ncol(Survey)) { if ((names(Survey[i])) == "brand"){ p[[i]]<-ggplot(Survey, aes(x = brand)) + geom_bar() + labs(x="Brand") } else if (is.numeric(Survey[[i]]) == "TRUE"){ p[[i]]<-ggplot(Survey, aes(x = Survey[[i]], fill=brand)) + geom_histogram() + labs(x=colnames(Survey[i])) } else { p[[i]]<-ggplot(Survey, aes(x = Survey[[i]], fill = brand)) + geom_bar() + labs(x=colnames(Survey[i])) } }
I think plots are appended correctly to the list but I can not plot them in a matrix form.
-
How to calculate sum of digits of a date in php?
I've date in following format.
2019-02-27
I've to find sum of digits in above date. I've exploded the above value like this,
$date = '2019-02-27'; $dateArray = explode('-',$date);
Now date array will be like this:
array:3 [▼ 0 => "2019" 1 => "01" 2 => "02" ]
With 2 loops, I can get sum of all digits but if there any much better way or builtin function to do this ?
Any kind of suggestion or help is appreciated.
-
How to write a working 2D Array List running sum function?
I have to be able to print the values of an array list in a matrix format with values increasing in the direction inputted in the method.
For example, if 3=up I need a 2Dlist with the values 10 15 30 40;15 5 8 2;20 2 4 2;1 4 5 0 inputted to print:
46 26 47 44
36 11 17 4
21 6 9 2
1 4 5 0
I also need it to be saved to the list so I can accomplish this if I am told to print down after I need it to look like this:
46 26 47 44
82 37 64 48
103 43 73 50
104 47 78 50
This is all I have for my method so far I need to adjust it to work properly any help is welcomed thanks.
public static void runningSum2DArrayList(ArrayList<ArrayList<Integer>> list, int a) { ArrayList<Integer>li; if (a==3) { //adds up int len=list.size(); int c=0; for (List<Integer> l: list) { int b=0; int j=0; for (int i: l) { li=list.get(len-(b+1)); j=li.get(c)+j; System.out.print(j+ " "); b++; } System.out.println(); c++; } } else if (a==2) { //adds right int j=0; for (List<Integer> l: list) { int b=0; for (int i: l) { if (b==0) { j=l.get(b); } else if (b==1) { j=l.get(b-1)+l.get(b); } else { j=l.get(b)+j; } b++; System.out.print(j+ " "); } System.out.println(); } System.out.println("right"); } else if (a==4) { //adds down int c=0; for (List<Integer> l: list) { int b=0; int j=0; for (int i: l) { li=list.get(b); j=li.get(c)+j; System.out.print(j+ " "); b++; } System.out.println(); c++; } } else if (a==1) { //adds left int j=0; for (List<Integer> l: list) { int len=l.size(); int b=0; for (int i: l) { if (b==0) { j=l.get(len-1); } else if (b==1) { j=l.get(len-1)+l.get(len-2); } else { j=l.get(len-(b+1))+j; } b++; System.out.print(j+ " "); } System.out.println(); } }
-
Describing number by sum of numbers
Given the whole number. Describe that by sum of numbers. For example input n=3 output 1+1+1=3 1+2=3
How can i code this?
-
Opengl Indexed Rendering with multiple VBOs
I'm a beginner at OpenGL, and I have a problem with rendering objects (I'll call them Entities).
I have a separate Renderer object, which just handles the draw calls, and I pass in the Entity I want to render.
Now the problem I'm having is that when I call
glDrawElements()
it doesn't draw anything.I've checked that every array has the correct stuff in it, and all the buffers are valid, also my shader works perfectly with the other rendering method. I've looked at other problems questions here, and I have a working render function with Entities that use normal Vertex Rendering.
My render function
void render(Entity model,EntityShader shader) { glBindVertexArray(model.getVao()); mat4 modelMatrix = mat4(); scale(modelMatrix, vec3(2.0)); translate(modelMatrix, vec3(5.0f,0.0f, 0.0f)); shader.loadTransformationMatrix(modelMatrix); glDrawElements(GL_TRIANGLES,model.getSize(),GL_UNSIGNED_SHORT,(void*)0); glBindVertexArray(0); }
My Entity's BufferLoader Part
GLuint vao, iv, pv, nv, tv; glGenBuffers(1, &tv); glGenBuffers(1, &nv); glGenBuffers(1, &pv); glGenBuffers(1, &iv); glGenVertexArrays(1,&vao); glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, pv); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vec3), &vertices[0], GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *)0); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, nv); glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(vec3), &normals[0], GL_STATIC_DRAW); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *)0); glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, tv); glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(vec2), &uvs[0], GL_STATIC_DRAW); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid *)0); glEnableVertexAttribArray(2); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iv); glBufferData(GL_ELEMENT_ARRAY_BUFFER,indices.size()*sizeof(unsigned short), &indices.front(), GL_STATIC_DRAW); glBindVertexArray(0);
Here's the data structure:
vector <vec3> vertices; vector <vec2> uvs; vector <vec3> normals; vector <unsigned short>indices;
These are my locals at glDrawElments() Thanks to your answers, in advance.
-
Sum over some indexes and axis with an irregulat list of list of indices numpy
I want to improve a code where I have to sum over different indices. This is the code:
neighbors = [[1,2,4],[2,0],[1,3],[],[0]] omega = np.random((5,5,3,3)) #Sum over indices 0 (filtered) and 2 suma = np.zeros((5,3)) for j in range(5): for l in range(3): suma[j,l] += np.sum(omega[neighbors[j],j,:,l])
Now I improved a little bit my code using the axis parameter of sum:
suma2 = np.zeros((5,3)) for j in range(5): suma2[j,:] += np.sum(omega[neighbors[j],j,:,:],axis=(0,1))
I want to know how I can avoid the first loop. I tried creating an array of booleans:
neighbors_bool = np.full((5,5),False,dtype=bool) for j in range(5): neighbors_bool[neighbors[j],j] = True
But I don't know how to put it in the sum.
-
AMPL how to have a summation constraint
I need a constraint that sums according to the following indices:
subject to shift[1]: x[1,2] + x[1,3] + x[1,4] + x[1,5] + x[1,6] >= 8; subject to shift[2]: x[1,2] + x[2,3] + x[2,4] + x[2,5] + x[2,6] >= 7; subject to shift[3]: x[1,3] + x[2,3] + x[3,4] + x[3,5] + x[3,6] >= 12; subject to shift[4]: x[1,4] + x[2,4] + x[3,4] + x[4,5] + x[4,6] >= 9; subject to shift[5]: x[1,5] + x[2,5] + x[3,5] + x[4,5] + x[5,6] >= 6; subject to shift[6]: x[1,6] + x[2,6] + x[3,6] + x[4,6] + x[5,6] >= 10;
What I have is:
param n; # number of shifts possible param demand {i in 1..n}; # demand of workers at each shift var x {1..n, 1..n} >= 0; # number of workers per shift # minimize function subject to shift {t in 1..n}: sum{j in 1..(n)} x[t,j] >= demand[t];
This is wrong as it gives the following:
subject to shift[1]: x[1,1] + x[1,2] + x[1,3] + x[1,4] + x[1,5] + x[1,6] >= 8; subject to shift[2]: x[2,1] + x[2,2] + x[2,3] + x[2,4] + x[2,5] + x[2,6] >= 7; subject to shift[3]: x[3,1] + x[3,2] + x[3,3] + x[3,4] + x[3,5] + x[3,6] >= 12; subject to shift[4]: x[4,1] + x[4,2] + x[4,3] + x[4,4] + x[4,5] + x[4,6] >= 9; subject to shift[5]: x[5,1] + x[5,2] + x[5,3] + x[5,4] + x[5,5] + x[5,6] >= 6; subject to shift[6]: x[6,1] + x[6,2] + x[6,3] + x[6,4] + x[6,5] + x[6,6] >= 10;