LoginSignup
0
0

More than 1 year has passed since last update.

model.fit エラー

Posted at

model.fit(,)を起動させようとするけれども、エラーが出てしまう。そんなときの対策方法をこの記事で挙げる。

Ex)model.fitエラーの例                
事前情報
 data0={      
'MaxTemp': [33,33,34,34,35,35,34,32,28,35,33,28,32,33,35,30,29,32,34,35],
'NumOfCus':[382,324,338,317,341,360,339,329,218,402,342,205,368,351,304,294,275,336,384,368]
}
Ice_data=DataFrame(data0)
Ice_data

X=Ice_data.loc[:,'MaxTemp'].values 
Y=Ice_data['NumOfCus'].values
model.fit(X,Y)
print('RegCoef:',model.coef_)
print('Inter:',model.intercept_)

これだとエラーが出てしまう。なぜだろうか?
その原因について探っていこう!

原因 数値が横ベクトルである (n=np.array[33,33,34,・・・]
sklearnのfit()では、DataFrame型やnumpyの行列形式(縦ベクトル)のみを受け付けているので、横ベクトルは不可!

model.fitをうまく表示させる対策
①配列をベクトル表示にする。つまり、[[33,33,34,・・・]] ←二重[]を用いて、ベクトルにする。
②reshape(-1,1)を用いる。
今回のようなEx)場合を考えると

X=Ice_data.loc[:,'MaxTemp'].values
Y=Ice_data['NumOfCus'].values
★x=np.array(X).reshape(-1,1)←ベクトルに直している

model.fit(x,Y)
print('RegCoef:',model.coef_)
print('Inter:',model.intercept_)

RegCoef: [17.6756487]
Inter: -249.14371257485038   

結果が出ました。
成功です!

0
0
1

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