2017年9月9日 星期六

利用OpenData研究北市房價與性侵地點的關係

一直很想練習用python去做一些資料分析和視覺化服務
所以就先簡單的找個例子來做實驗
想知道台北市各個行政區的房價是不是跟性侵案件的多寡有正相關
我們要從從Data.Taipei取得婦幼安全相關的資料
內政部提供的實價登錄資料
兩個資料我都取2017年第一季跟第二季加在一起來實驗

先處理房價部分的資料
我先用excel將第一季跟第二季的資料做結合
接下來用python去解析資料
import pandas去做讀取csv
import pandas
from pandas import DataFrame 
df = pandas.read_csv('/Users/Chou/Downloads/2017S1/A_lvr_land_A1_S1+S2.CSV',encoding = "big5") # 要encodig成big5不然會有亂碼

萬一遇到UnicodeDecodeError的問題
因為資料裡面可能含有一些不乾淨的byte
使用iconv講檔案處理乾淨
iconv -c -f big5 -t big5 A_lvr_land_A.CSV > A_lvr_land_A2.CSV
這時候把df印出來應該可以得到如下的資料
接下來可以看到交易標的的欄位有一些是單純的車位販售,引此不列入我們這次要抓取的資料

dfWithoutCar = df[df['交易標的'] != '汽車'] 
然後將所有的行政區集合起來並且算出平均數還有資料數量
groupByCity = dfWithoutCar.groupby(["鄉鎮市區"]).agg(['mean', 'count'])

將取出來的資料從高排到低
再將所有的mean(也就是我們要用的價錢)乘上3,
因為資料提供的單位是每平方公尺,轉換成每坪的價錢比較好讀
orderByPricePerPing = DataFrame(orderByPrcingPerMPlus)
for index, row in orderByPrcingPerMPlus.items():
    if(index == 'mean'):
        for i,price in row.items():
            orderByPricePerPing[index][i] = price * 3
orderByPricePerPing
將最後需要的資料取出來就是我們要用的北市行政區的資料了

2015年3月24日 星期二

Install MongoDB on CentOS 7

1.新增 MongoDB Repository
vim /etc/yum.repos.d/mogodb.repo
64位元版本
[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1

32位元版本
[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686/
gpgcheck=0
enabled=1

2.Install MongoDB
yum -y update
yum -y install mongodb-org mongodb-org-server

3.Get MongoDD Running

啟動MongoDB
systemctl start mongod
確認MongoDB狀態
systemctl status mongod
持續顯示MongoDB狀態
mongostat
每兩秒更新一次狀態(一次五列)
mongostat --rowcount 5 2
進入MongoDB控制台
mongo
預設port是27017

更改port設定
mongo --port 22222
關閉MongoDB服務
systemctl stop mongod

補充
PHP連結MongoDB

sudo pecl install mongo

編輯php.ini 加入下列指令
extension=mongo.so

Nginx + PHP-fpm 重新啟動
sudo service php-fpm restart
service nginx restart
ˊ





Nginx+CentOS&7 安裝流量分析工具 Awstats

1.下载awstats
cd /usr/local sudo unzip /tmp/awstats-7.2.zip
sudo mv awstats-7.2 awstats

2.以linode.tripresso.com為例,建立新的設定
/etc/awstats/awstats.linode.tripresso.com.conf
cd /usr/local/awstats
perl tools/awstats_configure.pl
# 根据提示依次输入
# web server config file path: none
# build a new AWStats config/profile: y
# awstats config file name: linode.tripresso.com(改成自己的网站名即可)
# awstats config file path: (不填,使用默认值/etc/awstats)

# 编辑配置文件
sudo vi /etc/awstats/awstats.linode.tripresso.com.conf
# 将LogFile改为nginx的access日志的位置
# LogFile="/var/log/nginx/access.log"

sudo chown -R nginx:nginx /usr/local/awstats/wwwroot

# 修改awstats的输出目录为/usr/local/awstats/wwwroot/output
# DirData="/usr/local/awstats/wwwroot/output"
sudo -u linode mkdir /usr/local/awstats/wwwroot/output

# 测试新生成的配置文件是否有效
sudo ./wwwroot/cgi-bin/awstats.pl -config=linode.tripresso.com
3.安裝Spawn-fcgi(要先安裝epel-release)
sudo yum install epel-release
yum update
yum install spawn-fcgi
4.安裝fcgiwrap(連結)
yum -y install fcgi-devel
cd /usr/local/src/
git clone git://github.com/gnosek/fcgiwrap.git
cd fcgiwrap
autoreconf -i
./configure
make
make install
vim /etc/sysconfig/spawn-fcgi

# You must set some working options before the "spawn-fcgi" service will work.
# If SOCKET points to a file, then this file is cleaned up by the init script.
#
# See spawn-fcgi(1) for all possible options.
#
# Example :
#SOCKET=/var/run/php-fcgi.sock
#OPTIONS="-u apache -g apache -s $SOCKET -S -M 0600 -C 32 -F 1 -P /var/run/spawn-fcgi.pid -- /usr/bin/php-cgi"

FCGI_SOCKET=/var/run/fcgiwrap.socket
FCGI_PROGRAM=/usr/local/sbin/fcgiwrap
FCGI_USER=apache
FCGI_GROUP=apache
FCGI_EXTRA_OPTIONS="-M 0770"
OPTIONS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -S $FCGI_EXTRA_OPTIONS -F 1 -P /var/run/spawn-fcgi.pid -- $FCGI_PROGRAM"
usermod -a -G apache nginx
chkconfig spawn-fcgi on
systemctl start spawn-fcgi

現在應該可以在 /var/run/fcgiwrap.socket  找到fcgiwrap socket  

5.設定權限及host

sudo chown nginx:nginx fcgiwrap.socket
vim /etc/nginx/conf.d/vhost.conf 
寫入以下內容
server {
    listen 80;
    root /usr/local/awstats/wwwroot;
    server_name awstats.atime.me;

    location ~ \.pl$ {
        gzip off;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
        fastcgi_index index.pl;
    }
}
sudo service nginx reload
chmod -R 777 /usr/local/awstats/wwwroot/

補充:
更新流量分析
/usr/local/awstats/tools/awstats_updateall.pl now