Django REST framework foreignkey 序列化

Django REST framework is a powerful and flexible toolkit for building Web APIs.

Some reasons you might want to use REST framework:

之前虽然也用了Django REST framework 但是序列化函数基本都是自己写的,并没有用框架带的序列化函数。这次不想在搞的那么麻烦,于是使用Django REST framework带的序列化函数。

但是在序列化foreignkey的时候却发现只有id,其余的数据没有。

model定义:

class PlayerGoodsItem(models.Model):
    create = models.DateTimeField(default=timezone.now, help_text='创建时间')
    update = models.DateTimeField(auto_now=True, help_text='修改时间')
    goods_item = models.ForeignKey(GoodsItem, related_name='goodsitem_playergoodsitem', help_text='商品信息',
                                   on_delete=models.CASCADE)

序列化代码:

class PlayerGoodsItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = PlayerGoodsItem
        fields = "__all__"

Continue Reading

基于DFA的敏感词过滤

在计算理论中,确定有限状态自动机或确定有限自动机(英语:deterministic finite automaton, DFA)是一个能实现状态转移的自动机。对于一个给定的属于该自动机的状态和一个属于该自动机字母表{\displaystyle \Sigma }Σ的字符,它都能根据事先给定的转移函数转移到下一个状态

DFA算法

DFA((Deterministic Finite automation))确定性的有穷状态自动机: 从一个状态输入一个字符集合能到达下一个确定的状态。如图:

 
dfa_1.png

如上图当AB状态输入a得到状态aB,状态aB输入b得到状态ab; 状态AB输入b得到状态Ab,状态Ab输入a得到状态ab。

Continue Reading

阿里云 EC2 CentOS 6.0 系统分区扩容

阿里云的虚拟主机磁盘空间满了,这个主要是用来放blog的,所以并没有其他的数据盘。所有的数据都是直接放到/dev/vda1磁盘下的,数据库也是在这个盘下。从阿里云的后台扩容了磁盘发现还需要登陆进行扩容,于是开始按照教程操作,事实证明给出的文档(https://help.aliyun.com/document_detail/25452.html?spm=a2c4g.11186623.6.788.666a3f87QwRXFB)里面方法比较蛋疼,对于扩展系统分区不大适用。 

后来发现貌似看的文档有问题,如果要扩展系统盘应该看这个https://help.aliyun.com/document_detail/111738.html?spm=5176.2020520101.0.0.44d34df5aZCEKE。 这个就尴尬了 smile ,没看好文档。

安装growpart

yum install cloud-utils-growpart

然后对系统分区进行扩容:

growpart /dev/vda 1

Continue Reading

阿里云 安卓push无法收到消息

官方给的demo代码如下:

    request = PushMessageToAndroidRequest.PushMessageToAndroidRequest()
    request.set_AppKey(appKey)
    request.set_Target('ALL')
    request.set_TargetValue('ALL')
    request.set_Title("PushMessageToAndroid title")
    request.set_Body("Message from ali push Open Api2.0 : PushMessageToAndroid")

    request.add_query_param('AndroidNotificationChannel', '1')

    result = clt.do_action(request)
    print result

这个代码有两个问题,如果和另外一份demo代码中的:

clt = client.AcsClient(properties.accessKeyId,properties.accessKeySecret,properties.regionId)

request = PushMessageToAndroidRequest.PushMessageToAndroidRequest()
request.set_AppKey(properties.appKey)
request.set_Target('ALL')
request.set_TargetValue('ALL')
request.set_Title("PushMessageToAndroid title")
request.set_Body("Message from ali push Open Api2.0 : PushMessageToAndroid")
result = clt.do_action(request)

很可能会出现下面的错误信息:



    EABF26DA-89EA-44BA-B16E-117FE03D29C7
    cloudpush.aliyuncs.com
    Throttling.User
    Request was denied due to user flow control.

这个错误信息按照官方的解释是限流了,但是实际并不是,可以尝试分开调用,不要连续进行push message和push notice。

另外一个问题是: 这份代码在对于最新的安卓8.0以上系统的消息无法正常弹出,按照文档解释需要设置NotificationChannel(文档链接:https://help.aliyun.com/knowledge_detail/67398.html)。

Continue Reading

jupyter notebook 调整字体 以及matplotlib显示中文

原生的jupyter theme看起来比较蛋疼,尤其是字体和字号。为了修改这个配置可以安装 jupyter theme。

项目链接: https://github.com/dunovank/jupyter-themes 如果不喜欢英文可以参考这个链接:https://www.jianshu.com/p/6de5f6cce06d

上面的样式对应的配置命令:
jt  -f fira -fs 11 -cellw 90% -ofs 11 -dfs 11 -T -t solarizedl

除此之外matplotlib 默认不支持中文显示,主要是字体问题,可以通过下面的代码配置来让matplotlib 支持中文

from matplotlib import pyplot as plt
%matplotlib inline
font = {'family' : 'MicroSoft YaHei',
'weight' : 'bold',
'size' : 10}
plt.rc("font", **font)

实际效果,另外还可以使用altair ,altair 默认支持中文显示 https://altair-viz.github.io

基于RandomForestClassifier的titanic生存概率分析

The Challenge

The sinking of the Titanic is one of the most infamous shipwrecks in history.

On April 15, 1912, during her maiden voyage, the widely considered “unsinkable” RMS Titanic sank after colliding with an iceberg. Unfortunately, there weren’t enough lifeboats for everyone onboard, resulting in the death of 1502 out of 2224 passengers and crew.

While there was some element of luck involved in surviving, it seems some groups of people were more likely to survive than others.

In this challenge, we ask you to build a predictive model that answers the question: “what sorts of people were more likely to survive?” using passenger data (ie name, age, gender, socio-economic class, etc).

这个是kaggle上的一个基础项目,目的是探测泰坦尼克号上的人员的生存概率,项目地址:https://www.kaggle.com/c/titanic

网上基于这个项目其实可以找到各种各样的解决方案,我也尝试了不同的做法。但是实际的效果并不是十分好,个人尝试最好的成绩是0.78468,一次是基于深度神经网络,另外一次就是基于当前的随机森林的模型。

另外还可以看到一系列score为1的提交,这些不知道是怎么做到的,真是太tm牛了~~

Continue Reading