Sei sulla pagina 1di 7

Traceback (most recent call last):

File "<stdin>", line 1, in <module>


NameError: name 'sr' is not defined
>>> sq=[x * x for x in range(1,11)]
>>> sq
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> b=[x<5 for x in range(1,11)]
>>> b
[True, True, True, True, False, False, False, False, False, False]
>>>

>>> def add(x,y):


... return x+y
...
>>> def sub(x,y):
... return x-y
...
>>> def mult(f1,f2,x,y):
... prod=f1(x,y)*f2(x,y)
... return prod
...
>>> mult(add,sub,10,5)
75

##fibo

def fin(n):
result=[0,1]
a,b=0,1
i=1
while i<n:
a,b=b,a+b
result.append(b)
i+=1
return result

print(fin(5))

# fibo with exception

def fin(n):
result=[0,1]
a,b=0,1
i=0
while i<n-2:
a,b=b,a+b
result.append(b)
i+=1
return result

try:

n=int(input("enter a no"))
print(fin(n))
except :

print("oops enter an integer")

#### Class

class User(object):

def __init__(self,fname,lname,email):

self.fname=fname
self.lname=lname
self.email=email

def get_details(self):
print("User's Name: {} {}".format (self.fname,self.lname))
print("Email: {}".format(self.email))

>>> import class_1


>>> dir(class_1)
['User', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__n
ame__', '__package__', '__spec__', 'get_details']
>>> vd=User("arun","anand","basu@gmail.com")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'User' is not defined
>>> vd.fname
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'vd' is not defined
>>> from class_1 import User
>>> vd=User("arun","anand","basu@gmail.com")
>>> vd.fname
'arun'
>>> vd.email
'basu@gmail.com'

class User(object):

def __init__(self,fname,lname,email):

self.fname=fname
self.lname=lname
self.email=email

if self.email.endswith(".com") and ("@" in self.email):


print("sigup success")
else:
print("email invalid")
def get_details(self):
print("User's Name: {} {}".format (self.fname,self.lname))
print("Email: {}".format(self.email))
myuser=User("arun","anand","basuec10@gmail.com")

(C:\ProgramData\Anaconda3) C:\Users\win7\Desktop\arun>python class_2.py


sigup success

(C:\ProgramData\Anaconda3) C:\Users\win7\Desktop\arun>python class_2.py


email invalid

(C:\ProgramData\Anaconda3) C:\Users\win7\Desktop\arun>

class Dog():
def __init__(self, name, age):
self.name = name
self.age = age
def sit(self):
print(self.name.title() + " is now sitting.")
def roll_over(self):
print(self.name.title() + " rolled over!")

>>> from class_3 import Dog


>>> my_dog=Dog('willi',6)
>>> print("My dog's name is " + my_dog.name.title() + ".")
My dog's name is Willi.
>>> print("My dog is " + str(my_dog.age) + " years old.")
My dog is 6 years old.
>>>>>> my_dog.name
'willi'
>>> my_dog.sit()
Willi is now sitting.
>>> my_dog.roll_over()
Willi rolled over!
>>>

class BankAccount(object):
def __init__(self, initial_balance=0):
self.balance = initial_balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
def overdrawn(self):
return self.balance < 0
my_account = BankAccount(15)
my_account.withdraw(5)
print (my_account.balance)

>>> from class_6 import BankAccount


>>> my_account=BankAccount(100)
>>> my_account.withdraw(10)
>>> print(my_account.balance)
90

# numpy

>>> import numpy as np


>>> a= np.array([1,2,3,4])
>>> a
array([1, 2, 3, 4])
>>> n=np.arange(1,15)
>>> n
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
>>> n.reshape(2,8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: cannot reshape array of size 14 into shape (2,8)
>>> n.reshape(2,7)
array([[ 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14]])
>>> n.reshape(2,5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: cannot reshape array of size 14 into shape (2,5)
>>> n.reshape(2,4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: cannot reshape array of size 14 into shape (2,4)
>>> n.reshape(7,2)
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12],
[13, 14]])
>>> matrix=np.array([])
>>> matrix=np.array([1,2,3],[4,5,6],[6,7,8])

^
SyntaxError: invalid syntax
>>> matrix= np.array ([[1,2,3],[4,5,6]])
>>> matrix
array([[1, 2, 3],
[4, 5, 6]])
>>> matrix= np.array ([[1,2,3],[4,5,6],[6,7,8]])
>>> matrix
array([[1, 2, 3],
[4, 5, 6],
[6, 7, 8]])
>>> type(matrix)
<class 'numpy.ndarray'>
>>> matrix.dtype
dtype('int32')
>>>

>>> matrix2=np.array([[1,2,3],[4,5,6]],dtype=np.float64)
>>> matrix2
array([[ 1., 2., 3.],
[ 4., 5., 6.]])
>>> matrix2.dtype
dtype('float64')
>>> np.zeros((3,5),dtype='int32')
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
>>> np.ones((3,5),dtype='int32')
array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]])
>>>
(C:\ProgramData\Anaconda3) C:\Users\win7\Desktop\arun>jupyter notebook

http://localhost:8888/tree#

http://localhost:8888/notebooks/Untitled.ipynb?kernel_name=python3

import numpy as np
import time
import sys
size=1000
I1=range(size)
I2=range(size)

start=time.time()
final=[x+y for x,y in zip(I1,I2)]
print(time.time()-start)

size=1000
I1=range(size)
I2=range(size)

start=time.time()
final=[x+y for x,y in zip(I1,I2)]
print((time.time()-start)*1000)

1.0001659393310547

a1=np.arange(size)
a2=np.arange(size)

start=time.time()
final=a1+a2
print((time.time()-start)*1000)

73.00400733947754

print(sys.getsizeof(5)*len(I1))

14000

print(a1.itemsize*a1.size)
4000

----------------------------------------
import numpy as np
import matplotlib.pyplot as pit
import pandas as pd
from sklearn.datasets import load_boston

boston=load_boston()

{'data': array([[ 6.32000000e-03, 1.80000000e+01, 2.31000000e+00, ...,


1.53000000e+01, 3.96900000e+02, 4.98000000e+00],
[ 2.73100000e-02, 0.00000000e+00, 7.07000000e+00, ...,
1.78000000e+01, 3.96900000e+02, 9.14000000e+00],
[ 2.72900000e-02, 0.00000000e+00, 7.07000000e+00, ...,
1.78000000e+01, 3.92830000e+02, 4.03000000e+00],
...,
[ 6.07600000e-02, 0.00000000e+00, 1.19300000e+01, ...,
2.10000000e+01, 3.96900000e+02, 5.64000000e+00],
[ 1.09590000e-01, 0.00000000e+00, 1.19300000e+01, ...,
2.10000000e+01, 3.93450000e+02, 6.48000000e+00],
[ 4.74100000e-02, 0.00000000e+00, 1.19300000e+01, ...,
2.10000000e+01, 3.96900000e+02, 7.88000000e+00]]), 'target':
array([ 24. , 21.6, 34.7, 33.4, 36.2, 28.7, 22.9, 27.1, 16.5,

boston.feature_names

array(['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD',


'TAX', 'PTRATIO', 'B', 'LSTAT'],
dtype='<U7')

features=pd.DataFrame(boston.data,columns=boston.feature_names)
target=pd.DataFrame(boston.target,columns=["Target"])
features.head(10)

CRIM ZN INDUS CHAS NOX RM AGE DIS RAD TAX PTRATIO B


LSTAT
0 0.00632 18.0 2.31 0.0 0.538 6.575 65.2 4.0900 1.0
296.0 15.3 396.90 4.98
1 0.02731 0.0 7.07 0.0 0.469 6.421 78.9 4.9671 2.0
242.0 17.8 396.90 9.14
2 0.02729 0.0 7.07 0.0 0.469 7.185 61.1 4.9671 2.0
242.0 17.8 392.83 4.03
3 0.03237 0.0 2.18 0.0 0.458 6.998 45.8 6.0622 3.0
222.0 18.7 394.63 2.94
4 0.06905 0.0 2.18 0.0 0.458 7.147 54.2 6.0622 3.0
222.0 18.7 396.90 5.33
5 0.02985 0.0 2.18 0.0 0.458 6.430 58.7 6.0622 3.0
222.0 18.7 394.12 5.21
6 0.08829 12.5 7.87 0.0 0.524 6.012 66.6 5.5605 5.0
311.0 15.2 395.60 12.43
7 0.14455 12.5 7.87 0.0 0.524 6.172 96.1 5.9505 5.0
311.0 15.2 396.90 19.15
8 0.21124 12.5 7.87 0.0 0.524 5.631 100.0 6.0821
5.0 311.0 15.2 386.63 29.93
9 0.17004 12.5 7.87 0.0 0.524 6.004 85.9 6.5921 5.0
311.0 15.2 386.71 17.10
print(boston.DESCR)

Boston House Prices dataset


===========================

Notes
------
Data Set Characteristics:

:Number of Instances: 506

:Number of Attributes: 13 numeric/categorical predictive

:Median Value (attribute 14) is usually the target

:Attribute Information (in order):


- CRIM per capita crime rate by town
- ZN proportion of residential land zoned for lots over 25,000 sq.ft.
- INDUS proportion of non-retail business acres per town
- CHAS Charles River dummy variable (= 1 if tract bounds river; 0
otherwise)
- NOX nitric oxides concentration (parts per 10 million)
- RM average number of rooms per dwelling
- AGE proportion of owner-occupied units built prior to 1940
- DIS weighted distances to five Boston employment centres
- RAD index of accessibility to radial highways
- TAX full-value property-tax rate per $10,000
- PTRATIO pupil-teacher ratio by town
- B 1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town
- LSTAT % lower status of the population
- MEDV Median value of owner-occupied homes in $1000's

x=features["LSTAT"]
y=target

%matplotlib inline
plt.plot(x,y,'b.')

---------------------------------

>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)
'C:\\Users\\HP\\AppData\\Local\\Programs\\Python\\Python36-32'

Potrebbero piacerti anche