小编atk*_*t12的帖子

通过tweepy从"user_timeline"获取完整的推文文本

我使用tweepy使用此处包含的脚本从用户的时间线获取推文.但是,这些推文正在被截断:

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
new_tweets = api.user_timeline(screen_name = screen_name,count=200, full_text=True)
Run Code Online (Sandbox Code Playgroud)

返回:

Status(contributors=None, 
     truncated=True, 
     text=u"#Hungary's new bill allows the detention of asylum seekers 
          & push backs to #Serbia. We've seen push backs before so\u2026 https:// 
          t.co/iDswEs3qYR", 
          is_quote_status=False, 
          ...
Run Code Online (Sandbox Code Playgroud)

也就是说,对某些人来说i,new_tweets[i].text.encode("utf-8")似乎是

#Hungary's new bill allows the detention of asylum seekers & 
push backs to #Serbia. We've seen push backs before so…https://t.co/
iDswEs3qYR
Run Code Online (Sandbox Code Playgroud)

...在这通常会在Twitter上显示后更换文本.

有谁知道如何覆盖truncated=True我的请求获取全文?

python twitter tweepy

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

由于精度问题,Shapely 无法在点上分割线

我试图将一个点插入到LineStringShapely 中,然后相应地分割线串。但是,由于精度误差,Shapely 认为插值点不在 上linestring,因此该split操作不起作用。

这是一个例子:

from shapely.ops import split
from shapely.geometry import LineString, Point

### Initialize point and line
line = LineString([(0.123,0.456),(5.678,7.890),(12.135,6.789)])    
point = Point(4.785,8.382)   

### Interpolate point onto line
new_point = line.interpolate(line.project(point))
print new_point
>> POINT (5.593949278213755 7.777518800043393)

### BUT: line does not intersect the interpolated point
line.intersects(new_point)
>> False

### EVEN THOUGH: distance between them is essentially (not exactly) zero
line.distance(new_point)
>> 0.0

### THEREFORE: line cannot be split using the new …
Run Code Online (Sandbox Code Playgroud)

python shapely

12
推荐指数
2
解决办法
3162
查看次数

Python'map'函数插入NaN,可能会返回原始值吗?

我正在将字典传递给map函数以重新编码Pandas数据帧列中的值.但是,我注意到如果原始系列中的某个值未在字典中显式显示,则会将其重新编码NaN.这是一个简单的例子:

打字...

s = pd.Series(['one','two','three','four'])
Run Code Online (Sandbox Code Playgroud)

...创建系列

0      one
1      two
2    three
3     four
dtype: object
Run Code Online (Sandbox Code Playgroud)

但是应用地图......

recodes = {'one':'A', 'two':'B', 'three':'C'}
s.map(recodes)
Run Code Online (Sandbox Code Playgroud)

...返回系列

0      A
1      B
2      C
3    NaN
dtype: object
Run Code Online (Sandbox Code Playgroud)

我希望如果系列中的任何元素s不在recodes字典中,它将保持不变.也就是说,我更愿意返回下面的系列(原文four而不是NaN).

0      A
1      B
2      C
3   four
dtype: object
Run Code Online (Sandbox Code Playgroud)

有没有一种简单的方法可以做到这一点,例如传递给map函数的选项?我遇到的挑战是,我无法始终预测将要重新编码的系列中的所有可能值 - 数据将在未来更新,并且可能会出现新值.

谢谢!

python pandas map-function

11
推荐指数
2
解决办法
5681
查看次数

带日期滑块的iPyWidget?

我想知道是否有一种简单的方法来构建一个带有日期时间滑块的iPyWidget.现在很容易在整数或浮点范围上滑动(例如数字1-10,小数0.01,0.02,......).

我想你可以将日期转换为浮点数或整数,使用这些生成某种滑块,然后转换回滑块上显示标签的日期.然而,这似乎很笨拙.有没有人有更顺畅的解决方案?

python ipython jupyter jupyter-notebook ipywidgets

6
推荐指数
2
解决办法
5729
查看次数

Tensorflow 线性回归结果与 Numpy/SciKit-Learn 不匹配

我正在研究 Aurelien Geron 的“Hands-On Machine Learning”一书中的 Tensorflow 示例。但是,我无法在此支持笔记本中复制简单的线性回归示例。为什么 Tensorflow 与 Numpy/SciKit-Learn 结果不匹配?

据我所知,没有优化(我们使用的是正规方程,所以它只是矩阵计算),而且答案似乎太不同了,不可能是精度误差。

import numpy as np
import tensorflow as tf
from sklearn.datasets import fetch_california_housing

housing = fetch_california_housing()
m, n = housing.data.shape
housing_data_plus_bias = np.c_[np.ones((m, 1)), housing.data]

X = tf.constant(housing_data_plus_bias, dtype=tf.float32, name="X")
y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name="y")
XT = tf.transpose(X)
theta = tf.matmul(tf.matmul(tf.matrix_inverse(tf.matmul(XT, X)), XT), y)

with tf.Session() as sess:
    theta_value = theta.eval()

theta_value
Run Code Online (Sandbox Code Playgroud)

回答:

array([[ -3.74651413e+01],
       [  4.35734153e-01],
       [  9.33829229e-03],
       [ -1.06622010e-01],
       [  6.44106984e-01],
       [ -4.25131839e-06], …
Run Code Online (Sandbox Code Playgroud)

python numpy linear-regression scikit-learn tensorflow

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

Flask:redirect(url_for())返回html但不加载页面

我正在Flask中构建一个小型Web应用程序,代码摘录如下.基本上,我试图存储一些数据startsession,然后tagpage一旦实现就继续.但是redirect没有用.

当我运行时TEST 1,它会显示hello, world在控制台中,但主页看起来是一样的.
当我运行时TEST 2,它会在控制台中显示html,但主页看起来是一样的.

这是我有的:

@app.route('/startsession', methods=['POST'])
def startsession(): 
    _username = request.form['inputName']
    session['user_name'] = _username    
    ...
    return  redirect(url_for('tagpage')) 
Run Code Online (Sandbox Code Playgroud)

然后,要么:

### TEST 1
@app.route("/tagpage")
def tagpage():
    return "hello, world"
Run Code Online (Sandbox Code Playgroud)

要么:

### TEST 2
@app.route("/tagpage")
def tagpage():
    return render_template('tagpage.html', filename=filename)
Run Code Online (Sandbox Code Playgroud)

单击触发按钮后我看到的示例startsession:

在此输入图像描述

我怎样才能让浏览器真正去http://localhost:5000/tagpage

更新:这是我的JQuery脚本:

$(function(){
    $("#btnStartSession").click(function(){     
        $.ajax({
            url: '/startsession',
            type: 'POST',
            data: $('form').serialize(),
            success: function(response){
                console.log(response);
            },
            error: function(error){
                console.log(error);
            }
        });             


    });
});
Run Code Online (Sandbox Code Playgroud)

python redirect flask

0
推荐指数
1
解决办法
4889
查看次数