Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
151 lines (128 loc) · 3.02 KB

File metadata and controls

151 lines (128 loc) · 3.02 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# _*_ coding:utf-8 _*_
"""
This file is about ways to create different types of np.array(),
like identity, diagonal matrix and so on.
"""
import numpy as np
def common_create():
"""
common way of creating array
:return: none
"""
arr1 = np.array([1, 2])
print arr1
# [1 2]
print arr1.shape
# (2L,)
arr2 = np.array([[1, 2], [3.1, 4.]])
print arr2
# [[ 1. 2. ]
# [ 3.1 4. ]]
print arr2.shape
# (2L, 2L)
arr3 = np.array([[1, 2], [3, 4]], dtype=complex)
print arr3
# [[ 1.+0.j 2.+0.j]
# [ 3.+0.j 4.+0.j]]
def about_shape():
print np.array([[1, 2, 3], [3, 4, 5]]).shape
# (2L, 3L)
print np.array([[1, 2, 3]]).shape
# (1L, 3L)
print np.array([1, 2, 3]).shape
# (3L,)
arr1 = np.array([[1, 2, 3], [3, 4, 5]])
arr2 = np.array([1, 2, 3])
print arr1 * arr2
# [[ 1 4 9]
# [ 3 8 15]]
# arr22 = np.array([[1], [2], [3]])
# print arr1 * arr22
print np.dot(arr1, arr2)
# [14 26]
def about_reshape():
arr = np.array([[1, 2, 3], [4, 5, 6], [10, 11, 12], [13, 14, 15]])
print arr.reshape(2, 6)
# [[ 1 2 3 4 5 6]
# [10 11 12 13 14 15]]
b = np.arange(1, 13).reshape((2, 2, 3))
print b
# [[[ 1 2 3]
# [ 4 5 6]]
#
# [[ 7 8 9]
# [10 11 12]]]
print b.reshape((2, 6))
# [[ 1 2 3 4 5 6]
# [ 7 8 9 10 11 12]]
def lst_2_array():
"""
list, tuple to array
:return: none
"""
tp = (1, 2, 3)
lst = [[1, 2], [3, 4]]
print np.array(lst).shape
# (2L, 2L)
print np.array(lst)
# [[1 2]
# [3 4]]
print np.asarray(lst)
# [[1 2]
# [3 4]]
print np.asarray(tp)
# [1 2 3]
def file_2np_arr():
"""txt file to numpy array"""
data_path = '../machine_learn/dataset/perception/dataset.txt'
x = np.loadtxt(data_path, dtype=float)
print x
# [ [ 1.1 2.2 0]
# [ 3.5 3.6 1]]
def empty_arr():
arr1 = np.arange(12).reshape(3, 4)
print arr1
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
arr2 = np.empty(arr1.shape)
print arr2
# [[ 0. 0. 0. 0.]
# [ 0. 0. 0. 0.]
# [ 0. 0. 0. 0.]]
def test_ndim():
# Number of array dimensions.
x = np.array([1, 2, 3])
print x.ndim
# 1
y = np.array([[1, 2, 3], [4, 5, 6]])
print y.ndim
# 2
z = np.arange(12).reshape((2, 2, 3))
print z.ndim
# 3
if __name__ == '__main__':
test_ndim()
# empty_arr()
# file_2np_arr()
# broadcast_demo()
# about_shape()
# common_create()
# lst_2_array()
# about_reshape()
lst = [[1.1, 2.3, 3], [3, 4, 5]]
arr = np.array(lst)
# print arr[1, :]
# [ 3. 4. 5.]
# print arr[1]
# [ 3. 4. 5.]
# print arr[...]
a = np.array([[1.1, 2.3, 3]])
# print a[0].tolist()
# print "".join([str(i) + "-" for i in a[0].tolist()])
# 1.1-2.3-3.0-
# if 4 in [1, 3, 5]:
# print 'in it'
# print arr[0, 1], arr[0, 1].flatten.A[0]
# print np.array(lst)[:-1]
pass
Morty Proxy This is a proxified and sanitized view of the page, visit original site.