Pystan上に出てくるErrorを解消したいです。
- MacOS(13.0(22A380))でPystanを回すとエラーが出ており解消したいです。
前提
MacOS上でAnaconda環境でPystanを使っています。簡単なベイズ推計をするテストコードを回すとエラーが出ております。
発生している問題・エラーメッセージ
DistutilsExecError Traceback (most recent call last) File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/unixccompiler.py:267, in UnixCCompiler.link(self, target_desc, objects, output_filename, output_dir, libraries, library_dirs, runtime_library_dirs, export_symbols, debug, extra_preargs, extra_postargs, build_temp, target_lang) 265 linker = compiler_fixup(linker, ld_args) --> 267 self.spawn(linker + ld_args) 268 except DistutilsExecError as msg: File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/ccompiler.py:1007, in CCompiler.spawn(self, cmd, **kwargs) 1006 def spawn(self, cmd, **kwargs): -> 1007 spawn(cmd, dry_run=self.dry_run, **kwargs) File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/spawn.py:70, in spawn(cmd, search_path, verbose, dry_run, env) 69 cmd = cmd[0] ---> 70 raise DistutilsExecError( 71 "command {!r} failed with exit code {}".format(cmd, exitcode) 72 ) DistutilsExecError: command '/Users/username/opt/anaconda3/envs/MyEnvitonment/bin/x86_64-apple-darwin13.4.0-clang++' failed with exit code 1 During handling of the above exception, another exception occurred: LinkError Traceback (most recent call last) Cell In[37], line 1 ----> 1 sm = pystan.StanModel(model_code=sample_code,verbose=True) 2 fit = sm.sampling(data=sample_data, iter=1000, chains=4) 3 print(fit) File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/pystan/model.py:378, in StanModel.__init__(self, file, charset, model_name, model_code, stanc_ret, include_paths, boost_lib, eigen_lib, verbose, obfuscate_model_name, extra_compile_args, allow_undefined, include_dirs, includes) 375 orig_stderr = pystan.misc._redirect_stderr() 377 try: --> 378 build_extension.run() 379 finally: 380 if redirect_stderr: 381 # restore stderr File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py:346, in build_ext.run(self) 343 self.compiler.set_link_objects(self.link_objects) 345 # Now actually compile and link everything. --> 346 self.build_extensions() File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py:468, in build_ext.build_extensions(self) 466 self._build_extensions_parallel() 467 else: --> 468 self._build_extensions_serial() File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py:494, in build_ext._build_extensions_serial(self) 492 for ext in self.extensions: 493 with self._filter_build_errors(ext): --> 494 self.build_extension(ext) File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py:573, in build_ext.build_extension(self, ext) 570 # Detect target language, if not provided 571 language = ext.language or self.compiler.detect_language(sources) --> 573 self.compiler.link_shared_object( 574 objects, 575 ext_path, 576 libraries=self.get_libraries(ext), 577 library_dirs=ext.library_dirs, 578 runtime_library_dirs=ext.runtime_library_dirs, 579 extra_postargs=extra_args, 580 export_symbols=self.get_export_symbols(ext), 581 debug=self.debug, 582 build_temp=self.build_temp, 583 target_lang=language, 584 ) File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/ccompiler.py:751, in CCompiler.link_shared_object(self, objects, output_filename, output_dir, libraries, library_dirs, runtime_library_dirs, export_symbols, debug, extra_preargs, extra_postargs, build_temp, target_lang) 736 def link_shared_object( 737 self, 738 objects, (...) 749 target_lang=None, 750 ): --> 751 self.link( 752 CCompiler.SHARED_OBJECT, 753 objects, 754 output_filename, 755 output_dir, 756 libraries, 757 library_dirs, 758 runtime_library_dirs, 759 export_symbols, 760 debug, 761 extra_preargs, 762 extra_postargs, 763 build_temp, 764 target_lang, 765 ) File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/unixccompiler.py:269, in UnixCCompiler.link(self, target_desc, objects, output_filename, output_dir, libraries, library_dirs, runtime_library_dirs, export_symbols, debug, extra_preargs, extra_postargs, build_temp, target_lang) 267 self.spawn(linker + ld_args) 268 except DistutilsExecError as msg: --> 269 raise LinkError(msg) 270 else: 271 log.debug("skipping %s (up-to-date)", output_filename) LinkError: command '/Users/username/opt/anaconda3/envs/MyEnvitonment/bin/x86_64-apple-darwin13.4.0-clang++' failed with exit code 1
該当のソースコード
Python3.9.16
1import numpy as np 2import pandas as pd 3import matplotlib.pyplot as plt 4%matplotlib inline 5import seaborn as sns 6sns.set_style('whitegrid') 7 8from sklearn.model_selection import train_test_split 9from sklearn.metrics import mean_squared_error, r2_score 10def generate_sample_data(num, seed=0): 11 target_list = [] # 目的変数のリスト 12 feature_vector_list = [] # 説明変数(特徴量)のリスト 13 14 feature_num = 8 # 特徴量の数 15 intercept = 0.2 # 切片 16 weight = [0.2, 0.3, 0.5, -0.4, 0.1, 0.2, 0.5, -0.3] # 各特徴量の重み 17 18 np.random.seed(seed=seed) 19 for i in range(num): 20 feature_vector = [np.random.rand() for n in range(feature_num)] # 特徴量をランダムに生成 21 noise = [np.random.normal(0, 0.1) for n in range(feature_num)] # ノイズをランダムに生成 22 target = sum([intercept+feature_vector[n]*weight[n]+noise[n] for n in range(feature_num)]) # 目的変数を生成 23 24 target_list.append(target) 25 feature_vector_list.append(feature_vector) 26 27 df = pd.DataFrame(np.c_[target_list, feature_vector_list], 28 columns=['target', 'feature0', 'feature1', 'feature2', 29 'feature3', 'feature4', 'feature5', 'feature6', 'feature7']) 30 return df 31 32data = generate_sample_data(num=1000, seed=0) 33 34X = data.drop('target', axis=1) 35y = data['target'] 36 37X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)] 38import pystan 39import arviz 40sample_code = """ 41 data { 42 int<lower=0> N; 43 int<lower=0> D; 44 matrix[N, D] X; 45 vector[N] y; 46 int<lower=0> N_new; 47 matrix[N_new, D] X_new; 48 } 49 parameters { 50 real w0; 51 vector[D] w; 52 real<lower=0> sigma; 53 } 54 model { 55 for (i in 1:N) 56 y[i] ~ normal(w0 + dot_product(X[i], w), sigma); 57 } 58 generated quantities { 59 vector[N_new] y_new; 60 for (i in 1:N_new) 61 y_new[i] = normal_rng(w0 + dot_product(X_new[i], w), sigma); 62 } 63""" 64 65sample_data = { 66 'N': X_train.shape[0], 67 'D': X_train.shape[1], 68 'X': X_train, 69 'y': y_train, 70 'N_new': X_test.shape[0], 71 'X_new': X_test 72} 73sm = pystan.StanModel(model_code=sample_code,verbose=True) 74fit = sm.sampling(data=sample_data, iter=1000, chains=4) 75print(fit)
試したこと
1.ライブラリのアップデート
2.gccのインストール
Mac上のC++ の処理にエラーが出ていそうなのですが、解決方法がわからず大変困っています。もしご存知の方いらっしゃいましたらお力を貸していただければ幸いです。よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー