Count occurrences of a value in a matrix [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I have a problem with my code:
I don't manage to find a right way to count occurrences of number one in a list of arrays like this
[[0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1],
[1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1]]
I want to produce an array that contains the sum of ones in each list of my matrix
I tried to use numpy making
for m in matrix:
y = m.np
y.count(1)
but the compiler returns me a message like this:
module 'numpy' has no attribute 'm'
How can I proceed?

Since you are counting ones, you can just sum them up:
count_ones = [sum(array) for array in matrix]

Grow a list with the counts; there is no need of numpy array.
ones_per_row = []
for m in matrix:
ones_per_row.append(m.count(1))
print(ones_per_row)
Output:
[7, 2, 2, 7, 8, 3, 7, 3]

Related

Spatial autocorrelation on a two-dimensional array with missing data

I have some observations collected at sea, and that we managed to classify in 2 clusters (blue and red), based on their properties. As you see in my example below, when projected, the classification looks as "spatially coherent", or at least, clusters don't look like randomly distributed. I'm looking for an statistic that tells about this spatial coherence, for each class, or for the full classification. I have seen examples in PYSAL or ESDA modules, but none with this type of data, a two-dimensional labeled array (1 and 2 values) with missing data (zero values). I don't know how to proceed.
This is the code example:
import matplotlib as mpl
import matplotlib.pyplot as plt
# DATA EXAMPLE
# I have a regular grid, with not-sampled (d==0) and sampled (d>0) areas.
# Sampled areas were classified as '1' and '2', based on some measurements
# that we collected at each location.
d = [[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0],
[1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2],
[0, 1, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 2, 2],
[0, 1, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 2, 2, 2],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2],
[0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2]]
# VISUAL
# class 1 (blue) and class 2 (red)
cmap = mpl.colors.ListedColormap(['w','b','r'])
plt.pcolormesh(d, cmap=cmap)
That's what you see when running the example:
Any advice on how to proceed? Thanks in advance!

Building SVM throws error because my training array has an extra dimension [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am creating a support vector machine. The model below reads the arrays that begin with "log" as vectors in the SVM graph. Arrays log15-log21 are to be classified "c" while the lines log22-log36 are to be classified "d". The goal is to give the svm another vector in the format of the "log" lines and for the svm to label it "c" or "d".
from sklearn import svm
log15 = [0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
log16 = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
log17 = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
log18 = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0]
log19 = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0]
log20 = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
log21 = [0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
log22 = [0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
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, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
log35 = [0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
log36 = [0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
cLines = [log15, log16, log17, log18, log19, log20, log21]
dLines = [log22, log23, log24, log25, log26, log27, log28, log29, log30, log31, log32, log33, log34, log35, log36]
lines = [log15, log16, log17, log18, log19, log20, log21, log22, log23, log24, log25, log26, log27, log28, log29, log30, log31, log32, log33, log34, log35, log36]
X = [lines]
y = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] #0 for c, 1 for d
clf = svm.SVC()
clf.fit(X, y)
print(clf.predict([[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]]))
When I run the code above I get this error:
Traceback (most recent call last):
File "C:/Users/craig/Code/Python Programs/TensorFlowLabs/svm.py", line 34, in <module>
clf.fit(X, y)
File "c:\Users\craig\Programming Languages\Lib\site-packages\sklearn\svm\_base.py", line 196, in fit
accept_large_sparse=False,
File "c:\Users\craig\Programming Languages\Lib\site-packages\sklearn\base.py", line 576, in _validate_data
X, y = check_X_y(X, y, **check_params)
File "c:\Users\craig\Programming Languages\Lib\site-packages\sklearn\utils\validation.py", line 968, in check_X_y
estimator=estimator,
File "c:\Users\craig\Programming Languages\Lib\site-packages\sklearn\utils\validation.py", line 788, in check_array
% (array.ndim, estimator_name)
builtins.ValueError: Found array with dim 3. Estimator expected <= 2.
An online guide I see does not have the vectors separated by commas, but the individual characters in the vector arrays have significant meaning, so I don't want the 1's and 0's to be "jumbled up", if that makes sense.
You are defining X as an array (you are using brackets). That's why you are obtaining an error. Change the way you define X and it should work:
X = lines

How would I remove all the zeros before the 1 in this python list

result = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0]
I want this list to be turned into
result = [1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0]
I have tried using a while loop to keep removing zeros until it finds a 1 but I cant seem to get that to work.
for x in result:
while (x == 0):
result.remove(0)
If there always is a 1, you could just find it and delete everything before it:
del result[:result.index(1)]
Or if it could be all zeros:
if any(result):
del result[:result.index(1)]
or
try:
del result[:result.index(1)]
except ValueError:
pass
or
result.append(1)
del result[:result.index(1)]
result.pop()
One way using itertools.dropwhile:
from itertools import dropwhile
list(dropwhile(lambda x: x == 0, result))
Output:
[1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0]
Problem in your code is you are changing the list while iterating. Instead you can use while loop.
while result and result[0] == 0:
result.pop(0)
print(result)
Output:
[1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0]
Or else try the following :
result = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0]
a=''
for i in result:
a=a+str(i)
print(list(a.lstrip('0')))
result = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0]
# prints index of 1st (leftmost) '1' encountered in the list
# print(result.index(1))
# print from that index to the wholw remaining list
print(result[result.index(1):])

Create Array with Binomial Values (Most Time Efficient) based on conditions

Currently I am working on a project that involves creating an array with 10 binomial values 0 and 1 and a given success rate (= ci_rate[i]/1'000).
Due to the fact that the rate is different for each of the 10 years, I run a loop 10 times that is creating 20'000 binomial values each time (for 20'000 scenarios).
The success rate for the binomial values is very small, but is an absorbing state for the following years. Simplified for only 10 scenarios and 10 years I would like to output the following:
[1,0,0,0,0,0,0,0,0,0]
[1,0,0,0,0,0,0,1,0,0]
[1,0,0,1,0,0,0,1,0,0]
[1,0,0,1,0,0,0,1,0,0]
[1,0,0,1,0,0,0,1,0,0]
[1,0,0,1,0,0,0,1,0,0]
[1,0,0,1,0,1,0,1,0,0]
[1,0,0,1,0,1,0,1,0,0]
[1,0,0,1,0,1,0,1,0,0]
[1,0,0,1,0,1,0,1,0,0]
Currently I am solving the problem in this way:
for j in range(20000):
tem = np.zeros(len(ci_rate))
for i in range(len(ci_rate)):
if i == 0:
tem[0] = (np.random.binomial(1, p = ci_rate[i] / 1000))
else:
tem[i]= int(np.where(tem[i-1]==1, 1, np.random.binomial(1, p = ci_rate[i] / 1000)))
ci_sim.append(tem)
Is anyone creative enough to solve this more time efficient?
This solution first ignores the persistence rule and enforces it afterwards using maximum.accumulate.
ci_rate = np.random.uniform(0, 0.1, 10)
res = np.maximum.accumulate(np.random.random((20000, ci_rate.size))<ci_rate, axis=1).view(np.int8)
res[:20]
#
# array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
# [0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
# [0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
# [0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
# [0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int8)
My attempt would be:
import numpy as np
ci_rate = np.random.normal(size=20)
ci_rate = (ci_rate - min(ci_rate)) /(max(ci_rate) - min(ci_rate)) - 0.7
ci_rate[ci_rate < 0] = 0
r = []
for i in range(100):
t = np.random.binomial(1, ci_rate)
r += [t.tolist()]
ci_rate = [1 if j == 1 else i for i, j in zip(ci_rate, t)]
#output
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0],
I am suggesting geometric distribution, since it looks like you are trying to see the number of trials for first success.
I am comparing the usefulness of using the geomentric distribution in terms of computation time
EDIT:
%%timeit
ci_rate = np.random.uniform(0, 0.1, nb_years)
successful_trail = np.random.geometric(p=ci_rate)
ci_sim=np.zeros((nb_scenarios,nb_years))
for i in range(nb_years):
ci_sim[i,successful_trail[i]:]=1
## 10000 loops, best of 3: 41.4 µs per loop
%%timeit
ci_rate = np.random.uniform(0, 0.1, nb_years)
res = np.maximum.accumulate(np.random.random((nb_scenarios, ci_rate.size))<ci_rate, axis=1).view(np.int8)
## 100 loops, best of 3: 2.97 ms per loop

Python: Given an adjacency matrix in form of an array of arrays - how can I get the connected components?

Given an adjacency matrix of an undirected graph in form of an array of arrays in python, how can I get the connected components in form of (row,col,class)?
I already used scipy.sparse.csgraph.connected_components(adjmx) - yet what I got was only a list of the connected component labels. How can I get their precise location (meaning: row and col and label)?
And here an example - given a sparse matrix M which belongs to an undirected graph G:
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]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(M, directed=False)
What it yields is:
a) the number of connected components: 7
b) an array:
[0 1 2 2 2 3 3 3 2 2 4 5 2 2 2 2 2 0 3 3 6 3 3 3 6]
What I need now is an output in the fashion (row, col, cc label). What is the precise semantic of this array i get in b) ?
EDIT: The solution provided in this post: Finding connected components in a pixel-array actually did the trick! It proposes also how to recover the rows and columns indices of a given label.

Categories

Resources