ロト7(loto7)予測

機械学習を勉強し始めたばかりで、勉強がてらロト7(loto7)の数字予測プログラム(Python)を作成してみました。もちろん、当たるよという保証もないです。

最初習ったのは、ランダムフォレストモデル(RandomForestRegressor)と決定木モデル(DecisionTreeRegressor)なので、この二つモデルをそのまま適用して、過去の当たり数字を学習したうえ、指定された日付の予測数字を出すというプログラムです。

ここに前提としての考えは、宝くじの出現数字はランダムと言いながら、もしかして日付に関連するじゃないか?という考えが入っています。なので、日付を入力すると、日付をもとに数字を予測した数字を7個出力してくれます。

これのソースをloto7.pyの名前で保存したうえ、「python loto7.py」で実行すれば動きます。(中で使用するデータセットloto7_train.csvは以下の場所からダウンロードしてください。https://loto7.thekyo.jp/download/index)

import pandas as pd
import numpy as np
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor

# Path of the file to read
loto7_file_path = './input/loto7_train.csv'
loto_data = pd.read_csv(loto7_file_path)

# Create 7th targets object and call it y1 to y7
train_y1 = loto_data.Num1
train_y2 = loto_data.Num2
train_y3 = loto_data.Num3
train_y4 = loto_data.Num4
train_y5 = loto_data.Num5
train_y6 = loto_data.Num6
train_y7 = loto_data.Num7

# Create X
loto_data['Date'] = pd.to_datetime(loto_data['Date'])
loto_data['LotoYear']=loto_data['Date'].dt.year
loto_data['LotoMonth']=loto_data['Date'].dt.month
loto_data['LotoDay']=loto_data['Date'].dt.day

features = ['LotoYear', 'LotoMonth', 'LotoDay']
X = loto_data[features]

# Define the model. Set random_state to 1
rf_model1 = RandomForestRegressor(random_state=1)
rf_model2 = RandomForestRegressor(random_state=1)
rf_model3 = RandomForestRegressor(random_state=1)
rf_model4 = RandomForestRegressor(random_state=1)
rf_model5 = RandomForestRegressor(random_state=1)
rf_model6 = RandomForestRegressor(random_state=1)
rf_model7 = RandomForestRegressor(random_state=1)

# fit the model for 7 targets number
rf_model1.fit(X, train_y1)
rf_model2.fit(X, train_y2)
rf_model3.fit(X, train_y3)
rf_model4.fit(X, train_y4)
rf_model5.fit(X, train_y5)
rf_model6.fit(X, train_y6)
rf_model7.fit(X, train_y7)


# path to file you will use for predictions
test_data_path = './loto7_test.csv'

# read test data file using pandas
test_data = pd.read_csv(test_data_path)

# create test_X which comes from test_data but includes only the columns you used for prediction.
# The list of columns is stored in a variable called features
test_data['Date'] = pd.to_datetime(test_data['Date'])
test_data['LotoYear']=test_data['Date'].dt.year
test_data['LotoMonth']=test_data['Date'].dt.month
test_data['LotoDay']=test_data['Date'].dt.day
features = ['LotoYear', 'LotoMonth', 'LotoDay']
test_X = test_data[features]

# make predictions which we will submit. 
num1_preds = rf_model1.predict(test_X)
num2_preds = rf_model2.predict(test_X)
num3_preds = rf_model3.predict(test_X)
num4_preds = rf_model4.predict(test_X)
num5_preds = rf_model5.predict(test_X)
num6_preds = rf_model6.predict(test_X)
num7_preds = rf_model7.predict(test_X)

# print(num1_preds,num2_preds,num3_preds,num4_preds,num5_preds,num6_preds,num7_preds)
print('---------Random Forest Model----------')

print('---------小数点以下切り上げ----------')
# 小数点以下切り上げ
print(np.ceil(num1_preds))
print(np.ceil(num2_preds))
print(np.ceil(num3_preds))
print(np.ceil(num4_preds))
print(np.ceil(num5_preds))
print(np.ceil(num6_preds))
print(np.ceil(num7_preds))

print('---------Decision Tree Model----------')

# Define the model. Set random_state to 1
dt_model1 = DecisionTreeRegressor(random_state=1)
dt_model2 = DecisionTreeRegressor(random_state=1)
dt_model3 = DecisionTreeRegressor(random_state=1)
dt_model4 = DecisionTreeRegressor(random_state=1)
dt_model5 = DecisionTreeRegressor(random_state=1)
dt_model6 = DecisionTreeRegressor(random_state=1)
dt_model7 = DecisionTreeRegressor(random_state=1)

# fit the model for 7 targets number
dt_model1.fit(X, train_y1)
dt_model2.fit(X, train_y2)
dt_model3.fit(X, train_y3)
dt_model4.fit(X, train_y4)
dt_model5.fit(X, train_y5)
dt_model6.fit(X, train_y6)
dt_model7.fit(X, train_y7)


# path to file you will use for predictions
test_data_path = './loto7_test.csv'

# read test data file using pandas
test_data = pd.read_csv(test_data_path)

# create test_X which comes from test_data but includes only the columns you used for prediction.
# The list of columns is stored in a variable called features
test_data['Date'] = pd.to_datetime(test_data['Date'])
test_data['LotoYear']=test_data['Date'].dt.year
test_data['LotoMonth']=test_data['Date'].dt.month
test_data['LotoDay']=test_data['Date'].dt.day
features = ['LotoYear', 'LotoMonth', 'LotoDay']
test_X = test_data[features]

# make predictions which we will submit. 
num1_preds = dt_model1.predict(test_X)
num2_preds = dt_model2.predict(test_X)
num3_preds = dt_model3.predict(test_X)
num4_preds = dt_model4.predict(test_X)
num5_preds = dt_model5.predict(test_X)
num6_preds = dt_model6.predict(test_X)
num7_preds = dt_model7.predict(test_X)

print('---------小数点以下切り上げ----------')
# 小数点以下切り上げ
print(np.ceil(num1_preds))
print(np.ceil(num2_preds))
print(np.ceil(num3_preds))
print(np.ceil(num4_preds))
print(np.ceil(num5_preds))
print(np.ceil(num6_preds))
print(np.ceil(num7_preds))

いろいろと意見やアドバイスがあるなら、コメントしてください。

中古本の処分はオークションをかけるか、捨てましょう

 引越しでたくさんの中古本が出てきました。皆さんも似たような方はいらっしゃって、悩みながらネット上で調べたりすると思います。ご参考ください。

 結論からいうと、

 中古本は時間をかけてオークションをかけるか、時間がかかることが嫌ならば、ゴミ収集場にそのまま捨てましょう。

 と言いたいです。

 すこしでもお金になるじゃないかと希望を抱えて、ネット上で調べてみました。その結果、ネット上の紹介だと、1箱程度で査定結果は約5000円ちょっと程度でブックオフからの買取サービスを利用していた方がいらっしゃったそうです。

 迷いながらも、2箱で約100冊の本を段ボールでブックオフに出して、査定してもらいました。自分の2箱本なら1万円程度になるじゃないかという夢も見ていました。理由はその中にフルセットの漫画「おしり探偵」新品購入して状態良好も入っているし、C言語やJava言語、サーバー構築、Oracleデータベース関連、Linuxコマンドなどのぶっ厚いIT技術書(4年ほど前に新品で購入して使っていたものもあります)が入っていました。これだと、1万円ぐらい余裕で売れるじゃないかと考えていました。

 その結果、郵送でブックオフに出して、翌日に査定結果は下記のようにメールで連絡してくれました。えっ?1冊161円なのかな?500円売れる本があってもおかしくないがと疑いながら、値段のつく39冊で合計で161円という笑えない事実でした。詐欺と言いませんが、かなり想定から離れた結果です。そして、査定結果を7段階で評価してくださいと依頼もありましたが、もちろん最低の7で評価しました。かなりショックでした。

——————————————————————-

このたびは、お客様の大切な品物をお送りいただきまして、
誠にありがとうございます。

下記、査定の結果についてご案内いたします。

───────────
【明細】
■書籍→39点/161円
■コミック→0点/0円
■CD→0点/0円
■DVD→0点/0円
■GAME→0点/0円
■その他→0点/0円
■買取専用ダンボール→0点/0円
■合計→39点/161円
■お値段のつかなかった商品→63点
■お送りいただいた箱数→2箱

───────────

以下、7段階の評価より
もっとも当てはまるもののリンクを1回クリックしてください。

◎1.とても満足だった

◎2.満足だった

◎3.どちらかと言えば満足だった

◎4.普通

◎5.どちらかといえば満足ではなかった

◎6.満足ではなかった

◎7.まったく満足ではなかった

——————————————————————-

Googleフォトのバックアップ

 スマホのGoogleフォト(グーグルフォト)アプリを使用して、クラウドに写真やビデオをバックアップしていて、ローカルパソコンのディスクにもダウンロードして保存したいと思っていませんか?

 普段スマホを使って、何も気にしないで、写真やビデオをパシャパシャと大量に撮影して、あっという間に何百GBにもなり、クラウド上の容量を大量消費してしまいます。Google フォトは便利とはいえ、有料になってしまいましたし、できるだけ購入した容量を有効に使い、写真やビデオをどんどんローカルパソコンのディスクに保存したいですよね。なんといってもローカルディスクは安くて手元にあって安心です。

 このGoogleフォトのローカル保存には、以下いくつかの難点があり、それぞれのソリューションを紹介します。もちろん、人の使い方はそれぞれで、あくまで一例としてご参考ください。

  • Google フォトからはどうダウンロードするか?
  • ダウンロードした(zip)ファイルはどう解凍するか?
  • 解凍後のゴミファイル(.json)はどう削除するか?

1.Google フォトからはどうダウンロードするか?

 Google フォトは以下のURLに保存されているが、さすがにログインしてマウスで一枚ずつクリックして選択したうえ、ダウンロードすることは、何千枚、何万枚の写真を持っている人にはなかなか無理ですよね。

https://photos.google.com/

 この場合、Googleフォトにダウンロード用リストを定期的に生成してもらうようにスケジュールを設定できます。そうすると、Googleフォトは月一回に全フォトに対して、エクスポート用のzipファイルを準備して、メールで知らせてくれます。そのやり方は以下どおりです。

 Googleフォトにログイン後、「設定」と「データのエクスポート」を順にクリックします。

「新しいエクスポートの作成」をクリックします。

 次のステップで、エクスポート先、頻度、ファイル形式とファイルサイズを設定します。最後は、「エクスポートを作成」をクリックして設定を完了します。あとはGoogleは裏で処理して、準備できたら知らせてくれます。

 今回はローカルディスクに保存するために、「ダウンロードリンクをメールで送信」と選択したが、GoogleドライブやOneDriveに保存するといった選択肢もあります。ご自分の状況に合わせて選択してください。

 最後、準備できたと知らせてくれた際のリンクを順次にクリックして、ローカルディスクにダウンロードします。こんなイメージです。

2.ダウンロードした(zip)ファイルはどう解凍するか?

 ダウンロードしたzipファイルは何十個や数百個まで上ります。PCとディスクの性能にもよりますが、1ファイルの解凍には、十数分かかります。ファイルを右クリックし、Windowsの「すべて展開…」で手動でやっていくと、クリックと待ち時間のオーバヘッドが大きく、時間がかかりますし、作業時間ももったいないです。

 以下通り、シンプルなPowerShellプログラムを作成していますので、よかったらご使用ください。

 以下のソースコードをファイル名「expand.ps1」として「D:\Google Photo」に保存してください。(ダウンロードしたzipファイルもこのフォルダに保存されている想定です。)

$filePath = Get-ChildItem -Path "D:\Google Photo\takeout*.zip"
ForEach($i in $filePath){
	echo "---Start---	$i"

	# ZIPファイルに対して解凍を行う。同じファイル名がある場合、上書きする
	Expand-Archive -Force -Path $i -DestinationPath .
	echo "---end---"
}

 ファイルを作成したあと、PowerShellコマンドを以下通り実行します。その結果、解凍した写真やビデオは「D:\Google Photo\Takeout」に生成されます。

> cd "D:\Google Photo"
> powershell -File .\expand.ps1 

3.解凍後のゴミファイル(.json)はどう削除するか?

 解凍で生成したTakeoutフォルダの中身を確認すればわかるが、「.json」ファイルがいっぱい生成されます。これはローカルPC上で必要のないファイルらしいです。煩わしいため、削除したほうが良いと思います。

とはいえ、大量にあります。以下の操作で簡単に削除できます。

(1)Windowsのエクスプローラーで「D:\Google Photo\Takeout」に移動します。

(2)右上の検索キーワード欄で「.json」を入力し、検索します。

(3)検索でヒットしたゴミファイルを1つクリックしたうえ、Ctrl+Aで全選択して、Deleteキーで削除します。

 以上はGoogleフォトの写真/ビデオダウンロード、解凍とゴミファイル削除によって、ローカルPCのディスクに保存する方法をまとめてみました。もしどこかミスなどがありましたら、ご連絡いただけると助かります。

皆さんに役立つように!

Linuxコマンドで写真サイズ変更(2023/7)

写真サイズ変更などを実現するのは、ImageMagicというツールの一つプログラムです。

CentOS(RedHat系)やUbuntu(Debian系)などのLinuxとも使えます。現在2023/7時点で、バージョン「6.9.11」が使えるようですが、最後の更新は2021年だったようです。

1.インストール

このソフトをCentOSとUbuntuでそれぞれのインストール仕方を下記通りメモします。

CentOS

♯ sudo yum -y install ImageMagick

Ubuntu

$ sudo apt-get install imagemagick

インストールするソフトはImageMagickですが、写真編集に使うコマンドは「Convert」となります。

$ which convert
/usr/bin/convert

$ convert --version
Version: ImageMagick 6.9.11-60 Q16 x86_64 2021-01-25 https://imagemagick.org
Copyright: (C) 1999-2021 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP(4.5)
Delegates (built-in): bzlib djvu fftw fontconfig freetype heic jbig jng jp2 jpeg lcms lqr ltdl lzma openexr pangocairo png tiff webp wmf x xml zlib

2.写真サイズ変更

convertコマンドの機能は結構豊富です。サイズ変更は、以下のように実行すればよいです。詳細はman convertで調べてください。

$ convert -resize 160x picture_a.jpg  picture_b.jpg

ちなみに、man convertで調べた結果の一部は以下となります。

$ man convert
NAME
       convert  -  convert  between  image  formats  as well as resize an image, blur, crop, de-
       speckle, dither, draw on, flip, join, re-sample, and much more.

SYNOPSIS
       convert-im6.q16 [input-option] input-file [output-option] output-file

OVERVIEW
       The convert-im6.q16 program is a member of the ImageMagick-ims6.q16(1)  suite  of  tools.
       Use  it  to  convert  between  image  formats as well as resize an image, blur, crop, de-
       speckle, dither, draw on, flip, join, re-sample, and much more.

       For   more   information   about   the   convert   command,   point   your   browser   to
       file:///usr/share/doc/imagemagick-6-common/html/www/convert.html  (on  debian  system you
       may install the imagemagick-6 package) or https://www.imagemagick.org/script/convert.php.

3.実行例

左側「orignal.png」は編集対象写真で、右側「out.png」は編集後写真です。

実行したコマンドは以下となります。

$ convert -resize 160x orignal.png  out.png

4.参照URL

以下のURLには、個々オプションの使用サンプルを説明しています。(英語)

「Examples of ImageMagick Usage」

https://imagemagick.org/Usage/

Viの使い方

目的

Windowsで作成したテキストファイルは、改行コードがCR+LFであるため、Linuxで開くと、行の末尾に^Mと表示される。削除したい。

解決策

これをviエディターで簡単に改行文字^Mを検索して、一気に削除できる。

通常の文字列を検索・削除するのは以下のコマンドで実行する。

%s/[対象文字列]//g

「^M」の入力は少し特別で、「^+M」ではなく、「Ctrl」を押したまま、順次に「v」「m」を入力していけばいい。

:%s/^M//g

実行例

実行前

実行後