def _ConvertLabelForClf(labels):
for i , v in enumerate(labels):
if v <= 1990:
labels[i] = 0
elif v <= 2005:
labels[i] = 1
elif v <= 2010:
labels[i] = 2
else:
labels[i] = 3
return labels
words = []
text = j['text']
for mp in mecab.parse(text, as_nodes=True):
if not mp.is_eos():
feature_splits = mp.feature.split(',')
if feature_splits[0] in ['名詞', '動詞', '形容詞']:
if feature_splits[1] in ['数']:
continue
elif feature_splits[2] in ['人名']:
continue
elif feature_splits[6] in ['*']:
continue
words.append(feature_splits[6])
if feature_splits[0] in [‘名詞’, ‘動詞’, ‘形容詞’]:の行で品詞を絞って、
def MakeDict(all_words):
dictionary = corpora.Dictionary(all_words)
print(dictionary.token2id)
for no_below in [5,20,40]:
for no_above in [0.1,0.3,0.5]:
dictionary.filter_extremes(no_below=no_below, no_above=no_above)
dictionary.save_as_text('filtered_dic_below{0}_above{1}.txt'.format(no_below, no_above))
def MakeDict(all_words):
dictionary = corpora.Dictionary(all_words)
print(dictionary.token2id)
for no_below in [5,20,40]:
for no_above in [0.1,0.3,0.5]:
dictionary.filter_extremes(no_below=no_below, no_above=no_above)
dictionary.save_as_text('filtered_dic_below{0}_above{1}.txt'.format(no_below, no_above))
def MakeFeatures(make_dict = False, dict_param = [5, 0.1]):
all_words = joblib.load('{0}/all_wordss.pkl'.format(WRITE_JOBLIB_DIR))
labels = joblib.load('{0}/publication_years.pkl'.format(WRITE_JOBLIB_DIR))
if (make_dict):
MakeDict(all_words)
dictionary = corpora.Dictionary.load_from_text('filtered_dic_below{0}_above{1}.txt'.format(dict_param[0], dict_param[1]))
dl = len(dictionary)
features = []
for w, l in zip(all_words, labels):
tmp = dictionary.doc2bow(w)
dense = list(matutils.corpus2dense([tmp], num_terms=len(dictionary)).T[0])
if not l == -1:
features.append(dense)
features = np.array(features)
features = np.reshape(features, (-1, dl))
labels = [int(v) for v in labels if v != -1]
joblib.dump(features, '{0}/features.pkl'.format(WRITE_JOBLIB_DIR))
joblib.dump(labels, '{0}/labels.pkl'.format(WRITE_JOBLIB_DIR))
public class Player : Character
{
public Player() : base()
{
}
public override int SetId()
{
return 0;
}
public override int SetMaxHp()
{
return 100;
}
public override int SetMaxMp()
{
return 100;
}
public override Dictionary<int, Action> SetActions()
{
var actions = new Dictionary<int, Action>();
actions.Add(ActionTable.normalAttack.id, ActionTable.normalAttack);
actions.Add(ActionTable.magicAttack.id, ActionTable.magicAttack);
actions.Add(ActionTable.heal.id, ActionTable.heal);
return actions;
}
public override Dictionary<string, double> SetWeekness()
{
var weeknesses = new Dictionary<string, double>();
weeknesses.Add("physical", 1.0);
weeknesses.Add("magic", 1.0);
return weeknesses;
}
}
SetWeeknessはさっき書いた属性に対する耐性です。今回属性は「物理」と「魔法」があります。
SetActions関数でそのキャラクターが選択できる行動を定義します。
CharacterクラスとPlayerクラスのように親クラスとしてActionクラスを作り、
行動の内容はActionTableクラスに定義されています。
public class Action
{
public int id;
public int hpDamage;
public int hpHeal;
public int mpCost;
public string attribute;
public Action(int id, int hpDamage, int hpHeal, int mpCost, string attribute, ref int count)
{
this.id = id;
this.hpDamage = hpDamage;
this.hpHeal = hpHeal;
this.mpCost = mpCost;
this.attribute = attribute;
++count;
}
}
static class ActionTable
{
public static int count;
public static Action normalAttack;
public static Action magicAttack;
public static Action heal;
public static Action strongAttack;
static ActionTable()
{
count = 0;
normalAttack = new Action(count, 10, 0, 0, "physical", ref count);
magicAttack = new Action(count, 15, 0, 10, "magic", ref count);
heal = new Action(count, 0, 40, 10, "magic", ref count);
strongAttack = new Action(count, 30, 0, 0, "physical", ref count);
}
}
import selectivesearch
from matplotlib import pyplot as plt
import matplotlib.patches as mpatches
def search(img):
#img = img[0:300,200:800]
# perform selective search
img_lbl, regions = selectivesearch.selective_search(
# img, scale=5, sigma=0.25, min_size=20)
img, scale=5, sigma=5, min_size=30)
candidates = set()
for r in regions:
#excluding same rectangle (with different segments)
if r['rect'] in candidates:
continue
# excluding regions smaller than 2000 pixels
if r['size'] < 30:
continue
# distorted rects
x, y, w, h = r['rect']
if h == 0 or w == 0:
continue
if w / h > 8 or h / w > 8:
continue
candidates.add(r['rect'])
#draw rectangles on the original image
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
ax.imshow(img)
for x, y, w, h in candidates:
rect = mpatches.Rectangle(
(x, y), w, h, fill=False, edgecolor='red', linewidth=1)
ax.add_patch(rect)
plt.show()
return candidates
def test_selective_search():
im = imread("tower.png")
search(im[:,:,0:3]) #アルファチャンネルを消す
if __name__ == "__main__":
test_selective_search()
learn()
test()
img_lbl, regions = selectivesearch.selective_search( img, scale=5, sigma=0.25, min_size=20) #img, scale=5, sigma=8, min_size=30) candidates = set() for r in regions: #excluding same rectangle (with different segments) if r['rect'] in candidates: continue #excluding regions smaller than 2000 pixels if r['size'] < 20 or r['size'] > 3000: continue # distorted rects x, y, w, h = r['rect'] if h == 0 or w == 0: continue if w / h > 2 or h / w > 2: continue candidates.add(r['rect'])
SIZE = 1000 d = [[0,0,0,0,0,0,0,0,1,0], [0,1,2,2,2,2,2,2,1,0], [0,1,2,0,0,0,0,0,1,0], [0,1,2,0,2,2,2,0,1,0], [0,1,2,0,0,0,2,0,1,0], [0,1,2,2,2,2,2,0,1,0], [0,1,1,1,1,1,1,0,1,0], [0,1,0,0,0,0,0,0,1,0], [0,1,0,0,0,0,0,0,1,0], [0,1,1,1,1,1,1,1,1,0]] x = [] y = [] ud = np.random.rand #alias for _ in range(SIZE): a = ud()*10 b = ud()*10 x.append([a, b]) y.append(d[int(a)][int(b)]) x = np.array(x) x = np.reshape(x,(-1,2)) y = np.array(y)
x = []
y = []
ud = np.random.rand #alias
for _ in range(SIZE):
a = ud()*10
b = ud()*10
x.append([a, b])
y.append(d[int(a)][int(b)])
x = np.array(x)
x = np.reshape(x,(-1,2))
y = np.array(y)
joblib.dump(x,"mx.pkl")
joblib.dump(y,"my.pkl")
def main():
x = joblib.load("mx.pkl")
y = joblib.load("my.pkl")
dx = x
#データの一部を表示
fig = plt.figure()
ax = fig.add_subplot(111)
colors = ["#ff0000", "#00ff00", "#0000ff"]
print("Drawing Images")
if len(dx) > 3000:
px = dx[0:3000]
else:
px = dx
for i ,v in enumerate(px):
ax.scatter(v[0], v[1], c=colors[int(y[i])-1], marker='o', alpha = 0.3)
ax.set_title('Dataset')
print("Finish")
plt.show()
plt.close()
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from sklearn.metrics import f1_score
from sklearn.model_selection import train_test_split
seed = 12345
(X_train, X_test, y_train, y_test) = train_test_split(x, y, test_size=0.50, random_state=seed)
for k in ["rbf", "linear"]:
for c in [30,300]:
for g in [0.01, 1]:
clf = SVC(C=c, kernel=k, gamma=g,class_weight = "balanced")
clf.fit(X_train, y_train)
y_predict = clf.predict(X_test)
ac = accuracy_score(y_test, y_predict)
f1 = f1_score(y_test, y_predict, average="macro")
fig = plt.figure()
ax = fig.add_subplot(111)
if len(X_test) > 3000:
px = X_test[0:3000]
else:
px = X_test
colors = ["#ff8800", "#00ff88", "#8800ff"]
print("Data plotting")
for i ,v in enumerate(px):
ax.scatter(v[0], v[1], c=colors[int(y_predict[i])-1], marker='o', alpha = 0.3)
ax.set_title('')
print("Finish")
print([k,c,g])
plt.show()
plt.close()
np.set_printoptions(suppress=True)
np.set_printoptions(threshold=np.inf, precision=2, floatmode='maxprec')
print(ac)
print(f1)
from keras.layers import Input, Dense, Activation, Dropout
from keras.models import Model, Sequential
from keras.wrappers.scikit_learn import KerasClassifier
from keras import optimizers
layers = 0
def make_model():
model = Sequential()
model.add(Dense(200, input_dim=2, activation='relu'))
for _ in range(layers):
model.add(Dense(200, activation='relu'))
model.add(Dense(3, activation='softmax'))
adam = optimizers.Adam(lr = 0.001, decay = 0)
model.compile(loss='sparse_categorical_crossentropy',
optimizer=adam,
metrics=['accuracy'],
)
#plot_model(model, to_file='model.png', show_shapes=True)
return model
for i in range(10):
layers = i
clf = KerasClassifier(make_model, batch_size=100)
history = clf.fit(X_train, y_train, epochs=100, verbose = 0, validation_data=(X_test, y_test))
np.set_printoptions(suppress=True)
np.set_printoptions(threshold=np.inf, precision=2, floatmode='maxprec')
ac = accuracy_score(y_test, y_predict)
f1 = f1_score(y_test, y_predict, average="macro")
print(ac)
print(f1)
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
y_predict = clf.predict(X_test)
fig = plt.figure()
ax = fig.add_subplot(111)
if len(X_test) > 3000:
px = X_test[0:3000]
else:
px = X_test
colors = ["#ff8800", "#00ff88", "#8800ff"]
for i ,v in enumerate(px):
ax.scatter(v[0], v[1], c=colors[int(y_predict[i])-1], marker='o', alpha = 0.3)
ax.set_title('Test plot')
print("Finish")
plt.show()
plt.close()
pythonが選択肢にないのでとりあえずEclipse IDE for java Developpersを選択、フォルダを選択してインストール。インストールが完了したらLaunchボタンを押して起動。
ここでWorkspaceの選択をする。選択したフォルダ内にpythonのプロジェクトを置いていくことになる。
反映させるには、まずeclipseを開き、penPerspectiveボタンを押して表示されるウィンドウにのPydevを選択してopenする。すると右上にpythonマークが出てくるのでこれを選択し、上のFile->New->PydevProjectを選択すると以下のようなウィンドウが出てくるのでプロジェクト名を適当に決めて真ん中付近のplease configure an interpreter before proceedingをクリックしManual Configを選択