如何修复TypeError:+不支持的操作数类型:'int'和'list'

时间:2019-04-15 16:45:15

标签: python

您好,我正在为rmse设置代码,但发生错误

<div id="fb-root"></div>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.2"></script>

<div class="w-container">
    <div id="allPosts">
        @foreach (var post in ViewBag.Posts.Data)
        {
            <div class="w-row">
                <div class="w-col w-col-12 w-col-medium-12 w-col-small-12">
                    <p>@post.MetaDescription ...</p>
                </div>
            </div>
            <div class="w-row post-button">
                <div class="w-col w-col-3 w-col-small-12">
                    <input type="hidden" class="postBodyHtmlHidden" value="@post.Body" />
                    <input type="hidden" class="postSlug" value="@post.Slug" />
                    <div class="fb-comments" style="display: none;" data-href="http://localhost:36998/blog#@post.Slug" data-numposts="5"></div>
                    <input type="button" value="Continue Reading" class="btn btn-primary continue-reading-btn" />
                </div>
            </div>
        }
    </div>

    <div id="postBody" style="display:none;">
        <div class="w-row">
            <div class="w-col w-col-12">
                <input type="button" value="Back" onclick="AllPosts()" class="btn btn-primary" />
                <br />
                <div id="postHmtlDiv">

                </div>
                <div id="postCommentDiv">

                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $('.continue-reading-btn').on("click", function () {
        var postHtml = $(this).closest('.w-row').find('.postBodyHtmlHidden').val();
        var postSlug = $(this).closest('.w-row').find('.postSlug').val();
        var postComment = $(this).closest('.w-row').find('.fb-comments').html();

        $('#allPosts').css("display", "none");
        $("#postHmtlDiv").html(postHtml);
        $('#postCommentDiv').html(postComment);
        $('#postCommentDiv .fb-comments').removeAttr('style');
        $("#postBody").fadeIn("slow");

    });

    function AllPosts() {
        $('#postBody').css('display', 'none');
        $('#postHmtlDiv').html('');
        $('#allPosts').fadeIn('slow');
    };

</script>

错误是

import numpy as np

import matplotlib.pyplot as plt

import random

data=[[235,591],[216,539],[148,413],[35,310],[85,308],[204,519],[49,325],[25,332],[173,498],[191,498],[134,392],[99,334],[117,385],[112,387],[162,425],[272,659],[159,400],[159,427],[59,319],[198,522]]

x_data=[x_row[0] for x_row in data]

y_data=[y_row[1] for y_row in data]

a=np.random.randint(0,10)

b=np.random.randint(0,100)

def f(x):
    return b+a*x

def E(x,y):
    return 0.5*np.sum((y-f(x))**2)

n=1e-3

D=1

count=0

error=E(x_data,y_data)


while D>1e-2:

    tmp0=b-n*np.sum((f(x_data)-y_data))
    tmp1=a-n*np.sum((f(x_data)-y_data)*x_data)
    b=tmp0
    a=tmp1
    current_error=E(x_data,y_data)
    D=error-current_error
    count=count+1
    if count % 100 == 0 :
        print(count,a,b,current_error,D)

1 个答案:

答案 0 :(得分:0)

问题出在

def f(x):
    return b+a*x

在这种情况下,ab被解释为整数,而x被解释为列表。

如果您打算将计算应用于x中的每个元素,请尝试执行以下操作:

def f(x):    
    out = [] 
    for i in x:
        out.append(b+a*i)
    return out

稍后在尝试在计算中使用列表的位置进行更多计算时,您将遇到类似的错误。您将必须进行类似的更改。

例如:

def E(x,y):
    return 0.5*np.sum((y-f(x))**2)

将必须更改为以下内容:

def E(x,y):
    out = [] 
    for xi, yi in zip(y, f(x)):
        out.append(0.5*np.sum((yi-xi**2)))
    return out

避免TypeError: unsupported operand type(s) for -: 'list' and 'list'