LoginSignup
0
0

Globally Coupled Mapの時系列データをCSVファイルに保存する

Last updated at Posted at 2024-04-05

Step0. 使用するパッケージ

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot') #グラフのスタイル
plt.rcParams['figure.figsize'] = [8, 3] # グラフサイズ設定
plt.rcParams['font.size'] = 10 #フォントサイズ
import random
import pandas as pd
import csv
import warnings
warnings.simplefilter('ignore')

Step1. 写像を定義

写像を定義
def f(x, alpha):
	y = 1 - alpha*x*x
	return y

def coupled_f(x_list, alpha, e):
	total = 0
	y_list = []
	num = len(x_list)

	for n in range(num):
		total += f(x_list[n], alpha)
	for n in range(num):
		y_list.append((1 - e) * f(x_list[n], alpha) + (e / num) * total)
	return y_list

Step2. 時系列を生成

時系列を生成
def series(alpha, e, initial, length, trans_period):
	num_coupled = len(initial)  # 結合数
	x_list = [n + trans_period for n in range(length)]
	y_list = []

	for n in range(num_coupled):
		y_list.append([initial[n]])
	length -= 1
	for n1 in range(length + trans_period):
		pre_y_list = []
		for n2 in range(len(initial)):
			pre_y_list.append(y_list[n2][n1])

		after_y_list = coupled_f(pre_y_list, alpha, e)
		for n2 in range(len(initial)):
			y_list[n2].append(after_y_list[n2])

	for n1 in range(num_coupled):
		y_list[n1] = y_list[n1][trans_period:]
	return x_list, np.transpose(y_list)

Step3. CSVファイルに書き込み

CSVファイルに保存
data = []

for sublist1, sublist2 in zip(x_list, y_list):
  data.append([sublist1] + sublist2.tolist())

header = ["index"]
for n in range(len(y_list[0])):
  header.append("Element_{}".format(n))
# CSVファイルに書き込む
with open('data_set.csv', 'w', newline='') as csvfile:  # 'w'は書き込みモード、newline=''は改行コードを指定
  csv_writer = csv.writer(csvfile)
  csv_writer.writerow(header)  # ヘッダーを書き込む
  csv_writer.writerows(data)  # データを書き込む
読み込んでプロット
dataset = "data_set.csv"
df = pd.read_csv(
  dataset,
  parse_dates=True,
  index_col="index"
)
print(df)
df.plot()
plt.show()
実行結果
       Element_0  Element_1  Element_2
index                                 
1000    0.922458  -0.007941   0.922458
1001   -0.270728   0.818381  -0.270728
1002    0.819108   0.055648   0.819108
1003   -0.002265   0.852572  -0.002265
1004    0.922458  -0.007941   0.922458

...
1026    0.819108   0.055648   0.819108
1027   -0.002265   0.852572  -0.002265
1028    0.922458  -0.007941   0.922458
1029   -0.270728   0.818381  -0.270728

image.png

Appendix. パッケージ化したものをGithubに公開しました.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0