def learn(teaching_data, save_file_path):
    # 学習実行
    trainFeature, trainLabel = teaching_data
    clf = ml(max_features="auto")  #, max_depth=7)
    clf.fit(trainFeature, trainLabel)

    # 学習成果を保存
    import pickle
    with open(save_file_path, 'wb') as f:
        pickle.dump(clf, f)

    # 学習結果を確認のために表示
    test = clf.predict(trainFeature[2])  # 試しに一つ確認
    print(test)
    result = clf.score(trainFeature, trainLabel)  # 学習データに対する、適合率
    print(result)
    print(clf.feature_importances_)  # 各特徴量に対する寄与度を求める

    return clf
def learn(teaching_data, save_file_path):
	# 学習実行
	trainFeature, trainLabel = teaching_data
	clf = ml(max_features="auto")#, max_depth=7)
	clf.fit(trainFeature, trainLabel)

	# 学習成果を保存
	import pickle
	with open(save_file_path, 'wb') as f:
		pickle.dump(clf, f)

	# 学習結果を確認のために表示
	test = clf.predict(trainFeature[2])             # 試しに一つ確認
	print(test)
	result = clf.score(trainFeature, trainLabel)	# 学習データに対する、適合率
	print(result)
	print(clf.feature_importances_)	                # 各特徴量に対する寄与度を求める

	return clf
def main():
	# 予想したい日の日付けを設定
	target_date = None
	_day = dt.now()        # まずはコマンドライン引数による指定がない場合を想定
	if _day.hour >= 10:    # この時刻を過ぎると、翌日の予想を実施する
		_day += td(days=1)
	target_date = dt(year=_day.year, month=_day.month, day=_day.day)

	argvs = sys.argv       # コマンドライン引数を格納したリストの取得
	argc = len(argvs)      # 引数の個数
	if argc >= 2:          # 引数で計算対象の日を渡す
		arg = argvs[1]
		arg += " 0:0:0"    # 時分秒を加える
		t = timeKM.getTime(arg)
		if t != None:
			target_date = t	
	print(target_date)

	# 予測を実行する時刻を決定する(引数がなければスクリプト実行時の時刻が使われる)
	process_hour = dt.now().hour
	if argc >= 3:          # 引数で予想実行時刻を渡す(その時刻に雲海が出るかを確認するものではない)
		arg = argvs[2]
		if arg.isdigit():
			process_hour = int(arg)


	# アメダスの観測所オブジェクトを作成
	amedas_nodes = amd.get_amedas_nodes()
	#print(amedas_nodes)
	# 観測データを読み出す
	#node_A = amedas_nodes["阿蘇山"]
	node_A = amedas_nodes["熊本"]        # 2015-09の噴火で阿蘇山頂のデータが得られないので、熊本に差し替え
	lines_A = get_amedas_data(node_A, target_date)
	node_B = amedas_nodes["阿蘇乙姫"]
	lines_B = get_amedas_data(node_B, target_date)
	# 観測データを処理して、特徴量の生成に適したオブジェクトに変更
	weather_data_A = feature.get_weather_dict(lines_A)
	weather_data_B = feature.get_weather_dict(lines_B)
	raw_data = [weather_data_A, weather_data_B]
	#print(weather_data_Aso)
	#print(weather_data_Otohime)


	# 機械学習オブジェクトを生成
	clf = ml()	
	if 23 > process_hour >= 16:
		with open('entry_16.pickle', 'rb') as f:
			clf = pickle.load(f)               # オブジェクト復元
	else:
		with open('entry_23.pickle', 'rb') as f:
			clf = pickle.load(f)               # オブジェクト復元


	# 特徴ベクトルを生成
	_feature = None
	if 23 > process_hour >= 16:
		_feature = feature.create_feature16(target_date, raw_data)
	else:
		_feature = feature.create_feature23(target_date, raw_data)
	print(_feature)

	# 予測を実施
	print("--predict--")
	print("target date: " + str(target_date))
	print("process hur: " + str(process_hour))
	done = False
	results = []
	if _feature != None:
		if not None in _feature:  # Noneがあると計算出来ない
			test = clf.predict(_feature)
			results.append((target_date, test[0], _feature))
			print(test)
			done = True
	if done == False:
		results.append((target_date, "NA", _feature))
		print("--can't predict. There is None data in feature-vector.--")


	# 予測結果を保存
	with open("result.csv", "w") as fw:
		for result in results:
			_date, predict_result, _feature = result
			str_feature = [str(x) for x in _feature]
			fw.write(str(dt.now()))
			fw.write(",")
			fw.write(str(_date))
			fw.write(",")
			fw.write(str(predict_result))
			fw.write(",")
			fw.write("$")
			fw.write(",")
			fw.write(",".join(str_feature))
			fw.write("\n")

	return results
# memo: 読み込むデータは、1行目に列名があり、最終列に正解ラベルが入っていること。
#       正解ラベルは整数もしくは文字列であること。
# created: 2017-07-05
#----------------------------------------
import pandas
import pickle
from sklearn.ensemble import RandomForestClassifier as ml
import numpy as np

# データの読み込み
data = pandas.read_csv("iris_learning.csv")
#print(data)
x = (data.iloc[:, :-1]).values  # transform to ndarray
y = (data.iloc[:, -1:]).values
y = np.ravel(y)  # transform 2次元 to 1次元 ぽいこと

# 学習
clf = ml()  # 学習器
clf.fit(x, y)
result = clf.score(x, y)  # 学習データに対する、適合率

# 学習済みの学習器を保存
with open('entry.pickle', 'wb') as f:
    pickle.dump(clf, f)

# 結果の確認
test = clf.predict([x[0]])  # 1個だけテスト
print(test)
print(result)  # 学習データに対する適合率
print(clf.feature_importances_)  # 各特徴量に対する寄与度を求める
def new():
    """ 初期化した機械学習オブジェクトを返す
	"""
    clf = ml(max_features="auto", max_depth=40, n_estimators=150,
             n_jobs=2)  # RF
    return clf
def new():
    """ 初期化した機械学習オブジェクトを返す
	"""
    clf = ml(max_features="auto", max_depth=40, n_estimators=150, n_jobs=2)  # RF
    return clf