IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

2015-01-01から1年間の記事一覧

const id

constとconst idの違い Prelude> const 'a' 1 'a' -- 第1引数を返す Prelude> (const id) 'a' 1 1 -- 第2引数を返す 型の確認 const idは第2引数を返すことがわかる。 Prelude> :t const id const id :: b -> a -> a 型の計算 Prelude> :t const const :: a …

clojureにcdr, cddr, cdddr, cddddr がない件

自分で定義するのが正解なのでしょうか? user=> (defn cdr [x] (rest x)) #'user/cdr user=> (defn cddr [x] (cdr (cdr x))) #'user/cddr user=> (defn cdddr [x] (cdr (cdr (cdr x)))) #'user/cdddr user=> (defn cddddr [x] (cdr (cdr (cdr (cdr x))))) #…

ClassCastException java.lang.Boolean cannot be cast to clojure.lang.IFn

cond関数を使っていたら表題のエラーがでました。 user=> (defn foo [x] (cond ((= x 2) "two") ((= x 3) "three"))) #'user/foo user=> (foo 3) ClassCastException java.lang.Boolean cannot be cast to clojure.lang.IFn user/foo (form-init268103478811…

Lisp事始め

Lispを始めるには? Lispを使うにはどの実装を使えば良いのでしょうか? CommonLisp系ではこちらにたくさん掲載されています。 CLiki: Common Lisp implementation Lisps 当然、どれを使うか迷うわけで、質問することになります。 programmers.stackexchange…

(.f)は定義域を書き換えると思ってみる。

用語の使い方が雑ですが、、、。 理解したかったこと Prelude> :t Data.Char.chr Data.Char.chr :: Int -> Char のときに Prelude> :t (. Data.Char.chr) (. Data.Char.chr) :: (Char -> c) -> Int -> c となる理由がよくわからなかったのでメモ。 考え方 g …

Tree Iterator

修正したい。。 github.com

Android Studioで通常のJava(メインメソッド)を実行する

Android Studio のバーション メインメソッドをもつJavaクラスを作る。 実行 -> 構成 を選択する。 構成ダイアログの右上のプラスを押す。 アプリケーションを選択する。 構成名称を記入する。 実行するJavaクラスを記入する。 モジュールを選択する。 Java…

ContentProvider

ContentProvider <--> Activity com.example.cp01 <--> com.example.cp02 ContentProvider Application (com.example.cp01) manifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.cp01"> </manifest>

ドラックアンドドロップ(Drag and Drop)

ドラックアンドドロップのサンプル package com.example.dnd02; import android.content.ClipData; import android.graphics.Color; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.DragEvent; import an…

fmap f = bind (return . f)

fmap f = bind (return . f)を確認する。 まず、定義の確認 fmap :: (a -> b) -> (W a -> W b) --- {*4} fmap f (W x) = W (f x) --- {*1} return :: a -> W a --- {*6} return x = W x --- {*3} bind :: (a -> W b) -> (W a -> W b) --- {*7} bind f (W x) …

HaskellのEitherとeither

Eitherという型がありますが、eitherという関数もあります。 Either型 Data.Either Prelude> :i Either data Either a b = Left a | Right b -- Defined in `Data.Either' instance (Eq a, Eq b) => Eq (Either a b) -- Defined in `Data.Either' instance M…

Haskellで例外処理の初歩

import System.Environment (getArgs) import Control.Exception (catch, SomeException) main :: IO () main = do args <- getArgs out <- catch (readFile $ head args ) (\_ -> return "error") print out test$ runghc test02.hs test02.hs1 test02.hs:1…

Haskellでコマンドライン実行で引数をとる

getArgsを使う import System.Environment (getArgs) main :: IO () main = do args <- getArgs print args test$ runghc test02.hs [] test$ runghc test02.hs a ["a"] test$ runghc test02.hs a b ["a","b"] test$ runghc test02.hs ab ["ab"] test$ rungh…

Pandasのgroupbyの結果をDateFrameにする

groupbyでは、groupieオブジェクトが返却されるが、DataFrameが欲しいときがある。その場合は、as_index=Falseを指定する。 In [70]: type(df.groupby(['項目1','項目2'],as_index=False).agg(np.average)) Out[70]: pandas.core.frame.DataFrame stackoverf…

PandasのDataFrameの嵌りどころ

データフレームから複数条件で行を絞るとき df[df['項目1']==0 and df['項目2']==1] df[df['項目1']==0 & df['項目2']==1] df[(df['項目1']==0) and (df['項目2']==1)] 上記3つはいずれも下記のエラーが出る。 ValueError: The truth value of a Series is …

Pythonでピボットテーブル

件数を数える。 import pandas df = pandas.read_csv(CSVファイル) df[[項目1,項目2]].pivot_table(index=項目1, columns=項目2, aggfunc=len) 単に1項目のみの件数を求めるなら、groupbyを使うよりvalue_countsのほうが手軽。 import pandas df = pandas.re…

matplotlibでヒストグラム

数値データとカテゴリデータではやり方が若干異なる。 $ ipython --version 3.2.0 %matplotlib inline import pandas df = pandas.read_csv(対象データファイル) # 数値項目のヒストグラムの作成 # binsで幅を調整する df['数値項目'].hist(bins=999) #カテ…

matplotlibとggplotを使ってみる

# ライブラリのインポート import pandas # データの読み込み df = pandas.read_csv('foo.csv') # 必要なデータに絞る df2 = df[['x','y']] # x軸を分類する df2['x_rank'] = df2['x'].apply(lambda x : int(x / 1000)) # matplotlibのインポート import mat…

jupyter(iPython notebook)を使ってみる

Anacondaをインストールしたら結果的に入っていた。 (root):test01 foo$ pyenv version anaconda3-2.3.0 (set by /Users/foo/.pyenv/version) (root):test01 foo$ (root):test01 fii$ ipython notebook [I 19:49:42.714 NotebookApp] Using MathJax from CDN…

線形回帰サンプルコーディング

import numpy import pandas import statsmodels.api as sm # データ読み込み df = pandas.read_csv(csvファイル); df['constant'] = 1 col = [独立変数のカラムの配列指定] # 線形回帰分析 X = df[col] y = df[説明変数のカラム名] X = sm.add_constant(X) …

Pythonの配列の計算

numpyを使う。 普通の配列の場合は直接演算はできない。 >>> a = [1,2,3] >>> b = [4,5,6] >>> a -b Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for -: 'list' and 'list' numpyを利用すると計算がで</module></stdin>…

PythonでWebアクセス

requestsというモジュールを使ってgetする。 htmlの取得 >>> import requests >>> requests.get('https://www.example.com').text '\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset="utf-8" />\n <meta http-equiv="Content-type" content="text/html; charset=utf-8" />\n </meta></meta></head></html>

Pythonに入門してみた

Python3系です 型はイラナイらしい >>> x = 'a' >>> x 'a' パラメータにはカッコが必要 >>> x = 'a' >>> x 'a' >>> print x File "<stdin>", line 1 print x ^ SyntaxError: Missing parentheses in call to 'print' >>> print 1+1 File "<stdin>", line 1 print 1+1 ^ Syn</stdin></stdin>…

ggplotインストール

audacityのintoro data scienceを参考にする。それだけだとimportエラーになるので追加でhuslをインストールする。 https://www.udacity.com/wiki/ud359/get-started https://github.com/yhat/ggplot/issues/407 ggplotでの画像の出力はggsaveを使う http://…

ggplotインストールと動作確認

Udacityのintoro data scienceを参考にする。それだけだとimportエラーになるので追加でhuslをインストールする。 https://www.udacity.com/wiki/ud359/get-started https://github.com/yhat/ggplot/issues/407 ggplotでの画像の出力はggsaveを使う http://p…

Python環境構築

pyenvのインストール wk$ git clone git://github.com/yyuu/pyenv.git ~/.pyenv Cloning into '/Users/foo/.pyenv'... fatal: Unable to look up github.com (port 9418) (nodename nor servname provided, or not known) wk$ wk$ git clone git://github.co…

最大回避ヒープ

参考 FOP - 第1章 二分ヒープ木の楽しみ data (Ord a) => Tree a = Null | Fork Int a (Tree a) (Tree a)みたいにdata定義で型の制約が出来ないみたいですね。 data Tree a = Null | Fork Int a (Tree a) (Tree a) deriving (Show) isEmpty :: (Ord a) => T…

エラトステネスの篩

エラトステネスの篩を実装してみる。 まずは100までで試してみる。 余りがゼロの数を取り除く。 *Main> filter (\x -> mod x 2 /= 0)[2..100] [3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,7…

最大公約数を求める

小さい作業を積み重ねていく その1:公式を使わずにやってみる。 余りの計算はrem関数を使う。 Prelude> 5 % 3 <interactive>:9:3: Not in scope: `%' Prelude> 5 / 3 1.6666666666666667 Prelude> 5 `rem` 3 2 Prelude> 5 `rem` 1 0 Prelude> 5 `rem` 5 0 Prelude> :t re</interactive>…

共通した最も長い接頭語を求める(Longest Common Prefix)

["abcdefg", "abcxyz", "abcaaaaa"] からabcを求める。 たとえば、Javaであれば、このリンク先にあるようなロジックになる。 LeetCode – Longest Common Prefix (Java) 一見すると、ループが多い印象。 これをHaskellで実装するとどうなるかを考えてみた。 …