使用pandas数据帧进行线性回归的推荐方法(如果有的话)是什么?我可以做到,但我的方法似乎非常精细.我制作的东西是不必要的复杂吗?
R代码,用于比较:
x <- c(1,2,3,4,5)
y <- c(2,1,3,5,4)
M <- lm(y~x)
summary(M)$coefficients
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.6 1.1489125 0.522233 0.6376181
x 0.8 0.3464102 2.309401 0.1040880
Run Code Online (Sandbox Code Playgroud)
现在,我的python(2.7.10),rpy2(2.6.0)和pandas(0.16.1)版本:
import pandas
import pandas.rpy.common as common
from rpy2 import robjects
from rpy2.robjects.packages import importr
base = importr('base')
stats = importr('stats')
dataframe = pandas.DataFrame({'x': [1,2,3,4,5],
'y': [2,1,3,5,4]})
robjects.globalenv['dataframe']\
= common.convert_to_r_dataframe(dataframe)
M = stats.lm('y~x', data=base.as_symbol('dataframe'))
print(base.summary(M).rx2('coefficients'))
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.6 1.1489125 0.522233 0.6376181
x 0.8 0.3464102 2.309401 0.1040880
Run Code Online (Sandbox Code Playgroud)
顺便说一下,我确实得到了一个FutureWarning的导入 …
升级到Django 1.9后,我现在收到警告
RemovedInDjango110Warning: SubfieldBase has been deprecated. Use Field.from_db_value instead.
Run Code Online (Sandbox Code Playgroud)
我知道问题出在哪里.我有一些自定义字段定义,其中我有__metaclass__ = models.SubfieldBase.例如,
class DurationField(models.FloatField):
__metaclass__ = models.SubfieldBase
def __init__(self, *args, **kwargs):
...
Run Code Online (Sandbox Code Playgroud)
如果该__metaclass__语句被弃用,我应该用什么来替换它呢?
我是否只是将其取出并添加一个from_db_value方法,如下例所示:https://docs.djangoproject.com/en/1.9/howto/custom-model-fields/#converting-values-to-python-objects
?
以及如何from_db_value和to_python不同?两者似乎都将数据库数据转换为Python对象?
scipy ConvexHull(参见http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.ConvexHull.html)对象中的"area"属性的值似乎不是(我理解为be)凸壳的面积.另一方面,"体积"的值似乎确实是凸包的面积.
from scipy.spatial import ConvexHull
import numpy
points = numpy.array([[-1,-1], [1,1], [-1, 1], [1,-1]])
hull = ConvexHull(points)
print("Volume is %2.2f" % hull.volume) # Prints 4.00
print("Area is %2.2f" % hull.area) # Prints 8.00
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我预计4点凸壳的面积为4.0.那就是"音量".那么"地区"给了我们什么?
是否可以在运行时动态加载django应用程序?通常,应用程序在初始化时使用settings.py中的INSTALLED_APPS元组加载.但是,是否可以在运行时加载其他应用程序?我在不同情况下遇到这个问题.例如,在测试期间,当我想动态加载或卸载应用程序时,会出现一种情况.
为了使问题更具体,想象一下我有一个目录apps,我把我的应用程序放在哪里,我想自动安装任何进入那里的新应用程序,而无需手动编辑settings.py.
这很容易.按照示例代码进行操作
Django:动态添加应用程序作为插件,自动构建网址和其他设置
我们将以下代码放入settings.py可以循环遍历app目录中所有子目录的名称,并像这样增加INSTALLED_APPS元组settings.py:
APPS_DIR = '/path_to/apps/'
for item in os.listdir(APPS_DIR):
if os.path.isdir(os.path.join(APPS_DIR, item)):
app_name = 'apps.%s' % item
if app_name not in INSTALLED_APPS:
INSTALLED_APPS += (app_name, )
Run Code Online (Sandbox Code Playgroud)
在那之后,如果我在django shell中,我可能会喜欢
from django.conf import settings
Run Code Online (Sandbox Code Playgroud)
并且应用程序将列在settings.INSTALLED_APPS.如果我做了
from django.core import management
management.call_command('syncdb', interactive=False)
Run Code Online (Sandbox Code Playgroud)
这将为应用程序创建必要的数据库表.
但是,如果我现在要在apps/目录中添加更多应用程序而不重新启动,则这些应用程序不会在settings.INSTALLED_APPS中列出,因此后续调用syncdb将无效.
我想知道的是,如果有什么我可以做 - 没有重新启动---重新加载设置和加载/安装新的应用程序.
我试图直接导入我的settings.py,即从myproject导入设置
然后reload是settings使用任何内置后的蟒蛇app目录更改.虽然现在更改了settings.INSTALLED_APPS以包含新添加的应用,但这最终没有任何区别.例如,
from django.db import models
models.get_apps()
Run Code Online (Sandbox Code Playgroud)
仅显示原始应用程序,apps而不是新添加的应用程序
management.call_command('syncdb', …Run Code Online (Sandbox Code Playgroud) 我有一个weight.csv包含以下内容的 csv 文件。
weight,weight_selfreport
81.5,81.66969147005445
72.6,72.59528130671505
92.9,93.01270417422867
79.4,79.4010889292196
94.6,96.64246823956442
80.2,79.4010889292196
116.2,113.43012704174228
95.4,95.73502722323049
99.5,99.8185117967332
Run Code Online (Sandbox Code Playgroud)
如果我做
library(readr)
Df <- read_csv('weight.csv')
Df
Run Code Online (Sandbox Code Playgroud)
我得到
# A tibble: 9 x 2
weight weight_selfreport
<dbl> <dbl>
1 81.5 81.7
2 72.6 72.6
3 92.9 93.0
4 79.4 79.4
5 94.6 96.6
6 80.2 79.4
7 116. 113.
8 95.4 95.7
9 99.5 99.8
Run Code Online (Sandbox Code Playgroud)
如果我将该小标题转换为普通数据框,我会看到更多数字。
as.data.frame(Df)
weight weight_selfreport
1 81.5 81.66969
2 72.6 72.59528
3 92.9 93.01270
4 79.4 79.40109
5 94.6 96.64247
6 80.2 …Run Code Online (Sandbox Code Playgroud) 在我的一个django测试中,我使用的方法django.utils.importlib.import_module类似于它在这里使用的方式.
在升级到django 1.8时,我收到了弃用警告
test_views.py:20: RemovedInDjango19Warning: django.utils.importlib will be removed in Django 1.9.
from django.utils.importlib import import_module
Run Code Online (Sandbox Code Playgroud)
是推荐的做法,现在使用import_module的importlib标准库(这似乎很好地工作)?或者建议不要import_module完全使用?
使用 RMarkdown 我总是通过 Rmd -> pandoc -> TeX -> pdflatex -> pdf 生成 pdf 文档,并使用以下示例中的方式\\label{something}完成图形引用:fig.cap
---
title: "Foo"
author: "Mark Andrews"
date: "10 January 2019"
output: pdf_document
---
See Figure \ref{figfoo} and see Figure \ref{figbar}.
```{r fig1, fig.cap='A figure\\label{figfoo}'}
plot(rnorm(10))
```
```{r fig2, fig.cap='Another figure\\label{figbar}'}
plot(rnorm(10))
```
Run Code Online (Sandbox Code Playgroud)
如果我更改output: pdf_document为output: html_document,则不起作用,这是可以理解的,因为它依赖于 LaTeX 的交叉引用系统。
那么 RMarkdown 中的图引用是如何工作的呢html_document?
以下不起作用:
---
title: "Foo"
author: "Mark Andrews"
date: "10 January 2019"
output: html_document
---
See Figure …Run Code Online (Sandbox Code Playgroud) 将python social auth添加到我安装的应用程序后,即
INSTALLED_APPS = (
...
'social.apps.django_app.default',
...
)
Run Code Online (Sandbox Code Playgroud)
然后尝试一个
python manage.py makemigrations
Run Code Online (Sandbox Code Playgroud)
我得到一个不足为奇的权限错误
Migrations for 'default':
0002_auto_20150217_2053.py:
- Alter field user on usersocialauth
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/lib/python2.7/site-packages/django/core/management /__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/usr/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 124, in handle
self.write_migration_files(changes)
File "/usr/lib/python2.7/site-packages/django/core/management/commands /makemigrations.py", …Run Code Online (Sandbox Code Playgroud) 我想从重组结构化文本字符串中的代码指令中逐字提取源代码。
接下来是我第一次尝试这样做,但是我想知道是否有更好的方法(即更健壮或更通用或更直接)来做到这一点。
假设我在Python中将以下第一个文本作为字符串:
s = '''
My title
========
Use this to square a number.
.. code:: python
def square(x):
return x**2
and here is some javascript too.
.. code:: javascript
foo = function() {
console.log('foo');
}
'''
Run Code Online (Sandbox Code Playgroud)
要获得两个代码块,我可以做
from docutils.core import publish_doctree
doctree = publish_doctree(s)
source_code = [child.astext() for child in doctree.children
if 'code' in child.attributes['classes']]
Run Code Online (Sandbox Code Playgroud)
现在,source_code是仅包含两个代码块中逐字记录源代码的列表。如果需要,我也可以使用child的attribute属性来找出代码类型。
它可以完成工作,但是有更好的方法吗?
我在使用ctrl键的vim中的键盘映射遇到了一些奇怪的行为.我猜这有一个简单的原因和解决方案,但我只是看不到它.
在重构文本的编辑过程中,我发现自己输入了类似的内容
:math:`x`
Run Code Online (Sandbox Code Playgroud)
经常(这个:数学:角色会导致刻度内的任何内容被排版为例如乳胶输出中的数学).
我想映射一个键,如m输入:math:``进入文本并将光标定位在刻度内.
我做到了这一点
map m i:math:``ha
Run Code Online (Sandbox Code Playgroud)
这似乎工作正常.
但是,我希望能够在插入模式下使用此映射.为此,我认为使用ctrl+ m将是最好的.我弄完了
imap <c-m> :math:``ha
Run Code Online (Sandbox Code Playgroud)
虽然正确输入:math:``并在我执行ctrl+ 时将光标定位在刻度内m,但问题是在此之后,每次按下enter插入模式时,它都会运行与我输入ctrl+ 相同的命令m.换句话说,现在进入插入模式似乎映射到
:math:``ha
Run Code Online (Sandbox Code Playgroud)
同样.
看起来它肯定与使用ctrl密钥有关.如果我F5按如下方式绑定密钥
imap <F5> :math:``ha
Run Code Online (Sandbox Code Playgroud)
一切都好.
我可以使用eg F5键并为自己节省更多麻烦,但我想知道将来会发生什么.
ctrl在关键地图中使用密钥是否存在基本缺失?
谢谢,
django ×4
python ×3
r ×2
bookdown ×1
convex-hull ×1
django-1.8 ×1
django-apps ×1
docutils ×1
knitr ×1
mapping ×1
pandas ×1
r-markdown ×1
rpy2 ×1
scipy ×1
tibble ×1
tidyverse ×1
vim ×1