質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

2回答

2179閲覧

LogisticRegressionについて

Mr_K

総合スコア28

Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2019/02/18 02:59

編集2019/02/20 03:51

ロジスティック回帰のcode実装をしたくてyoutubeに上がっているものをそのまま写しているのですがYouTubeではできていて自分のPCではエラーが出てしまい意味が分かりません。どなたか分かる方ご教授お願いします。codeはipynbファイルをHTMLに書き直してerror箇所まですべて貼りました。
参考にしたyoutubeはhttps://youtu.be/mMMzDFttZ8Aです。

python

1import numpy as np 2import pandas as pd 3from sklearn.datasets import load_iris 4 iris=load_iris() 5#load_iris関数でロードしている 6iris 7'target': array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 11 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 12 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 13 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]), 14 'target_names': array(['setosa', 'versicolor', 'virginica'], dtype='<U10'), 15 'DESCR': '.. _iris_dataset:\n\nIris plants dataset\n--------------------\n\n**Data Set Characteristics:**\n\n :Number of Instances: 150 (50 in each of three classes)\n :Number of Attributes: 4 numeric, predictive attributes and the class\n :Attribute Information:\n - sepal length in cm\n - sepal width in cm\n - petal length in cm\n - petal width in cm\n - class:\n - Iris-Setosa\n - Iris-Versicolour\n - Iris-Virginica\n \n :Summary Statistics:\n\n ============== ==== ==== ======= ===== ====================\n Min Max Mean SD Class Correlation\n ============== ==== ==== ======= ===== ====================\n sepal length: 4.3 7.9 5.84 0.83 0.7826\n sepal width: 2.0 4.4 3.05 0.43 -0.4194\n petal length: 1.0 6.9 3.76 1.76 0.9490 (high!)\n petal width: 0.1 2.5 1.20 0.76 0.9565 (high!)\n ============== ==== ==== ======= ===== ====================\n\n :Missing Attribute Values: None\n :Class Distribution: 33.3% for each of 3 classes.\n :Creator: R.A. Fisher\n :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n :Date: July, 1988\n\nThe famous Iris database, first used by Sir R.A. Fisher. The dataset is taken\nfrom Fisher\'s paper. Note that it\'s the same as in R, but not as in the UCI\nMachine Learning Repository, which has two wrong data points.\n\nThis is perhaps the best known database to be found in the\npattern recognition literature. Fisher\'s paper is a classic in the field and\nis referenced frequently to this day. (See Duda & Hart, for example.) The\ndata set contains 3 classes of 50 instances each, where each class refers to a\ntype of iris plant. One class is linearly separable from the other 2; the\nlatter are NOT linearly separable from each other.\n\n.. topic:: References\n\n - Fisher, R.A. "The use of multiple measurements in taxonomic problems"\n Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to\n Mathematical Statistics" (John Wiley, NY, 1950).\n - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.\n (Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218.\n - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System\n Structure and Classification Rule for Recognition in Partially Exposed\n Environments". IEEE Transactions on Pattern Analysis and Machine\n Intelligence, Vol. PAMI-2, No. 1, 67-71.\n - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule". IEEE Transactions\n on Information Theory, May 1972, 431-433.\n - See also: 1988 MLC Proceedings, 54-64. Cheeseman et al"s AUTOCLASS II\n conceptual clustering system finds 3 classes in the data.\n - Many, many more ...', 16 'feature_names': ['sepal length (cm)', 17 'sepal width (cm)', 18 'petal length (cm)', 19 'petal width (cm)'], 20 'filename': 'C:\Users\mkou0\Anaconda3\lib\site-packages\sklearn\datasets\data\iris.csv'} 21print(iris.target_names) 22#花の種類が格納されている 23['setosa' 'versicolor' 'virginica'] 24for data,target in zip(iris.data[:5],iris.target[:5]): 25 print(data,target) 26#がくの長さ、幅、花弁の長さ、幅の特徴量 27[5.1 3.5 1.4 0.2] 0 28[4.9 3. 1.4 0.2] 0 29[4.7 3.2 1.3 0.2] 0 30[4.6 3.1 1.5 0.2] 0 31[5. 3.6 1.4 0.2] 0 32df =pd.DataFrame(iris.data,columns=iris.feature_names) 33df['target']=iris.target 34df 35sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target 360 5.1 3.5 1.4 0.2 0 371 4.9 3.0 1.4 0.2 0 382 4.7 3.2 1.3 0.2 0 393 4.6 3.1 1.5 0.2 0 404 5.0 3.6 1.4 0.2 0 415 5.4 3.9 1.7 0.4 0 426 4.6 3.4 1.4 0.3 0 437 5.0 3.4 1.5 0.2 0 448 4.4 2.9 1.4 0.2 0 459 4.9 3.1 1.5 0.1 0 4610 5.4 3.7 1.5 0.2 0 4711 4.8 3.4 1.6 0.2 0 4812 4.8 3.0 1.4 0.1 0 4913 4.3 3.0 1.1 0.1 0 5014 5.8 4.0 1.2 0.2 0 5115 5.7 4.4 1.5 0.4 0 5216 5.4 3.9 1.3 0.4 0 5317 5.1 3.5 1.4 0.3 0 5418 5.7 3.8 1.7 0.3 0 5519 5.1 3.8 1.5 0.3 0 5620 5.4 3.4 1.7 0.2 0 5721 5.1 3.7 1.5 0.4 0 5822 4.6 3.6 1.0 0.2 0 5923 5.1 3.3 1.7 0.5 0 6024 4.8 3.4 1.9 0.2 0 6125 5.0 3.0 1.6 0.2 0 6226 5.0 3.4 1.6 0.4 0 6327 5.2 3.5 1.5 0.2 0 6428 5.2 3.4 1.4 0.2 0 6529 4.7 3.2 1.6 0.2 0 66... ... ... ... ... ... 67120 6.9 3.2 5.7 2.3 2 68121 5.6 2.8 4.9 2.0 2 69122 7.7 2.8 6.7 2.0 2 70123 6.3 2.7 4.9 1.8 2 71124 6.7 3.3 5.7 2.1 2 72125 7.2 3.2 6.0 1.8 2 73126 6.2 2.8 4.8 1.8 2 74127 6.1 3.0 4.9 1.8 2 75128 6.4 2.8 5.6 2.1 2 76129 7.2 3.0 5.8 1.6 2 77130 7.4 2.8 6.1 1.9 2 78131 7.9 3.8 6.4 2.0 2 79132 6.4 2.8 5.6 2.2 2 80133 6.3 2.8 5.1 1.5 2 81134 6.1 2.6 5.6 1.4 2 82135 7.7 3.0 6.1 2.3 2 83136 6.3 3.4 5.6 2.4 2 84137 6.4 3.1 5.5 1.8 2 85138 6.0 3.0 4.8 1.8 2 86139 6.9 3.1 5.4 2.1 2 87140 6.7 3.1 5.6 2.4 2 88141 6.9 3.1 5.1 2.3 2 89142 5.8 2.7 5.1 1.9 2 90143 6.8 3.2 5.9 2.3 2 91144 6.7 3.3 5.7 2.5 2 92145 6.7 3.0 5.2 2.3 2 93146 6.3 2.5 5.0 1.9 2 94147 6.5 3.0 5.2 2.0 2 95148 6.2 3.4 5.4 2.3 2 96149 5.9 3.0 5.1 1.8 2 97150 rows × 5 columns 98 99x=iris.data[50:,2].reshape(-1,1) 100y=iris.data[50:]-1 101#x,yの50行から二列取り出す 102 103from sklearn.linear_model import LogisticRegression 104from sklearn.preprocessing import StandardScaler 105from sklearn.model_selection import train_test_split 106#標準化を行う関数StandardScaler 107scaler=StandardScaler() 108x_scaled=scaler.fit_transform(x) 109 110X_train,x_test,Y_train,y_test=train_test_split(x_scaled,y,random_state=0) 111log_reg=LogisticRegression().fit(X_train,Y_train) 112C:\Users\mkou0\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.py:433: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning. 113 FutureWarning) 114--------------------------------------------------------------------------- 115ValueError Traceback (most recent call last) 116<ipython-input-28-106621ea5937> in <module> 117----> 1 log_reg=LogisticRegression().fit(X_train,Y_train) 118 119~\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.py in fit(self, X, y, sample_weight) 120 1283 121 1284 X, y = check_X_y(X, y, accept_sparse='csr', dtype=_dtype, order="C", 122-> 1285 accept_large_sparse=solver != 'liblinear') 123 1286 check_classification_targets(y) 124 1287 self.classes_ = np.unique(y) 125 126~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator) 127 759 dtype=None) 128 760 else: 129--> 761 y = column_or_1d(y, warn=True) 130 762 _assert_all_finite(y) 131 763 if y_numeric and y.dtype.kind == 'O': 132 133~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in column_or_1d(y, warn) 134 795 return np.ravel(y) 135 796 136--> 797 raise ValueError("bad input shape {0}".format(shape)) 137 798 138 799 139 140ValueError: bad input shape (75, 4)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

y_trainのshapeが(75, 4)でおかしいことになっている、という意味のエラーです。

実行したコードの全文、元の動画へのリンクなどがないとそれ以上のことはわかりません。

あと、Jupyterの場合はセルを1つずつ実行することもできますが、整合の取れない結果を招く可能性があるのでRun Allで先頭からまとめて動かした方が良いでしょう。

投稿2019/02/18 13:06

hayataka2049

総合スコア30933

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ファイル名をlogistic.pyから別の名前に変更してみてはどうでしょうか

投稿2019/02/18 04:41

pekeuto

総合スコア19

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hayataka2049

2019/02/18 04:52

logistic.pyはscikit-learnのライブラリ内部のファイルなので変える必要はありません。というかいじっちゃだめ。
pekeuto

2019/02/18 07:33

質問者さん申し訳ありません
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問