数据挖掘第一周作业
程序员文章站
2024-02-12 13:31:52
...
c=np.arange(1,13).reshape(6,2)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-ab91df390b78> in <module>
----> 1 c=np.arange(1,13).reshape(6,2)
NameError: name 'np' is not defined
import numpy
c=np.arange(1,13).reshape(6,2)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-ab91df390b78> in <module>
----> 1 c=np.arange(1,13).reshape(6,2)
NameError: name 'np' is not defined
import numpy as np
c=np.arange(1,13).reshape(6,2)
c
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12]])
np.vsplit(c,3)
[array([[1, 2],
[3, 4]]),
array([[5, 6],
[7, 8]]),
array([[ 9, 10],
[11, 12]])]
d=c.T
d
array([[ 1, 3, 5, 7, 9, 11],
[ 2, 4, 6, 8, 10, 12]])
np.hsplit(d,3)
[array([[1, 3],
[2, 4]]),
array([[5, 7],
[6, 8]]),
array([[ 9, 11],
[10, 12]])]
e=np.dstack((a,b))
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-11-8536c3abef53> in <module>
----> 1 e=np.dstack((a,b))
NameError: name 'a' is not defined
e=array([[[11, 21],
[12, 22],
[13, 23]],
[[14, 24],
[15, 25],
[16, 26]],
[[17, 27],
[18, 28],
[19, 29]]])
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-12-2ab0b7cdb590> in <module>
----> 1 e=array([[[11, 21],
2 [12, 22],
3 [13, 23]],
4
5 [[14, 24],
NameError: name 'array' is not defined
e=([[[11, 21],
[12, 22],
[13, 23]],
[[14, 24],
[15, 25],
[16, 26]],
[[17, 27],
[18, 28],
[19, 29]]])
np.dsplit(e,2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
D:\progarmdata\Anaconda3\lib\site-packages\numpy\lib\shape_base.py in split(ary, indices_or_sections, axis)
866 try:
--> 867 len(indices_or_sections)
868 except TypeError:
TypeError: object of type 'int' has no len()
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-14-be1746c3d12c> in <module>
----> 1 np.dsplit(e,2)
<__array_function__ internals> in dsplit(*args, **kwargs)
D:\progarmdata\Anaconda3\lib\site-packages\numpy\lib\shape_base.py in dsplit(ary, indices_or_sections)
1034 if _nx.ndim(ary) < 3:
1035 raise ValueError('dsplit only works on arrays of 3 or more dimensions')
-> 1036 return split(ary, indices_or_sections, 2)
1037
1038 def get_array_prepare(*args):
<__array_function__ internals> in split(*args, **kwargs)
D:\progarmdata\Anaconda3\lib\site-packages\numpy\lib\shape_base.py in split(ary, indices_or_sections, axis)
868 except TypeError:
869 sections = indices_or_sections
--> 870 N = ary.shape[axis]
871 if N % sections:
872 raise ValueError(
AttributeError: 'list' object has no attribute 'shape'
a=np.array([1,1,1,1])
b=np.array([[1],[1],[1],[1]])
a+b
array([[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2]])
c=np.array([[1,1,1,1]])
c+b
array([[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2]])
W=np.array([[1,1,1],[2,2,2]])
W[:1]
array([[1, 1, 1]])
W=np.array([[1,1,1],[2,2,2]])
W[:,1]
array([1, 2])
W[1]
array([2, 2, 2])
W[:,1]=np.array([5,5])
W
array([[1, 5, 1],
[2, 5, 2]])
matrix=[
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
]
p1=np.delete(matrix,1,0)
print(">>>>p1>>>>\n",p1)
p2=np.delete(matrix,1,1)
print(">>>>p2>>>>\n",p2)
p3=np.delete(matrix,1)
print(">>>>p3>>>>\n",p3)
p4=np.delete(matrix,[0,1],1)
print(">>>>p4>>>>\n",p4)
>>>>p1>>>>
[[ 1 2 3 4]
[ 9 10 11 12]]
>>>>p2>>>>
[[ 1 3 4]
[ 5 7 8]
[ 9 11 12]]
>>>>p3>>>>
[ 1 3 4 5 6 7 8 9 10 11 12]
>>>>p4>>>>
[[ 3 4]
[ 7 8]
[11 12]]
q1=np.insert(matrix,1,[1,1,1,1],0)
print(">>>>q1>>>>\n",q1)
q2=np.insert(matrix,0,[1,1,1],1)
print(">>>>q2>>>>\n",q2)
q3=np.insert(matrix,3,[1,1,1,1],0)
print(">>>>q3>>>>\n",q3)
>>>>q1>>>>
[[ 1 2 3 4]
[ 1 1 1 1]
[ 5 6 7 8]
[ 9 10 11 12]]
>>>>q2>>>>
[[ 1 1 2 3 4]
[ 1 5 6 7 8]
[ 1 9 10 11 12]]
>>>>q3>>>>
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[ 1 1 1 1]]
m1=np.append(matrix,[[1,1,1,1]],axis=0)
print(">>>>m1>>>>\n",m1)
m2=np.append(matrix,[[1],[1],[1]],axis=1)
print(">>>>m2>>>>\n",m2)
m3=np.append(matrix,[1,1,1,1])
print(">>>>m3>>>>\n",m3)
>>>>m1>>>>
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[ 1 1 1 1]]
>>>>m2>>>>
[[ 1 2 3 4 1]
[ 5 6 7 8 1]
[ 9 10 11 12 1]]
>>>>m3>>>>
[ 1 2 3 4 5 6 7 8 9 10 11 12 1 1 1 1]
a1=np.random.choice(7,5)
a1
array([4, 2, 4, 2, 6])
a2=np.randox.choice([0,1,2,3,4,5,6],5)
a2
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-25-c81ade230897> in <module>
----> 1 a2=np.randox.choice([0,1,2,3,4,5,6],5)
2 a2
D:\progarmdata\Anaconda3\lib\site-packages\numpy\__init__.py in __getattr__(attr)
212 return Tester
213 else:
--> 214 raise AttributeError("module {!r} has no attribute "
215 "{!r}".format(__name__, attr))
216
AttributeError: module 'numpy' has no attribute 'randox'
a2=np.random.choice([0,1,2,3,4,5,6],5)
a2
array([6, 0, 0, 0, 6])
a3=np.random.choice(np.array([0,1,2,3,4,5,6]),5)
a3
array([0, 2, 5, 1, 4])
a4=np.random.choice([0,1,2,3,4,5,6],5,replace=False)
a4
array([5, 2, 1, 4, 6])
a5=np.random.choice(np.array([0,1,2,3,4,5,6]),5)
p
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-30-6c10289a8da7> in <module>
----> 1 p
NameError: name 'p' is not defined
a5=np.random.choice(np.array([0,1,2,3,4,5,6]),5,p=[0.1,0.1,0.1,0.1,0.1,0.1,0.4])
a5
array([4, 4, 4, 6, 2])
a=np.array([[1,1,1],[2,2,2],[0,3,6]])
a
array([[1, 1, 1],
[2, 2, 2],
[0, 3, 6]])
b1=np.argmax(a)
b1
8
b2=np.argmax(a,axis=0)
b2
array([1, 2, 2], dtype=int64)
b3=np.argmax(a,axis=1)
b3
array([0, 0, 2], dtype=int64)
y1=np.linspace(-10.0,10.0)
y1
array([-10. , -9.59183673, -9.18367347, -8.7755102 ,
-8.36734694, -7.95918367, -7.55102041, -7.14285714,
-6.73469388, -6.32653061, -5.91836735, -5.51020408,
-5.10204082, -4.69387755, -4.28571429, -3.87755102,
-3.46938776, -3.06122449, -2.65306122, -2.24489796,
-1.83673469, -1.42857143, -1.02040816, -0.6122449 ,
-0.20408163, 0.20408163, 0.6122449 , 1.02040816,
1.42857143, 1.83673469, 2.24489796, 2.65306122,
3.06122449, 3.46938776, 3.87755102, 4.28571429,
4.69387755, 5.10204082, 5.51020408, 5.91836735,
6.32653061, 6.73469388, 7.14285714, 7.55102041,
7.95918367, 8.36734694, 8.7755102 , 9.18367347,
9.59183673, 10. ])
y2=np.linspace(1,10,10)
y2
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
y3=.np.linspace(1,10,10,endpoint=False)
y3
File "<ipython-input-39-7f2a9d6c946f>", line 1
y3=.np.linspace(1,10,10,endpoint=False)
^
SyntaxError: invalid syntax
y3=np.linspace(1,10,10,endpoint=False)
y3
array([1. , 1.9, 2.8, 3.7, 4.6, 5.5, 6.4, 7.3, 8.2, 9.1])
y4=np.linspace(1,10,6,restep=True)
y4
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-41-ab9744f3b0e8> in <module>
----> 1 y4=np.linspace(1,10,6,restep=True)
2 y4
<__array_function__ internals> in linspace(*args, **kwargs)
TypeError: _linspace_dispatcher() got an unexpected keyword argument 'restep'
y4=np.linspace(1,10,6,retstep=True)
y4
(array([ 1. , 2.8, 4.6, 6.4, 8.2, 10. ]), 1.8)
a = np.array([1,1,1,1])
b = np.array([[1],[1],[1],[1]])
e = np.dstack((a,b))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-43-3ff6a14b9efc> in <module>
1 a = np.array([1,1,1,1])
2 b = np.array([[1],[1],[1],[1]])
----> 3 e = np.dstack((a,b))
<__array_function__ internals> in dstack(*args, **kwargs)
D:\progarmdata\Anaconda3\lib\site-packages\numpy\lib\shape_base.py in dstack(tup)
721 if not isinstance(arrs, list):
722 arrs = [arrs]
--> 723 return _nx.concatenate(arrs, 2)
724
725
<__array_function__ internals> in concatenate(*args, **kwargs)
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 1 and the array at index 1 has size 4
x=np.array([[1,2,3],[4,5,6],[1,2,3]])
x.flatten()
array([1, 2, 3, 4, 5, 6, 1, 2, 3])
x.ravel()
array([1, 2, 3, 4, 5, 6, 1, 2, 3])
x.ravel("F")
array([1, 4, 1, 2, 5, 2, 3, 6, 3])
x.flatten("F")
array([1, 4, 1, 2, 5, 2, 3, 6, 3])
x.flatten()[1]=20
x
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3]])
x.ravel()[1]=20
x
array([[ 1, 20, 3],
[ 4, 5, 6],
[ 1, 2, 3]])
x.reshape(1,-1)
array([[ 1, 20, 3, 4, 5, 6, 1, 2, 3]])
x=np.array([1,2,3,6,7,8])
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-1613df480449> in <module>
----> 1 x=np.array([1,2,3,6,7,8])
NameError: name 'np' is not defined
import numpy as np
x=np.array([1,2,3,6,7,8])
x[None,:]
array([[1, 2, 3, 6, 7, 8]])
x[:None]
array([1, 2, 3, 6, 7, 8])
x[:,None]
array([[1],
[2],
[3],
[6],
[7],
[8]])
x[np.newaxis,:]
array([[1, 2, 3, 6, 7, 8]])
x=np.array([[1,2,3],[2,3,4]])
np.prod(x)
144
np.prod(x,axis=1)
array([ 6, 24])
np.prod(x,axis=0)
array([ 2, 6, 12])
x=np.array([[1,2,3],[-3,2,4],[5,-2,9]])
x
array([[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]])
y1=np.maximum(0,x)
y1
array([[1, 2, 3],
[0, 2, 4],
[5, 0, 9]])
y2=np.minimum(0,x)
y2
array([[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]])
x1=x.copy()
x1
array([[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]])
x1[x1<0]=0
x1
array([[1, 2, 3],
[0, 2, 4],
[5, 0, 9]])
x2=x.copy()
x2[x2>0]=0
x2
array([[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]])
x=np.array([[1,2,3],[-3,2,4],[5,-2,9]])
x
array([[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]])
x1=x.copy()
x1[x1>0]=0
x1
array([[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]])
x
array([[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]])
x2=x
x2
array([[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]])
x2[x2>0]=0
x2
array([[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]])
x
array([[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]])
x=np.array([[1,2,3],[-3,2,4],[5,-2,9]])
x3=x[2]
x3
array([ 5, -2, 9])
x3[2]=100
x
array([[ 1, 2, 3],
[ -3, 2, 4],
[ 5, -2, 100]])
x=np.array([[1,2,3],[4,5,6]])
np.zeros_like(x)
array([[0, 0, 0],
[0, 0, 0]])
np.random.rand(3,4)
n
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-26-0724c215bdca> in <module>
1 np.random.rand(3,4)
----> 2 n
NameError: name 'n' is not defined
n=np.random.rand(3,4)
n
array([[0.34791141, 0.20320878, 0.87996176, 0.87030528],
[0.03708444, 0.9414981 , 0.46970678, 0.61123357],
[0.1661619 , 0.53021957, 0.08446142, 0.02144265]])
x=np.random.randn(2,3)
x
array([[-0.11720869, 1.15617183, -0.19736795],
[-0.25257873, -0.58597682, -0.49646397]])
y=np.multiply(0.1,np.random,randn(2,3))+0.5
y
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-29-8fc98edcd3a8> in <module>
----> 1 y=np.multiply(0.1,np.random,randn(2,3))+0.5
2 y
NameError: name 'randn' is not defined
y=np.multiply(0.1,np.random.randn(2,3))+0.5
y
array([[0.53466755, 0.46847936, 0.37939621],
[0.51870802, 0.55021238, 0.491875 ]])
z=np.random.randint(2,9,(2,3))
z
array([[5, 6, 2],
[6, 7, 4]])
m=np.random,randint(9,size=(2,3))
m
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-32-6a0decdc2288> in <module>
----> 1 m=np.random,randint(9,size=(2,3))
2 m
NameError: name 'randint' is not defined
m=np.random.randint(9,size=(2,3))
m
array([[3, 8, 1],
[4, 2, 5]])
x="You are right"
type(x)
str
assert type(x)==str,"x is not str"
x=[1,2,3]
type(x)
list
assert type(x)==str,"x is not str"
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-36-5b482e2efed8> in <module>
----> 1 assert type(x)==str,"x is not str"
AssertionError: x is not str
A=np.arrange(95,99).reshape(2,2)
A
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-37-824c81e341b7> in <module>
----> 1 A=np.arrange(95,99).reshape(2,2)
2 A
D:\anaconda3\lib\site-packages\numpy\__init__.py in __getattr__(attr)
217 return Tester
218 else:
--> 219 raise AttributeError("module {!r} has no attribute "
220 "{!r}".format(__name__, attr))
221
AttributeError: module 'numpy' has no attribute 'arrange'
A=np.arange(95,99).reshape(2,2)
A
array([[95, 96],
[97, 98]])
np.pad(A,((3,2),(2,3)),"constant",constant_values=(0,0))
array([[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 95, 96, 0, 0, 0],
[ 0, 0, 97, 98, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0]])
b=np.array([[[1,2],[3,4],[7,8],[4,5],[1,2]]])
b
array([[[1, 2],
[3, 4],
[7, 8],
[4, 5],
[1, 2]]])
np.pad(b,((0,0),(1,1),(1,1)),"constant",constant_values=0)
array([[[0, 0, 0, 0],
[0, 1, 2, 0],
[0, 3, 4, 0],
[0, 7, 8, 0],
[0, 4, 5, 0],
[0, 1, 2, 0],
[0, 0, 0, 0]]])
x=np.empty([3,2],dtype=int)
print(x)
[[0 0]
[1 1]
[1 1]]
x=np.empty([3,2],dtype=int)
print(x)
[[0 0]
[1 1]
[1 1]]
numpy.empty(shape,dtype=float,order="C)
1
File "<ipython-input-44-e7ba450d70e1>", line 1
numpy.empty(shape,dtype=float,order="C)
^
SyntaxError: EOL while scanning string literal
numpy.empty(shape,dtype=float,order="C")
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-45-43678a75714b> in <module>
----> 1 numpy.empty(shape,dtype=float,order="C")
NameError: name 'numpy' is not defined
c=np.array([[1,2],[3,4]])
c
array([[1, 2],
[3, 4]])
c.astype(np.float32)
array([[1., 2.],
[3., 4.]], dtype=float32)
x=np.array([1,3,5])
y=np.array([4,6])
XX,YY=np.meshgrid(x,y)
XX
array([[1, 3, 5],
[1, 3, 5]])
YY
array([[4, 4, 4],
[6, 6, 6]])
x=np.array([[3,4,5],[1,3,4]])
y=np.array([[1,1,1],[2,2,2]])
np.hstack(x,y)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-50-21151a5e3975> in <module>
1 x=np.array([[3,4,5],[1,3,4]])
2 y=np.array([[1,1,1],[2,2,2]])
----> 3 np.hstack(x,y)
<__array_function__ internals> in hstack(*args, **kwargs)
TypeError: _vhstack_dispatcher() takes 1 positional argument but 2 were given
x=np.array([[3,4,5],[1,3,4]])
y=np.array([[1,1,1],[2,2,2]])
np.hstack((x,y))
array([[3, 4, 5, 1, 1, 1],
[1, 3, 4, 2, 2, 2]])
np.vstack((x,y))
array([[3, 4, 5],
[1, 3, 4],
[1, 1, 1],
[2, 2, 2]])
a=np.array([0.125,0.568,5.688])
np.round(a)
array([0., 1., 6.])
np.round(a,decimals=2)
array([0.12, 0.57, 5.69])
np.floor(a)
array([0., 0., 5.])
np.ceil(a)
array([1., 1., 6.])
a=np.array([[1,2,3],[4,5,6]])
a=np.array([[1,2,3,6],[4,5,6,6]])
a1=a.reshape((1,2,4))
a1
array([[[1, 2, 3, 6],
[4, 5, 6, 6]]])
b=np.array([[3,4,5,6],[1,2,3,4],[4,5,5,5]])
b
array([[3, 4, 5, 6],
[1, 2, 3, 4],
[4, 5, 5, 5]])
b1=b.reshape((1,3,4)).tanspose((1,0,2))
b1
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-59-2095ea0eac44> in <module>
----> 1 b1=b.reshape((1,3,4)).tanspose((1,0,2))
2 b1
AttributeError: 'numpy.ndarray' object has no attribute 'tanspose'
b1=b.reshape((1,3,4)).transpose((1,0,2))
b1
array([[[3, 4, 5, 6]],
[[1, 2, 3, 4]],
[[4, 5, 5, 5]]])
a1
array([[[1, 2, 3, 6],
[4, 5, 6, 6]]])
a1+b1
array([[[ 4, 6, 8, 12],
[ 7, 9, 11, 12]],
[[ 2, 4, 6, 10],
[ 5, 7, 9, 10]],
[[ 5, 7, 8, 11],
[ 8, 10, 11, 11]]])
c=np.array([[[1,2,5],[3,4,6],[4,5,6],[7,8,9]]])
c
array([[[1, 2, 5],
[3, 4, 6],
[4, 5, 6],
[7, 8, 9]]])
c.transpose(1,0,2)
array([[[1, 2, 5]],
[[3, 4, 6]],
[[4, 5, 6]],
[[7, 8, 9]]])
c.transpose(1,2,0)
array([[[1],
[2],
[5]],
[[3],
[4],
[6]],
[[4],
[5],
[6]],
[[7],
[8],
[9]]])
a=np.array([2,3,4,5,5,6,7])
a[0:7:2]
array([2, 4, 5, 7])
a=np.array([2,2,3,4,5,5,6,7])
a[0::2]
array([2, 3, 5, 6])
a[::-1]
array([7, 6, 5, 5, 4, 3, 2, 2])
a=np.array([2,2,3,4,5,5,6,7])
s=slice(0,7,2)
a[s]
array([2, 3, 5, 6])