前提・実現したいこと
Python初心者です。
現在機械学習に関する勉強を進めており、以下の記事に倣い、線形回帰に関するコードを書いていたのですが、配列同士の結合でエラーが発生してしまいました。
https://qiita.com/wsuzume/items/09a59036c8944fd563ff
発生している問題・エラーメッセージ
--------------------------------------------------------------------------- AxisError Traceback (most recent call last) <ipython-input-4-9a1db8d5e631> in <module> 5 6 # 重みWを求める ----> 7 W = linear_regression(data['x'].values, data['y'].values) <ipython-input-4-9a1db8d5e631> in linear_regression(X, Y) 1 def linear_regression(X, Y): ----> 2 X = np.concatenate((np.ones(X.shape[0]), X), axis=1) 3 W = (np.linalg.inv(X.T.dot(X)).dot(X.T)).dot(Y) 4 return W 5 AxisError: axis 1 is out of bounds for array of dimension 1
Python
1import numpy as np 2import pandas as pd 3from matplotlib import pyplot as plt 4import seaborn as sns 5 6# 線形回帰モデル用データセット 7# a : slope of a line 8# b : intercept of a line 9# size : number of data 10# xlim : domain of variable x 11# scale : standard deviation 12def linear_dataset(a, b, size, xlim=[0, 1], scale=None): 13 x = np.random.uniform(xlim[0], xlim[1], size) 14 y = a * x + b 15 if scale is not None: 16 noize = np.random.normal(0, scale, size) 17 y = y + noize 18 df = pd.DataFrame() 19 df['x'] = x 20 df['y'] = y 21 return df 22 23# データを生成 24data = linear_dataset(0.5, 0.2, 20, scale=0.03) 25 26def linear_regression(X, Y): 27 X = np.concatenate((np.ones(X.shape[0]), X), axis=1) 28 W = (np.linalg.inv(X.T.dot(X)).dot(X.T)).dot(Y) 29 return W 30 31# 重みWを求める 32W = linear_regression(data['x'].values, data['y'].values)
現状の認識
一次元配列Xと、これとshapeが等しい単位行列をconcatenateで結合するときにエラーが発生しているという認識でいます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/04/23 14:52