Skip to content

Commit 79e32dd

Browse files
Created using Colab
1 parent 07c3af4 commit 79e32dd

File tree

1 file changed

+374
-1
lines changed

1 file changed

+374
-1
lines changed

NumPy_101.ipynb

Lines changed: 374 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"\n",
4242
"1. 📚 Introduction\n",
4343
"2. 📚 Array Creation\n",
44-
"3. 📚 Basic Operation on NumPay Array\n",
44+
"3. 📚 Arthimetic Operations\n",
4545
"4. 📚 Basic Mathematic Operations\n",
4646
"5. 📚 Statistical Analysis\n",
4747
"6. 📚 Linear Algebra\n",
@@ -4163,6 +4163,379 @@
41634163
}
41644164
]
41654165
},
4166+
{
4167+
"cell_type": "markdown",
4168+
"source": [
4169+
"# **📚Section 6 -Linear Algebra**"
4170+
],
4171+
"metadata": {
4172+
"id": "9ac_XaU4Ubv_"
4173+
}
4174+
},
4175+
{
4176+
"cell_type": "markdown",
4177+
"source": [
4178+
"##**6.1-Basic Linear Algebra Operations with NumPy**"
4179+
],
4180+
"metadata": {
4181+
"id": "jtvjoelXVINB"
4182+
}
4183+
},
4184+
{
4185+
"cell_type": "code",
4186+
"source": [
4187+
"# Creating Arrays and Matrices:\n",
4188+
"import numpy as np\n",
4189+
"# Create a 1D array\n",
4190+
"array_1d = np.array([1, 2, 3, 4, 5])\n",
4191+
"# Create a 2D array (matrix)\n",
4192+
"matrix_2d = np.array([[1, 2], [3, 4]])"
4193+
],
4194+
"metadata": {
4195+
"id": "H2gx6f3rVdf0"
4196+
},
4197+
"execution_count": 5,
4198+
"outputs": []
4199+
},
4200+
{
4201+
"cell_type": "code",
4202+
"source": [
4203+
"# Matrix Multiplication:\n",
4204+
"result = np.dot(matrix_2d, array_1d)\n",
4205+
"\n",
4206+
"narray = np.array([1, 2, 3, 4]) # Define a numpy array\n",
4207+
"print(narray * 3)\n",
4208+
"print(alist * 3)"
4209+
],
4210+
"metadata": {
4211+
"id": "A4CZm4STVh-A"
4212+
},
4213+
"execution_count": null,
4214+
"outputs": []
4215+
},
4216+
{
4217+
"cell_type": "code",
4218+
"source": [
4219+
"# Matrix Inversion:\n",
4220+
"inverse_matrix = np.linalg.inv(matrix_2d)"
4221+
],
4222+
"metadata": {
4223+
"id": "MV0At4huVuOQ"
4224+
},
4225+
"execution_count": 7,
4226+
"outputs": []
4227+
},
4228+
{
4229+
"cell_type": "code",
4230+
"source": [
4231+
"# Eigenvalue decomposition\n",
4232+
"eigenvalues, eigenvectors = np.linalg.eig(matrix_2d)"
4233+
],
4234+
"metadata": {
4235+
"id": "nllQV8b2V2tL"
4236+
},
4237+
"execution_count": null,
4238+
"outputs": []
4239+
},
4240+
{
4241+
"cell_type": "code",
4242+
"source": [
4243+
"# Singular value decomposition (SVD)\n",
4244+
"u, s, vh = np.linalg.svd(matrix_2d)"
4245+
],
4246+
"metadata": {
4247+
"id": "i09xl-xUV5Pq"
4248+
},
4249+
"execution_count": null,
4250+
"outputs": []
4251+
},
4252+
{
4253+
"cell_type": "code",
4254+
"source": [
4255+
"# Transpose a Matrix\n",
4256+
"matrix3x2 = np.array([[1, 2], [3, 4], [5, 6]]) # Define a 3x2 matrix\n",
4257+
"print('Original matrix 3 x 2')\n",
4258+
"print(matrix3x2)\n",
4259+
"print('Transposed matrix 2 x 3')\n",
4260+
"print(matrix3x2.T)"
4261+
],
4262+
"metadata": {
4263+
"id": "OxSUYFlHV8h1"
4264+
},
4265+
"execution_count": null,
4266+
"outputs": []
4267+
},
4268+
{
4269+
"cell_type": "code",
4270+
"source": [
4271+
"# note that the transpose operation does not affect 1D arrays.\n",
4272+
"nparray = np.array([1, 2, 3, 4]) # Define an array\n",
4273+
"print('Original array')\n",
4274+
"print(nparray)\n",
4275+
"print('Transposed array')\n",
4276+
"print(nparray.T)"
4277+
],
4278+
"metadata": {
4279+
"id": "2hp-WgWMWHD3"
4280+
},
4281+
"execution_count": null,
4282+
"outputs": []
4283+
},
4284+
{
4285+
"cell_type": "code",
4286+
"source": [
4287+
"nparray = np.array([[1, 2, 3, 4]]) # Define a 1 x 4 matrix. Note the 2 level of square brackets\n",
4288+
"print('Original array')\n",
4289+
"print(nparray)\n",
4290+
"print('Transposed array')\n",
4291+
"print(nparray.T)"
4292+
],
4293+
"metadata": {
4294+
"id": "O6VUPQrgWKdh"
4295+
},
4296+
"execution_count": null,
4297+
"outputs": []
4298+
},
4299+
{
4300+
"cell_type": "code",
4301+
"source": [
4302+
"# dot product\n",
4303+
"nparray1 = np.array([0, 1, 2, 3]) # Define an array\n",
4304+
"nparray2 = np.array([4, 5, 6, 7]) # Define an array\n",
4305+
"flavor1 = np.dot(nparray1, nparray2) # Recommended way\n",
4306+
"print(flavor1)\n",
4307+
"flavor2 = np.sum(nparray1 * nparray2) # Ok way\n",
4308+
"print(flavor2)\n",
4309+
"flavor3 = nparray1 @ nparray2 # Geeks way\n",
4310+
"print(flavor3)\n",
4311+
"# As you never should do: # Noobs way\n",
4312+
"flavor4 = 0\n",
4313+
"for a, b in zip(nparray1, nparray2):\n",
4314+
" flavor4 += a * b\n",
4315+
"print(flavor4)\n",
4316+
"\n",
4317+
"norm1 = np.dot(np.array([1, 2]), np.array([3, 4])) # Dot product on nparrays\n",
4318+
"norm2 = np.dot([1, 2], [3, 4]) # Dot product on python lists\n",
4319+
"print(norm1, '=', norm2 )"
4320+
],
4321+
"metadata": {
4322+
"id": "I1GVbBPxWN4_"
4323+
},
4324+
"execution_count": null,
4325+
"outputs": []
4326+
},
4327+
{
4328+
"cell_type": "code",
4329+
"source": [
4330+
"# Sums by rows and columns\n",
4331+
"nparray2 = np.array([[1, -1], [2, -2], [3, -3]]) # Define a 3 x 2 matrix.\n",
4332+
"sumByCols = np.sum(nparray2, axis=0) # Get the sum for each column. Returns 2 elements\n",
4333+
"sumByRows = np.sum(nparray2, axis=1) # get the sum for each row. Returns 3 elements\n",
4334+
"print('Matrix mean: ')\n",
4335+
"print(mean)\n",
4336+
"print('Mean by columns: ')\n",
4337+
"print(meanByCols)\n",
4338+
"print('Mean by rows:')\n",
4339+
"print(meanByRows)"
4340+
],
4341+
"metadata": {
4342+
"id": "vAnTXcBWWT0r"
4343+
},
4344+
"execution_count": null,
4345+
"outputs": []
4346+
},
4347+
{
4348+
"cell_type": "markdown",
4349+
"source": [
4350+
"##**6.2-Advanced Linear Algebra Techniques with NumPy**"
4351+
],
4352+
"metadata": {
4353+
"id": "uNk6Hc9VWZGa"
4354+
}
4355+
},
4356+
{
4357+
"cell_type": "markdown",
4358+
"source": [
4359+
"### **Solving Linear Systems:**"
4360+
],
4361+
"metadata": {
4362+
"id": "zFoFF138XGVn"
4363+
}
4364+
},
4365+
{
4366+
"cell_type": "code",
4367+
"source": [
4368+
"# Solve linear equations\n",
4369+
"coefficients = np.array([[2, 3], [1, -1]])\n",
4370+
"constants = np.array([8, 1])\n",
4371+
"solution = np.linalg.solve(coefficients, constants)"
4372+
],
4373+
"metadata": {
4374+
"id": "Td7XmEDlXUim"
4375+
},
4376+
"execution_count": null,
4377+
"outputs": []
4378+
},
4379+
{
4380+
"cell_type": "markdown",
4381+
"source": [
4382+
"### **Least Squares Fitting**"
4383+
],
4384+
"metadata": {
4385+
"id": "0Uy1vKIjXX-m"
4386+
}
4387+
},
4388+
{
4389+
"cell_type": "code",
4390+
"source": [
4391+
"# Fit a line to data using least squares\n",
4392+
"x = np.array([0, 1, 2, 3])\n",
4393+
"y = np.array([0, 0.9, 2.1, 2.8])\n",
4394+
"coefficients = np.polyfit(x, y, deg=1)"
4395+
],
4396+
"metadata": {
4397+
"id": "FddjOrQ_Xd6e"
4398+
},
4399+
"execution_count": null,
4400+
"outputs": []
4401+
},
4402+
{
4403+
"cell_type": "markdown",
4404+
"source": [
4405+
"### **Princial Component Analysis (PCA)**"
4406+
],
4407+
"metadata": {
4408+
"id": "EF7mojZhXsOu"
4409+
}
4410+
},
4411+
{
4412+
"cell_type": "code",
4413+
"source": [
4414+
"# Perform PCA\n",
4415+
"data = np.random.rand(100, 3) # Sample data\n",
4416+
"mean = np.mean(data, axis=0)\n",
4417+
"centered_data = data - mean\n",
4418+
"covariance_matrix = np.cov(centered_data, rowvar=False)\n",
4419+
"eigenvalues, eigenvectors = np.linalg.eig(covariance_matrix)"
4420+
],
4421+
"metadata": {
4422+
"id": "4ef5CfMiX1x-"
4423+
},
4424+
"execution_count": null,
4425+
"outputs": []
4426+
},
4427+
{
4428+
"cell_type": "markdown",
4429+
"source": [
4430+
"# **📚Section 7 -Data Cleaning**"
4431+
],
4432+
"metadata": {
4433+
"id": "Ph4nunOxYNLN"
4434+
}
4435+
},
4436+
{
4437+
"cell_type": "markdown",
4438+
"source": [
4439+
"## **7.1-Identifying Missing Values**"
4440+
],
4441+
"metadata": {
4442+
"id": "pOWkZiDAYctQ"
4443+
}
4444+
},
4445+
{
4446+
"cell_type": "code",
4447+
"source": [
4448+
"# Create a NumPy array with missing values\n",
4449+
"data = np.array([1, 2, np.nan, 4, np.nan, 6])\n",
4450+
"# Check for missing values\n",
4451+
"has_missing = np.isnan(data)\n",
4452+
"print(has_missing)"
4453+
],
4454+
"metadata": {
4455+
"id": "Erq0fabkYsn7",
4456+
"outputId": "d7be023e-3def-49a7-da61-2419a7132d02",
4457+
"colab": {
4458+
"base_uri": "https://localhost:8080/"
4459+
}
4460+
},
4461+
"execution_count": 8,
4462+
"outputs": [
4463+
{
4464+
"output_type": "stream",
4465+
"name": "stdout",
4466+
"text": [
4467+
"[False False True False True False]\n"
4468+
]
4469+
}
4470+
]
4471+
},
4472+
{
4473+
"cell_type": "markdown",
4474+
"source": [
4475+
"## **7.2-Removing Rows or Columns with Missing Values**"
4476+
],
4477+
"metadata": {
4478+
"id": "QtXF8_DVYyXK"
4479+
}
4480+
},
4481+
{
4482+
"cell_type": "code",
4483+
"source": [
4484+
"# Create a 2D array with missing values\n",
4485+
"data = np.array([[1, 2, 3], [4, np.nan, 6], [7, 8, 9]])\n",
4486+
"# Remove rows with any missing values\n",
4487+
"cleaned_data = data[~np.any(np.isnan(data), axis=1)]\n",
4488+
"print(cleaned_data) # Result: [[1,2,3],[7,8,9]]"
4489+
],
4490+
"metadata": {
4491+
"id": "KwfwO7WhY6Zj"
4492+
},
4493+
"execution_count": null,
4494+
"outputs": []
4495+
},
4496+
{
4497+
"cell_type": "markdown",
4498+
"source": [
4499+
"## **7.3-Removing Outliers**"
4500+
],
4501+
"metadata": {
4502+
"id": "CB75ZdyxY9Tz"
4503+
}
4504+
},
4505+
{
4506+
"cell_type": "code",
4507+
"source": [
4508+
"data = np.array([1, 2, 3, 100, 101, 102])\n",
4509+
"threshold = np.percentile(data, 95) # Get 95th percentile\n",
4510+
"cleaned_data = data[data <= threshold] # Remove outliers above threshold"
4511+
],
4512+
"metadata": {
4513+
"id": "eN7U5f33ZAr6"
4514+
},
4515+
"execution_count": null,
4516+
"outputs": []
4517+
},
4518+
{
4519+
"cell_type": "markdown",
4520+
"source": [
4521+
"## **7.4-Removing Duplicates**"
4522+
],
4523+
"metadata": {
4524+
"id": "P_TZjeqmZrhc"
4525+
}
4526+
},
4527+
{
4528+
"cell_type": "code",
4529+
"source": [
4530+
"data = np.array([1, 2, 3, 2, 4, 5, 1])\n",
4531+
"unique_values = np.unique(data) # Get unique values"
4532+
],
4533+
"metadata": {
4534+
"id": "HidXyEwNZxIx"
4535+
},
4536+
"execution_count": null,
4537+
"outputs": []
4538+
},
41664539
{
41674540
"cell_type": "markdown",
41684541
"source": [

0 commit comments

Comments
 (0)