Index ソフト・ハード Python | Sample |
scikit_learn 日本語を使用 ファイルアクセス |
sampleなどはCentOS7で実施 $ python3 $ python3 xxxx.pyscikit_learn ・学習例1
$ python3
Python 3.6.8 (default, Aug 7 2019, 17:28:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sklearn import svm # SVM(サポートベクターマシン)インポート
>>> from sklearn.metrics import accuracy_score # 精度を出力
>>> train_data = [[0, 0], [1, 0], [0, 1], [1, 1]] # 学習データ(教師データ)
>>> train_label = [0, 1, 1, 0] # 正解ラベル(教師データ)
>>> test_data = [[0, 0], [1, 0], [0, 1], [1, 1]] # テストデータ(学習データと同じ多重リスト)
>>> clf = svm.SVC(C=10, gamma=0.1) # アルゴリズム指定
>>> clf.fit(train_data,train_label) # 学習実行
SVC(C=10, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma=0.1, kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
>>> test_label = clf.predict(test_data) #
>>> print("テストデータ:{0},予測ラベル:{1}".format(test_data,test_label)) #
テストデータ:[[0, 0], [1, 0], [0, 1], [1, 1]],予測ラベル:[0 1 1 0]
>>> print("正解率= {}".format(accuracy_score([0, 1, 1, 0], test_label))) #
正解率= 1.0
>>>
テストデータに対して学習済みモデルが正しく[0, 1, 1, 0]と判定できるかを確認
日本語を使用 #!/usr/bin/env python3 # -*- coding: utf-8 -*- # このソースがUTF-8で書かれている。 import re # 正規表現操作(参考) import codecs # codec 用の基底クラスを定義 import sysファイルアクセス ・$ python3 xxxx.py
#!/usr/bin/env python3
import os
import pathlib
import glob
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#from sklearn.linear_model import LinearRegression
from sklearn import linear_model
pd.set_option("display.max.columns", 100)
pd.set_option("display.max.rows", 100)
current_dir = os.getcwd()
print("現在のディレクトリ=", current_dir)
csv_gakusyu01 = pd.read_csv(filepath_or_buffer="./gakusyuData/kg01.csv",sep=",", header=0)
print("csv_gakusyu01.shape->", csv_gakusyu01.shape)
pd_gakusyu01 = csv_gakusyu01.drop(["Kekka"], axis=1)
X_train = pd_gakusyu01.values
y_train = csv_gakusyu01["Kekka"].values
# learn
clf = linear_model.Lasso()
clf.fit(X_train, y_train)
print("column,clf.coef_=", pd.DataFrame({"column": pd_gakusyu01.columns, "coef": clf.coef_}))
print("clf.intercept_=", clf.intercept_)
# test
files = glob.glob('./testData/*')
for file in files:
#print(file)
csv_test01 = pd.read_csv(filepath_or_buffer=file, sep=",", header=0)
path = pathlib.Path(file)
fileName = path.stem # 結果書き込み用
print(fileName)
pd_test01 = csv_test01.drop(["Kekka"], axis=1)
X_test = pd_test01.values
#y_test = csv_test01["Kekka"].values
#print(y_test)
# predict
predict = clf.predict(X_test)
print(predict)
#print(y_test)
df = pd.DataFrame(predict) # DataFrameに
print(df)
df.to_csv("./testKekkaData/" + fileName)
else:
print('forEnd')
print("End")
|
All Rights Reserved. Copyright (C) ITCL |