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

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

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

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

783閲覧

次のnumpyのコードについて聞きたいことがあります。

doodai

総合スコア21

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/08/10 15:31

in

python

1import numpy as np 2a = np.array([1, 2, 3, 4, 5])

###聞きたいこと
2行目についてなのですが、numpyというライブラリーの中にあるarrayというクラスをインスタンス化したという認識で合っていますか?

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

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

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

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

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

guest

回答2

0

ベストアンサー

お疲れ様です。

雰囲気はそのとおりですが、
正確にはndarrayを返す関数だと思います。

ヘルプの一部を以下に抜粋します。

python

1>>> import numpy 2>>> help(numpy.array) 3Help on built-in function array in module numpy: 4 5array(...) 6 array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0) 7 8 Create an array. 9 10 Parameters 11 ---------- 12 object : array_like 13 An array, any object exposing the array interface, an object whose 14 __array__ method returns an array, or any (nested) sequence. 15 dtype : data-type, optional 16 The desired data-type for the array. If not given, then the type will 17 be determined as the minimum type required to hold the objects in the 18 sequence. This argument can only be used to 'upcast' the array. For 19 downcasting, use the .astype(t) method. 20 copy : bool, optional 21 If true (default), then the object is copied. Otherwise, a copy will 22 only be made if __array__ returns a copy, if obj is a nested sequence, 23 or if a copy is needed to satisfy any of the other requirements 24 (`dtype`, `order`, etc.). 25 order : {'K', 'A', 'C', 'F'}, optional 26 Specify the memory layout of the array. If object is not an array, the 27 newly created array will be in C order (row major) unless 'F' is 28 specified, in which case it will be in Fortran order (column major). 29 If object is an array the following holds. 30 31 ===== ========= =================================================== 32 order no copy copy=True 33 ===== ========= =================================================== 34 'K' unchanged F & C order preserved, otherwise most similar order 35 'A' unchanged F order if input is F and not C, otherwise C order 36 'C' C order C order 37 'F' F order F order 38 ===== ========= =================================================== 39 40 When ``copy=False`` and a copy is made for other reasons, the result is 41 the same as if ``copy=True``, with some exceptions for `A`, see the 42 Notes section. The default order is 'K'. 43 subok : bool, optional 44 If True, then sub-classes will be passed-through, otherwise 45 the returned array will be forced to be a base-class array (default). 46 ndmin : int, optional 47 Specifies the minimum number of dimensions that the resulting 48 array should have. Ones will be pre-pended to the shape as 49 needed to meet this requirement. 50 51 Returns 52 ------- 53 out : ndarray 54 An array object satisfying the specified requirements. 55 56 See Also 57 -------- 58 empty_like : Return an empty array with shape and type of input. 59 ones_like : Return an array of ones with shape and type of input. 60 zeros_like : Return an array of zeros with shape and type of input. 61 full_like : Return a new array with shape of input filled with value. 62 empty : Return a new uninitialized array. 63 ones : Return a new array setting values to one. 64 zeros : Return a new array setting values to zero. 65 full : Return a new array of given shape filled with value. 66 67 68 Notes 69 ----- 70 When order is 'A' and `object` is an array in neither 'C' nor 'F' order, 71 and a copy is forced by a change in dtype, then the order of the result is 72 not necessarily 'C' as expected. This is likely a bug. 73 74 Examples 75 -------- 76 >>> np.array([1, 2, 3]) 77 array([1, 2, 3]) 78

投稿2019/08/11 01:45

0kcal

総合スコア275

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

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

doodai

2019/08/11 02:48

回答していただきありがとうございます。 ヘルプとても助かりました。 とても細かいことだと思うのですが、1つ質問させてください。 もしarrayが関数であれば、np.arrayというのは「ライブラリー.メソッド」という形になっていると思うのですが、これは表記として大丈夫なのでしょうか? ライブラリーの中にたくさん.pyのファイルがあって、その中にクラスがありその中にメソッドがあるというように学んだのですが、「ライブラリー.メソッド」というような表記だと迷子(?)になってしまう気がします。 もしくはnumpyというライブラリーはたくさんの関数が集まったライブラリーなのでしょうか? help(numpy)で調べてみたのですがよく分からなかったもので…
0kcal

2019/08/11 03:36

お疲れ様です。 (ワタシでわかる範囲で回答します。) 「Pythonでは、すべて(ブール値、整数、浮動小数点、文字列、もっと大きなデータ構造、関数、プログラム)がオブジェクトとして実装されている。」と言われています。 すべてがオブジェクトということです。 メソッドは、 オブジェクト.メソッド の形で示せます。 pythonでライブラリという言葉は、それほど、具体的なものは規定していない気もします。(言葉としては使いますが。) モジュール、もしくは、パッケージが具体的なものをさしている気がします。 当然、それらは、オブジェクトです。 以下などに書かれていますが、より正確に理解を深められるためには、docs.python.orgをチラチラ、何度かみられるのがいいような気もします。 https://qiita.com/enoughspacefor/items/9f714c9d4a13b6f1eaa9 補足。dir(numpy)で、arrayと出てますので、直結(直下)と言えると思います。 >>> import numpy >>> dir(numpy) ['AL..., 'array', 'ar... 以上
doodai

2019/08/11 05:17

なるほど、pythonにおいてはライブラリという言葉は曖昧に定義されているのですね。 help()やdir()の話も勉強になりました。 docs.python.orgも覗いてみます。 ご丁寧にありがとうございました。
guest

0

合っていると思います

投稿2019/08/10 20:42

pea

総合スコア426

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問