Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Jupyter notebook 2017-04-07-142242.ipynb

Project: pyton
Views: 108
Kernel: Python 3 (Ubuntu Linux)
import numpy as np
a=np.array([1,2,3,4,5]) print(a)
[1 2 3 4 5]
print(len(a)) print(a.size)
5 5
a.dtype
dtype('int64')
a.shape
(5,)
a1=np.array([1.,2.,3.,4.,5.]) print(a1,a1.dtype,a1.shape)
[ 1. 2. 3. 4. 5.] float64 (5,)
a2=np.array(([1,2,3,4,5]), dtype=np.float16) print(a2,a2.dtype,a2.shape)
[ 1. 2. 3. 4. 5.] float16 (5,)
a2.astype(np.int32)
array([1, 2, 3, 4, 5], dtype=int32)
A=np.array([[1.,2.],[3.,4.],[5.,6.]]) print(A)
[[ 1. 2.] [ 3. 4.] [ 5. 6.]]
print('len: ', len(A)) print('size: ', A.size) print('dtype: ', A.dtype) print('shape: ', A.shape)
len: 3 size: 6 dtype: float64 shape: (3, 2)
AT=A.T print(AT) print('len: ', len(AT)) print('size: ', AT.size) print('dtype: ', AT.dtype) print('shape: ', AT.shape)
[[ 1. 3. 5.] [ 2. 4. 6.]] len: 2 size: 6 dtype: float64 shape: (2, 3)
at=a.T print(at) print('len: ', len(at)) print('size: ', at.size) print('dtype: ', at.dtype) print('shape: ', at.shape)
[1 2 3 4 5] len: 5 size: 5 dtype: int64 shape: (5,)
aa=A.reshape((6,)) print(A) print(aa)
[[ 1. 2.] [ 3. 4.] [ 5. 6.]] [ 1. 2. 3. 4. 5. 6.]
ab=A.reshape((6,1)) print(A) print(ab)
[[ 1. 2.] [ 3. 4.] [ 5. 6.]] [[ 1.] [ 2.] [ 3.] [ 4.] [ 5.] [ 6.]]
ab=A.reshape((1,6)) print(A) print(ab)
[[ 1. 2.] [ 3. 4.] [ 5. 6.]] [[ 1. 2. 3. 4. 5. 6.]]
ac=A.reshape((1,-1)) print(A) print(ac)
[[ 1. 2.] [ 3. 4.] [ 5. 6.]] [[ 1. 2. 3. 4. 5. 6.]]
A[1,1]
4.0
A[1,:]
array([ 3., 4.])
A[:,1]
array([ 2., 4., 6.])
A.max()
6.0
A.max(axis=0)
array([ 5., 6.])
A.max(axis=1)
array([ 2., 4., 6.])
B=np.random.random(7) print(B) B.sort() print(B)
[ 0.5647215 0.00094002 0.24878213 0.68678843 0.52699816 0.03735719 0.2454302 ] [ 0.00094002 0.03735719 0.2454302 0.24878213 0.52699816 0.5647215 0.68678843]
B2=np.random.random(size=(2,3)) print(B2) B2.sort() print(B2)
[[ 0.64321401 0.65931002 0.21608559] [ 0.92885038 0.564786 0.39182436]] [[ 0.21608559 0.64321401 0.65931002] [ 0.39182436 0.564786 0.92885038]]
B3=np.random.random(size=(2,3)) print(B3) B3.sort(axis=0) print(B3)
[[ 0.16288666 0.11172614 0.35282611] [ 0.38132885 0.02666066 0.15043217]] [[ 0.16288666 0.02666066 0.15043217] [ 0.38132885 0.11172614 0.35282611]]
B4=np.random.random(size=(2,3)) print(B4) B4.sort(axis=1) print(B4)
[[ 0.12819814 0.48234593 0.86324019] [ 0.45798466 0.12867528 0.55265798]] [[ 0.12819814 0.48234593 0.86324019] [ 0.12867528 0.45798466 0.55265798]]
B5=np.random.random(size=(2,3)) print(B5) print(B5.argsort())
[[ 0.30914498 0.64406892 0.08580514] [ 0.25394587 0.39194642 0.92343702]] [[2 0 1] [0 1 2]]
A1=np.array([1.,2.,3.]) A2=np.array([3.,2.,1.]) np.inner(A1, A2)
10.0
M1=np.array([[2.,3.],[3.,1.],[1.,2]]) print(M1)
[[ 2. 3.] [ 3. 1.] [ 1. 2.]]
X1=np.array([1.,1.,1.]) X2=np.array([1.,1.]) np.dot(M1,X2)
array([ 5., 4., 3.])
RA=np.array([(1,2.),(2,3.)], dtype=[('id', np.int32),('val', np.float64)]) print(RA) print(RA.dtype)
[(1, 2.) (2, 3.)] [('id', '<i4'), ('val', '<f8')]
RA[0]
(1, 2.)
print(RA['id'],RA['val'])
[1 2] [ 2. 3.]
RA[0]['id']
1
print(RA.tostring())
b'\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08@'
print(M1.tostring())
b'\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@'
np.fromstring(RA.tostring(), dtype=[('id',np.int32),('val',np.float64)])
array([(1, 2.), (2, 3.)], dtype=[('id', '<i4'), ('val', '<f8')])
M1.tolist()
[[2.0, 3.0], [3.0, 1.0], [1.0, 2.0]]
RA.tolist()
[(1, 2.0), (2, 3.0)]
X1=np.random.random(7) X2=np.random.random(7) print(X1) print(X2) print(X1+X2)
[ 0.63959344 0.72079659 0.86719556 0.24628904 0.15319488 0.05360769 0.97353974] [ 0.48495132 0.68221772 0.09359279 0.29566355 0.18079382 0.52736749 0.78261675] [ 1.12454475 1.4030143 0.96078836 0.54195259 0.3339887 0.58097517 1.75615649]
print(X1**2+X2**2)
[ 0.64425754 0.98496873 0.76078776 0.14807523 0.05615508 0.28099025 1.5602686 ]
print(np.sqrt(X1))
[ 0.79974586 0.8489974 0.93123336 0.49627516 0.39140117 0.23153334 0.98668118]
print(np.exp(X1))
[ 1.89571 2.0560704 2.38022629 1.27926927 1.16555209 1.0550706 2.64729865]
text="""\ 1 2.0 2 3.0 """
import io
f=io.StringIO(text) AA=np.loadtxt(f,delimiter=' ',dtype=[('id',np.int32), ('val', np.float64)]) f.close() print(AA)
[(1, 2.) (2, 3.)]
print(type(AA), AA.dtype,AA.shape)
<class 'numpy.ndarray'> [('id', '<i4'), ('val', '<f8')] (2,)
#f2=io.StringIO("") #np.savetxt(f2,AA,delimiter=',', fmt=[('id','%i'), ('val','%.if')]) #2.close()
#np.savetxt?
import pandas as pd
# sepal -чашелистник # petal - лепесток attr_names = """sepal lenght sepal width petal length petal width class""" attr_names = attr_names.split("\n") print(attr_names)
['sepal lenght', 'sepal width', 'petal length', 'petal width', 'class']
iris_df=pd.read_csv("data/iris.data", sep=',', names=attr_names)
print(type(iris_df)) iris_df
<class 'pandas.core.frame.DataFrame'>
sepal lenght sepal width petal length petal width class
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5.0 3.6 1.4 0.2 Iris-setosa
5 5.4 3.9 1.7 0.4 Iris-setosa
6 4.6 3.4 1.4 0.3 Iris-setosa
7 5.0 3.4 1.5 0.2 Iris-setosa
8 4.4 2.9 1.4 0.2 Iris-setosa
9 4.9 3.1 1.5 0.1 Iris-setosa
10 5.4 3.7 1.5 0.2 Iris-setosa
11 4.8 3.4 1.6 0.2 Iris-setosa
12 4.8 3.0 1.4 0.1 Iris-setosa
13 4.3 3.0 1.1 0.1 Iris-setosa
14 5.8 4.0 1.2 0.2 Iris-setosa
15 5.7 4.4 1.5 0.4 Iris-setosa
16 5.4 3.9 1.3 0.4 Iris-setosa
17 5.1 3.5 1.4 0.3 Iris-setosa
18 5.7 3.8 1.7 0.3 Iris-setosa
19 5.1 3.8 1.5 0.3 Iris-setosa
20 5.4 3.4 1.7 0.2 Iris-setosa
21 5.1 3.7 1.5 0.4 Iris-setosa
22 4.6 3.6 1.0 0.2 Iris-setosa
23 5.1 3.3 1.7 0.5 Iris-setosa
24 4.8 3.4 1.9 0.2 Iris-setosa
25 5.0 3.0 1.6 0.2 Iris-setosa
26 5.0 3.4 1.6 0.4 Iris-setosa
27 5.2 3.5 1.5 0.2 Iris-setosa
28 5.2 3.4 1.4 0.2 Iris-setosa
29 4.7 3.2 1.6 0.2 Iris-setosa
... ... ... ... ... ...
120 6.9 3.2 5.7 2.3 Iris-virginica
121 5.6 2.8 4.9 2.0 Iris-virginica
122 7.7 2.8 6.7 2.0 Iris-virginica
123 6.3 2.7 4.9 1.8 Iris-virginica
124 6.7 3.3 5.7 2.1 Iris-virginica
125 7.2 3.2 6.0 1.8 Iris-virginica
126 6.2 2.8 4.8 1.8 Iris-virginica
127 6.1 3.0 4.9 1.8 Iris-virginica
128 6.4 2.8 5.6 2.1 Iris-virginica
129 7.2 3.0 5.8 1.6 Iris-virginica
130 7.4 2.8 6.1 1.9 Iris-virginica
131 7.9 3.8 6.4 2.0 Iris-virginica
132 6.4 2.8 5.6 2.2 Iris-virginica
133 6.3 2.8 5.1 1.5 Iris-virginica
134 6.1 2.6 5.6 1.4 Iris-virginica
135 7.7 3.0 6.1 2.3 Iris-virginica
136 6.3 3.4 5.6 2.4 Iris-virginica
137 6.4 3.1 5.5 1.8 Iris-virginica
138 6.0 3.0 4.8 1.8 Iris-virginica
139 6.9 3.1 5.4 2.1 Iris-virginica
140 6.7 3.1 5.6 2.4 Iris-virginica
141 6.9 3.1 5.1 2.3 Iris-virginica
142 5.8 2.7 5.1 1.9 Iris-virginica
143 6.8 3.2 5.9 2.3 Iris-virginica
144 6.7 3.3 5.7 2.5 Iris-virginica
145 6.7 3.0 5.2 2.3 Iris-virginica
146 6.3 2.5 5.0 1.9 Iris-virginica
147 6.5 3.0 5.2 2.0 Iris-virginica
148 6.2 3.4 5.4 2.3 Iris-virginica
149 5.9 3.0 5.1 1.8 Iris-virginica

150 rows × 5 columns

iris_df.index
RangeIndex(start=0, stop=150, step=1)
col = iris_df ["sepal length"] print(type(col)) col
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) /usr/local/lib/python3.4/dist-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance) 2133 try: -> 2134 return self._engine.get_loc(key) 2135 except KeyError: pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4433)() pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4279)() pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13742)() pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13696)() KeyError: 'sepal length' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) <ipython-input-58-2ca01e06736b> in <module>() ----> 1 col = iris_df ["sepal length"] 2 print(type(col)) 3 col /usr/local/lib/python3.4/dist-packages/pandas/core/frame.py in __getitem__(self, key) 2057 return self._getitem_multilevel(key) 2058 else: -> 2059 return self._getitem_column(key) 2060 2061 def _getitem_column(self, key): /usr/local/lib/python3.4/dist-packages/pandas/core/frame.py in _getitem_column(self, key) 2064 # get column 2065 if self.columns.is_unique: -> 2066 return self._get_item_cache(key) 2067 2068 # duplicate columns & possible reduce dimensionality /usr/local/lib/python3.4/dist-packages/pandas/core/generic.py in _get_item_cache(self, item) 1384 res = cache.get(item) 1385 if res is None: -> 1386 values = self._data.get(item) 1387 res = self._box_item_values(item, values) 1388 cache[item] = res /usr/local/lib/python3.4/dist-packages/pandas/core/internals.py in get(self, item, fastpath) 3541 3542 if not isnull(item): -> 3543 loc = self.items.get_loc(item) 3544 else: 3545 indexer = np.arange(len(self.items))[isnull(self.items)] /usr/local/lib/python3.4/dist-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance) 2134 return self._engine.get_loc(key) 2135 except KeyError: -> 2136 return self._engine.get_loc(self._maybe_cast_indexer(key)) 2137 2138 indexer = self.get_indexer([key], method=method, tolerance=tolerance) pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4433)() pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4279)() pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13742)() pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13696)() KeyError: 'sepal length'
iris_df[attr_names[0]]
class_num = iris_df["class"] class_names = class_num.unique() class_names = list(class_names) print(class_names) class_num = class_num.apply(lambda val: class_names.index(val)+1) print(class_num)
iris_df["class_num"] = class_num iris_df
X1 = iris_df[attr_names[0]].values X2 = iris_df[attr_names[1]].values X3 = iris_df[attr_names[2]].values X4 = iris_df[attr_names[3]].values print(X4)
X = np.c_[X1, X2, X3, X4] print(X.shape) print(X)
Y = iris_df["class_num"].values print(Y)
import matplotlib.pyplot as plt
X[Y==1]
fltr = Y==1 print(fltr) print(X[fltr])
plt.figure(figsize=(5,4)) idx1 = (Y==1) idx2 = (Y==2) idx3 = (Y==3) plt.scatter(X[:,1][idx1], X[:,2][idx1], s=36, c='r', label='класс 1') plt.scatter(X[:,1][idx2], X[:,2][idx2], s=36, c='b', label='класс 2') plt.scatter(X[:,1][idx3], X[:,2][idx3], s=36, c='g', label='класс 3') plt.xlabel(attr_names[1]) plt.ylabel(attr_names[2]) plt.legend(loc='best') plt.grid(1) plt.show()
m=4 idx=(Y==1)