Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Numpy Mathlib

This document contains code snippets demonstrating the use of the NumPy library in Python for working with numerical arrays and matrices. It shows how to create, manipulate, and visualize numeric data using NumPy and Matplotlib.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Numpy Mathlib

This document contains code snippets demonstrating the use of the NumPy library in Python for working with numerical arrays and matrices. It shows how to create, manipulate, and visualize numeric data using NumPy and Matplotlib.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1/28/24, 5:22 PM Untitled0.

ipynb - Colaboratory

import numpy as np

a=np.array([1,2,3,4])
print(a)
b=np.array([1,2.3,4,5])
print(b)
a1=np.array([[1,2],[1,2]])
print(a1)

print(a.shape)
print(a.ndim)
print(a.dtype.name)
print(a.itemsize)
print(a.size)
print(type(a))
print(np.ndarray)

[1 2 3 4]
[1. 2.3 4. 5. ]
[[1 2]
[1 2]]
(4,)
1
int64
8
4
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>

import numpy as np

a=np.linspace(1,10,5) # (start+end)/n all time add the ratio


print(a)

e=np.ones(5)
f=np.zeros(5)
print(e,f)

a=np.array([1,2,3,4])
a=a+5
print(a)
a=a-5
print(a)
a=a*5
print(a)
a=a/5
print(a)

b=np.array([2,3,4,5,6])
h=b
b=b+4
print(b)
print(h)

k=np.copy(b)
k[0]=0
print(k)
print(b)

a=np.linspace(1,10,6)
print(a)

[ 1. 3.25 5.5 7.75 10. ]


[1. 1. 1. 1. 1.] [0. 0. 0. 0. 0.]
[6 7 8 9]
[1 2 3 4]
[ 5 10 15 20]
[1. 2. 3. 4.]
[ 6 7 8 9 10]
[2 3 4 5 6]
[ 0 7 8 9 10]
[ 6 7 8 9 10]
[ 1. 2.8 4.6 6.4 8.2 10. ]

https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 1/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
import numpy as np
b=np.arange(10) # 0 to 9
print(b)

c=np.arange(5,10)
d=np.arange(10,1,-1)
print(c)
print(d)

[0 1 2 3 4 5 6 7 8 9]
[5 6 7 8 9]
[10 9 8 7 6 5 4 3 2]

import numpy as np
a=np.array([1,2,3,4])
b=np.array([5,6,7,8])
c=np.concatenate([a,b])
print(c)

[1 2 3 4 5 6 7 8]

import numpy as np

a=np.array([[1,2,3,4],[2,3,4,5]])
print(a)
a=a+4 #+,-,*,/
print(a)
print(a.ndim)
print(a.shape)
print(a.size)
print(a.itemsize)
print(a.reshape(8,1))
print(a.reshape(2,4))

[[1 2 3 4]
[2 3 4 5]]
[[5 6 7 8]
[6 7 8 9]]
2
(2, 4)
8
8
[[5]
[6]
[7]
[8]
[6]
[7]
[8]
[9]]
[[5 6 7 8]
[6 7 8 9]]

b=np.empty([2,3])
print(b)
b=np.empty([3,3],dtype=int)
print(b)
b=np.empty([1,3],dtype=float)
print(b)

[[ 1. 2.8 4.6]
[ 6.4 8.2 10. ]]
[[ 4607182418800017408 0 0]
[-9223372036854775808 -4616189618054758400 -9223372036854775808]
[ 0 0 4607182418800017408]]
[[0. 0.99999869 1. ]]

import numpy as np

a=np.array([1,2,3,4,5,6,7,8])
b=np.array_split(a,[3,5])
print(b)

c=np.array([[1,2,3,4],[2,3,4,5]])
d=np.array_split(c,4)
print(d)

sp=np.hsplit(c,2)
print(sp)
sp=np.vsplit(c,2)
print(sp)

https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 2/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory

[array([1, 2, 3]), array([4, 5]), array([6, 7, 8])]


[array([[1, 2, 3, 4]]), array([[2, 3, 4, 5]]), array([], shape=(0, 4), dtype=int64), array([], shape=(0, 4), dtype=int64)]
[array([[1, 2],
[2, 3]]), array([[3, 4],
[4, 5]])]
[array([[1, 2, 3, 4]]), array([[2, 3, 4, 5]])]

import numpy as np

a=np.array([[1,2,3,4],[2,3,4,5],[4,5,6,7]])
print(a[0:2,:])
print(a[:,1:3])

b=np.array([1,2,3,4,5,6]) #indexing
print(b[2:-2])
print(b[0:1])
print(b[0:5:2])

c=np.zeros([2,2],dtype=int)
print(c)

c=np.zeros([3,2],dtype=float)
print(c)

d=np.eye(4)
print(d)

[[1 2 3 4]
[2 3 4 5]]
[[2 3]
[3 4]
[5 6]]
[3 4]
[1]
[1 3 5]
[[0 0]
[0 0]]
[[0. 0.]
[0. 0.]
[0. 0.]]
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]

A=np.ones([2,2],dtype=int)
print(A)

A=np.ones([3,1],dtype=float)
print(A)

[[1 1]
[1 1]]
[[1.]
[1.]
[1.]]

import numpy as np

a=np.array([[1,2,3,4],[2,3,4,5]])
b=np.empty([3,4],dtype=int)
print(b)

[[93953841706394 0 0 0]
[ 0 0 0 0]
[ 0 0 0 0]]

import numpy as np
a=np.array([[1,2,3,4],[2,3,4,5]])
d=np.concatenate([a,a],axis=0)
print(d)
x=np.array([1,2,3,4,5,6])
b=np.hstack(x)
c=np.vstack(x)
print(b)
print(c)

[[1 2 3 4]
[2 3 4 5]
[1 2 3 4]

https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 3/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
[2 3 4 5]]
[1 2 3 4 5 6]
[[1]
[2]
[3]
[4]
[5]
[6]]

import numpy as np

a=np.array([[1,2,3,4],[2,3,4,5]])
a=a.reshape(4,2)
print(a)

a=([1,2,3,4,5])
b=([6,7,7,8,5])
c=np.hstack((a,b)) #np.vsplit and np.hsplit
d=np.vstack((a,b))
print(c)
print(d)

[[1 2]
[3 4]
[2 3]
[4 5]]
[1 2 3 4 5 6 7 7 8 5]
[[1 2 3 4 5]
[6 7 7 8 5]]

import matplotlib.pyplot as plt

x=([1,2,3])
y=([2,4,6])
plt.plot(x,y)
plt.xlabel("Score")
plt.ylabel("Match")
plt.title("Day-1")
plt.show()

import matplotlib.pyplot as plt


y=([1,4,9,4,45,3,7])
plt.plot( y, linestyle='-', color='b', marker='o')
plt.show()

https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 4/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory

x = np.arange(0, 3 * np.pi, 0.1)


y = np.sin(x)
plt.title("sine wave graph")
plt.plot(x,y)
x = np.arange(0, 4 * np.pi, 0.015)
y_sin = np.sin(x)
y_cos = np.cos(x)
plt.subplot(2, 1, 1)
plt.plot(x, y_sin)
plt.title('Sine Wave')
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine Wave')
plt.show()

<ipython-input-79-e3b57ab27b92>:8: MatplotlibDeprecationWarning: Auto-removal of over


plt.subplot(2, 1, 1)

x = [0,2,3,4,5,6,7]
y1=[5,7,9,0,-1,6,8]
y2=[0,6,-2,-9,3,1,9]
plt.plot(x,y1,label="Day",linewidth=4,linestyle='dashed')
plt.plot(x,y2,label="Score")
plt.legend(loc=2)
plt.show()

https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 5/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory

import matplotlib.pyplot as plt


import numpy as np
a=np.array([1,2,3,4])
b=np.array([0,8,3,0])
plt.plot(a,b,'>:y')
plt.xlabel("Score")
plt.ylabel("Knowladge")
plt.title("Day-2")
plt.xticks(np.arange(5))
plt.yticks(np.arange(9))
plt.xticks(np.arange(1,5),['a','b','c','d'], rotation=45)
plt.grid()
plt.show()

x=np.arange(1,11)
y=x*3-2
plt.plot(x,y,marker='+',markeredgecolor='red',markersize='10')
plt.show()

https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 6/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory

import matplotlib.pyplot as plt


import numpy as np
a=np.array([1,2,3,4])
b=np.array([9,8,3,3])
plt.bar(a,b)
plt.show()

a=np.array([3,6,9,1])
b=np.array([9,8,3,3])
plt.bar(a,b,width=0.6,color='pink')
plt.show()

https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 7/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
city=np.array(['Morbi','Rajkot','Halvad','Delhi'])
temp=np.array([43,34,29,45])
plt.barh(city,temp)
plt.show()

import matplotlib.pyplot as plt


import numpy as np
a=np.array([50,20,15,10,5])
b=np.array([0,0.2,0.3,0,0])
deps=['a','b','c','d','e']
plt.pie(a,b,labels=deps,colors=['blue','red','pink','yellow','green'])
plt.title("Pie Chart")
plt.show()

import matplotlib.pyplot as plt #W3SCHOOLS


import numpy as np
a=np.array([1,2,3,4,9])
b=np.array([6,8,9,0,2])
plt.plot(a,b,marker='o',linestyle='',color='k')
plt.show()

https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 8/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory

https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 9/9

You might also like