Given a 2x3 array, I want to calculate the average on """colors URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from accounts import views
from colorsets import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$',views.PostListView.as_view(),name='index'),
url(r'^accounts/',include('accounts.urls',namespace='accounts')),
url(r'^colorsets/',include('colorsets.urls',namespace='colorsets')),
]
, but only considering values that are larger than 0.
So given the array
{% extends 'base.html' %}
{% block content %}
<h3>Are you sure you want to delete this post?</h3>
<div class="posts">
{% include "colorsets/_post.html" with post=object hide_delete=True %}
</div>
<form method="POST">
{% csrf_token %}
<input type="submit" value="Confirm Delete" class="btn btn-danger btn-large" />
<a class="btn btn-simple btn-large" href="{% url 'index' username=user.username pk=object.pk %}">Cancel</a>
</form>
{% endblock %}
I want the output to be
{% extends "base.html" %}
{% block content %}
<div class="container">
<div class="sidebar">
<div class="widget widget-one">
<div class="widget-content">
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}</p>
{% endif %}
</div>
</div>
<div class="widget widget-two">
<p>Widget Two</p>
</div>
<div class="widget widget-three">
<p>Widget Three</p>
</div>
</div>
<div class="content">
{% for colorset in colorset_list %}
<table class="colorset">
<tr>
<h3 class="set-name">{{ colorset.name }}</h3>
<p class="author accent-text">Author: {{ colorset.user }}</p>
{% if user.is_authenticated and colorset.user == user %}
<a href="{% url 'colorsets:delete' pk=colorset.pk %}" title="delete" class="btn btn-simple">
<span class="text-danger icon-label">Delete</span>
</a>
{% endif %}
<td class="color" style="background-color:#{{ colorset.color_one }}">
</td>
<td class="color" style="background-color:#{{ colorset.color_two }}">
</td>
<td class="color" style="background-color:#{{ colorset.color_three }}">
</td>
<td class="color" style="background-color:#{{ colorset.color_four }}">
</td>
<td class="color" style="background-color:#{{ colorset.color_five }}">
</td>
</tr>
<tr>
<td>
<p>#{{ colorset.color_one }}</p>
</td>
<td>
<p>#{{ colorset.color_two }}</p>
</td>
<td>
<p>#{{ colorset.color_three }}</p>
</td>
<td>
<p>#{{ colorset.color_four }}</p>
</td>
<td>
<p>#{{ colorset.color_five }}</p>
</td>
</tr>
</table>
{% endfor %}
</div>
</div>
{% endblock %}
My current code is
axis=0
This gives me
[ [1,0],
[0,0],
[1,0] ]
I don't understand this error. What am I doing wrong and how can I get the average for all values greater than zero along # 1, 0, 1 filtered for > 0 gives 1, 1, average = (1+1)/2 = 1
# 0, 0, 0 filtered for > 0 gives 0, 0, 0, average = 0
[1 0]
? Thanks!
答案 0 :(得分:0)
You can get the mask of greater than zeros and use it to do elementwise multilication and sum-reduction along the first axis. Finally, divide by the number of masked elements along the first axis for getting the average values.
Thus, one solution would be -
public abstract class Vehicle{}
public class Car extends Vehicle{}
public class Van extends Vehicle{}
Sample run -
<request>
<car>...</car>
</request>
To account for all zero columns, it seems we are expecting <request>
<vehicle xsi:type="car"></vehicle>
</request>
as the result. So, we can use mask = a > 0 # Input array : a
out = np.einsum('i...,i...->...',a,mask)/mask.sum(0)
to do the choosing, like so -
In [52]: a
Out[52]:
array([[ 3, -3, 3],
[ 2, 2, 0],
[ 0, -3, 1],
[ 0, 1, 1]])
In [53]: mask = a > 0
In [56]: np.einsum('i...,i...->...',a,mask) # summations of > 0s
Out[56]: array([5, 3, 5])
In [57]: np.einsum('i...,i...->...',a,mask)/mask.sum(0) # avg values of >0s
Out[57]: array([ 2.5 , 1.5 , 1.66666667])
Just ignore the warning there.
If you feel paranoid about warnings, use 0
-
np.where