小编gmd*_*mds的帖子

Python请求的异步请求

我尝试了python请求库文档中提供的示例:

http://docs.python-requests.org/en/latest/user/advanced/#asynchronous-requests

async.map(rs)我得到的响应代码,但我想请求每一页的内容.

out = async.map(rs)
print out[0].content
Run Code Online (Sandbox Code Playgroud)

例如,只是不工作.

python asynchronous httprequest python-requests

128
推荐指数
10
解决办法
21万
查看次数

Roc曲线和切断点.蟒蛇.

我正在运行一个逻辑模型,我预测了logit值.我用了 :

 from sklearn import metrics
 fpr, tpr, thresholds = metrics.roc_curve(Y_test,p)
Run Code Online (Sandbox Code Playgroud)

我知道metric.roc_auc_score将给出曲线下面积但是任何人都可以让我知道找到最佳截止点(阈值)的命令是什么.

python roc logistic-regression

42
推荐指数
5
解决办法
3万
查看次数

如何加速Angular构建过程

我努力解决在TypeScript文件中的一些变化后每个构建需要超过20分钟的问题.我运行这个命令ng build --output-path=..\..\static\angularjs.

如果我在Microsoft PowerShell上运行它:它需要25 - 30秒.这是很多时间.

环境

  • Windows 10
  • 8 GB-Ram
  • PyCharm 64
  • MS PowerShell

有人知道,如何加快速度?

angular-cli angular

28
推荐指数
7
解决办法
4万
查看次数

如何根据python的前身删除列表项

给定一个Python列表,我想删除连续的“重复项”。但是,重复值是列表项的属性(在此示例中,是tuple第一个元素)。

输入:

[(1, 'a'), (2, 'b'), (2, 'b'), (2, 'c'), (3, 'd'), (2, 'e')]
Run Code Online (Sandbox Code Playgroud)

所需输出:

[(1, 'a'), (2, 'b'), (3, 'd'), (2, 'e')]
Run Code Online (Sandbox Code Playgroud)

不能使用setdict,因为顺序很重要。

无法使用列表推导功能[x for x in somelist if not determine(x)],因为检查取决于前任。

我想要的是这样的:

[(1, 'a'), (2, 'b'), (2, 'b'), (2, 'c'), (3, 'd'), (2, 'e')]
Run Code Online (Sandbox Code Playgroud)

用Python解决此问题的首选方法是什么?

python list

18
推荐指数
3
解决办法
1124
查看次数

具有两个或多个返回参数的函数注释

当我为返回一个参数的函数编写注释时,我没有任何问题。

def func() -> str:
    return "ok"
Run Code Online (Sandbox Code Playgroud)

但是,当我编写带有两个或多个参数的注释时,我的PyCharm给了我SyntaxError: invalid syntax

def func() -> str, str:
    return "ok - 1", "ok - 2"
Run Code Online (Sandbox Code Playgroud)

我认为参数可以与组合使用tuple,但是我认为这不是最好的方法。

我的问题是:如何正确注释带有两个或多个返回参数的函数?

请在您的回复中加入PEP链接(如果有)。我在PEP 484PEP 3107上寻找答案,但找不到。

python type-hinting python-3.x

16
推荐指数
1
解决办法
1048
查看次数

Go模板:无法评估Y类型中的字段X(X不是Y的一部分,但卡在{{range}}循环中)

类似的问题在这里得到了回答,但我不认为它解决了我的问题.

假设你有以下结构:

type User struct {
    Username string
    Password []byte
    Email string
    ...
}
Run Code Online (Sandbox Code Playgroud)

此外,URL的结构如下:example.com/en/users,其中"en"是一个URL参数,将被传递到模板中,如下所示:

renderer.HTML(w, http.StatusOK, "users/index", map[string]interface{}{
  "lang":  chi.URLParam(r, "lang"),
  "users": users})
Run Code Online (Sandbox Code Playgroud)

在HTML模板中,我有以下内容:

{{ range .users }}
  <form action="/{{ .lang }}/users" method="POST">
    <input type="text" name="Username" value="{{ .Username }}">
    <input type="text" name="Email" value="{{ .Email }}">
  </form>
{{ end }}
Run Code Online (Sandbox Code Playgroud)

现在,问题是因为{{.lang}}不是User结构的一部分,所以我得到了错误..那么如何在{{range .users}}中访问{{.lang}}?

go go-templates

13
推荐指数
2
解决办法
1万
查看次数

Why does a PySpark UDF that operates on a column generated by rand() fail?

Given the following Python function:

def f(col):
    return col
Run Code Online (Sandbox Code Playgroud)

If I turn it into a UDF and apply it to a column object, it works...

from pyspark.sql import functions as F
from pyspark.sql.types import DoubleType

df = spark.range(10)
udf = F.udf(f, returnType=DoubleType()).asNondeterministic()

df.withColumn('new', udf(F.lit(0))).show()
Run Code Online (Sandbox Code Playgroud)

...Except if the column is generated by rand:

df.withColumn('new', udf(F.rand())).show()  # fails
Run Code Online (Sandbox Code Playgroud)

However, the following two work:

df.withColumn('new', F.rand()).show()
df.withColumn('new', F.rand()).withColumn('new2', udf(F.col('new'))).show()
Run Code Online (Sandbox Code Playgroud)

The error:

Py4JJavaError: An error occurred while calling o469.showString.
: org.apache.spark.SparkException: Job …
Run Code Online (Sandbox Code Playgroud)

python apache-spark pyspark

6
推荐指数
1
解决办法
418
查看次数

sklearn的classification_report中,avg/total是什么意思?它是如何计算的?

              precision    recall  f1-score   support

          0       0.98      0.90      0.94       305
          1       0.77      0.93      0.84       102
avg / total       0.92      0.91      0.91       407    
Run Code Online (Sandbox Code Playgroud)

平均/总计是什么意思?以及它是如何计算的?在TAT官方网站上没有找到答案

scikit-learn

5
推荐指数
1
解决办法
1866
查看次数

sys:1: RuntimeWarning: 协程从未被等待

我正在尝试编写一个请求处理程序来帮助我以异步模式发送请求。当我用 Ctrl+D 或 exit() 关闭 python 终端时它会提示

表明 sys:1: RuntimeWarning: coroutine was never awaited

import asyncio
import urllib.request
import json 

class RequestHandler:
    def SendPostRequest(method="post",url=None, JsonFormatData={}):
        # Encode JSON
        data =json.dumps(JsonFormatData).encode('utf8')
        # Config Request Header
        req = urllib.request.Request(url)
        req.add_header('Content-Type', 'application/json')      
        # Send request and wait the response
        response = urllib.request.urlopen(req,data=data)    
        return response 

    async def AsyncSend(method="post",url=None, JsonFormatData=None):
        if method == "post":
            loop = asyncio.get_event_loop()
            task = loop.create_task(SendPostRequest(method="post",url=url,JsonFormatData=JsonFormatData))

###################################
# Example
##### In main python terminal, i run like this:
# from RequestHandler …
Run Code Online (Sandbox Code Playgroud)

coroutine python-3.x python-asyncio

5
推荐指数
2
解决办法
4万
查看次数

在线python代码运行时出错,离线工作

代码离线成功运行,但是,当上传到代码挑战站点时,它会给我一个RuntimeError. 我不确定它为什么这样做。

我的代码:

inputinteger = int(input(""))
N = inputinteger

inputinteger2 = int(input(""))
M = inputinteger2

A = int(N / M)

if (N % M == 0):
    B = 0
    print("masing-masing " + str(A))
    print("bersisa " + str(B))

else:
    B = int(N % M);
    print("masing-masing " + str(A))
    print("bersisa " + str(B))
Run Code Online (Sandbox Code Playgroud)

注意:“masing-masing”在英文中的意思是“每个”。“bersisa”在英语中的意思是“剩余部分”。

输入一些测试输入153给出预期结果。但是,该代码在在线运行时不起作用。

感谢您的帮助!

python runtime-error

5
推荐指数
1
解决办法
451
查看次数