numpy.ravel() in Python

numpy.ravel() is a function present in numpy module which allows us to change a 2 dimensional or multi dimensional array into a contiguous flattened array i.e 1 dimensional array with all input elements and of same type as it. A copy is made only when needed. If input array is masked then returned array is also masked.

Syntax:

numpy.ravel(x, order=’C’)

Parameters:

x : array_like

This reads the input array and all the elements are read in the order specified by the order parameter.

order:{‘C’,’F’,’A’,’K’} (optional)

This order parameter is optional.

  • Order parameter ‘C’ means array flattens in row major order. The last axis change is fastest and the first axis change is slowest.
  • Order parameter ‘F’  (Fortran contiguous memory order) means array flattens in the column major order. Here the first axis change is fastest and last axis change is slowest.
  • Order parameter ‘A’ means read / write elements in Fortran like index order only if array is fortran contiguous memory order otherwise C like order.
  • Order parameter ‘K’ means read / write elements in order present as it is in the system.

Returns:

This function returns a contiguous flattened array with same data type as input array and has equal size (x.size)

Example1:

import numpy as np 

x = np.array([[4, 7, 8, 9], [16, 24, 86,45]]) 

y=np.ravel(x) 

y

Example2:

import numpy as np

x = np.array([[23, 76, 11, 42], [74, 91, 8, 34]])

y = np.ravel(x)

print('Flattened array: \n', y)

y[1] = 121

print('Original array: \n', x)

The above example shows that any changes in flattened array will reflect in original array as you can see that value y[1] = 121 is changed in original array as well.

Example3:

import numpy as np

a = np.arange(12).reshape(3,4)

print('The original array is:\n',a)

print('\n') 

print('After applying ravel function:',a.ravel())

print('\n' )

print('Applying ravel function in F-style ordering:',a.ravel(order = 'F'))

print('Applying ravel function in C-style ordering:',a.ravel(order = 'C'))

print('Applying ravel function in K-style ordering:',a.ravel(order = 'K'))

print('Applying ravel function in A-style ordering:',a.ravel(order = 'A'))

With the above examples we have seen about the ravel function and its ordering styles in detail. Now if you want to know more about numpy click here!

numpy.ndarray.flatten() in Python:

Numpy flatten() converts a multi dimensional array into ‘flattened’ one dimensional array. It returns a copy of the array in one dimension. Now let us see about numpy.ndarray.flatten().

numpy.ndarray.flatten()

Syntax:

ndarray.flatten(order = ‘C’)

Parameters:

  • order: The order in which items from numpy array wil be used.
  • ‘C’ – read items row wise i.e, using row major order
  • ‘F’ – read items column wise i.e, column major order
  • ‘K’ – read items in the order that occur in the memory
  • ‘A’ – read items column wise only when the array is Fortran contiguous in memory.
  • The default is ‘C’

Returns:

A copy of input array, flattened  to 1D array.

Example:

Flatten an array by row:
import numpy as np 

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

b=a.flatten('C') 

print('Flattened array by row:\n', b)
Flatten an array by column:
import numpy as np 

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

b=a.flatten('F') 

print('Flattened array by column:\n', b)

ndarray.flatten() returns the copy of the original array any changes made in flattened array will not be reflected in original array.

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

flat_array = a.flatten()

flat_array[2] = 10

print('Flattened 1D Numpy Array:')

print(flat_array)

print('Original Numpy Array')

print(a)

If you look the above code when we change the array value of index 2 as 10 i.e flat_array[2] = 10, it won’t affect the original array only the copy is changed.

To know more about numpy click here!

numpy.log() or np.log() in Python

What is numpy.log()?

numpy.log() is a mathematical function that helps user to calculate Natural logarithm of x where x belongs to imput array elements. The natural logarithm log is an inverse of exponential function log(exp(x)) = x. It is a logarithm of base ‘e’. It calculates the mathematical log of any number or array.

Syntax:

Parameters:

3 parameters of np.log are x, out and where.

out and where are rarely used.

x provide input to function. This parameter accepts array like objects as input. Like it will also accept Python list as input.

How to use np.log() in Python?

First we have to import numpy module using the command.

import numpy as np

Now the numpy module is imported.

np.log with a single number:

We will try to apply log on numbers and on mathematical constant e, Euler’s number.

import numpy as np
np.log(2)
import numpy as np
print(np.e)
np.log(np.e)

What will happen if we use log on 0?

Lets see what happens

import numpy as np
print(np.log(0))

Calculating log with base 2 and base 10:

import numpy as np
print(np.log2(8))
import numpy as np
print(np.log2(30))
import numpy as np
print(np.log10(30))
import numpy as np
print(np.log10(100))

Calculating log on 1D array:

To calculate the logarithm of a 1D array:

import numpy as np
np.log([3,4,5,7])
import numpy as np
arr1=np.array([1,3,5,5**3])
print(arr1)
arr2=np.log(arr1)
print(arr2)
arr3=np.log2(arr1)
print(arr3)
arr4=np.log10(arr1)
print(arr4)

Calculating log on 2D array:

To calculate the logarithm of a 2D array:

import numpy as np
nparray = np.arange(1,10).reshape(3,3)
print("Original np 2D Array:\n",nparray)
print("\n Logarithmic value of 2D np array elements:\n",np.log(nparray))

Plotting log using matplotlib:

Let’s try plotting a graph for the logarithmic function using matplotlib. If you are not familiar with matplotlib, we have a separate article on this check here.

import numpy as np
import matplotlib.pyplot as plt
arr1 = [3,2.1,2.2,3.2,3.3]
result1=np.log(arr1)
result2=np.log2(arr1)
result3=np.log10(arr1)
plt.plot(arr1,arr1, color='green', marker="") plt.plot(result1,arr1, color='red', marker="o") plt.plot(result2,arr1, color='blue', marker="")
plt.plot(result3,arr1, color='black', marker="o")
plt.show()

Temporal accusative and genitive prepositions

This is a continuation of previous blog, if you want to know about temporal prepositions please click here!

Akkusativ:

für – for

for how long?

A period of time in future

Ich bleibe nur für drei Tage in Kiel.

Mein Freund will in den Sommerferien für zwei Monaten reisen.

Er hat den Bewerber für 17Uhr bestellt.

gegen – about

When?, At what time?

An imprecise point in time

Es ist gegen 12 Uhr.

Ich kann dich erst gegen Abend abholen.

Ich komme dann so gegen Mittag.

über – over

How long?

Time period

Ich warte schon über eine Stunde auf dich!

Über die Karnevelstage soll es laut Wetterbericht stürmisch werden.

Ich fahre übers Wochenende weg.

Sometimes, über is interchanged with mehr als or länger als

Das Haus ist über 30 Jahre alt.

Das Haus ist mehr als 30 Jahre alt.

Ich suche schon über einem Jahr nach einem neuen Job.

Ich suche schon länger als einem Jahr nach einem neuen Job.

um – at, around.

At what time?, When?

Der Unterricht beginnt um 9.00 Uhr.

Die Veranstaltung beginnt um 18.00 Uhr.

Wir treffen uns wieder um Weihnachten.

Der Zug fährt um 17:34 Uhr vom Frankfurter Hauptbahnhof ab

Genitive:

außerhalb – outside

When?

Period of time

Außerhalb der Sprechstunden wenden Sie sich bitte an den Notdienst.

Außerhalb der Öffnungszeiten geht bei uns niemand ans Telefon.

Außerhalb der Öffnungszeiten der Bank gibt es einen Kundenservice im Rahmen des Selbstbedienungsangebots. 

innerhalb – inside, in, within

When?

period of time

Der Schnee ist innerhalb eines Tages geschmolzen.

Innerhalb einer Stunde müssen alle die Hausafgaben bei mir abgegeben haben.

Ich werde innerhalb der nächsten drei Stunden antworten.

während – during

When?

period of time

Während des Semesters werden viele Partys organisiert.

Die Kinder waren während der Sommerferien bei ihren Großeltern auf dem Land.

Während des Spiels kaufte er etwas zu trinken.

These are all the temporal prepositions in accusative and genitive.

Temporal prepositions or prepositions of time

Show the relationship of noun, pronoun and articles to time. It always answers the questions like “when?” and “how long?” In temporal we speak about time period, time span or point of time.

It always answers the questions

  • bis wann?
  • wie lange?
  • wann?
  • um wie viel Uhr?
  • ab/seit wann?
  • von wann bis wann?

To understand better we will try to group these according the German cases accusative, dative and genitive.

In this article we will see about temporal prepositions with dative case

Dative:

ab -from

“When will something begin?”

The time or date something begins.

Ab morgen bin ich im Urlaub

Ab Juli lerne ich Spanisch

Ich bin ab Montag in Rom

an / am – on, in, at

When?

Can be used with specific point in time (date, day of the week, time of the day, or festival)

Wir fahren am Montag in den Urlaub

An Weihnachten besuchen wir unsere Familie.

Am Morgen bin ich müde.

aus – from

When?

Time of origin

Ich erinnere mich daran, aus meiner Kindheit.

Diese Kirche ist aus dem 19. Jahrhundert.

bei – while, at the same time, during

period of time when some things are done simultaneously.

Beim Abendessen sehe ich Nachrichten.

Vorsicht bei der Abfahrt des Zuges!

Beim Essen soll man kein Wasser trinken.

nach – after (opposite of vor)

When?

A point of time after a specific event (opposite of vor)

Nach einer Minute war unser Gespräch schon beendet.

Wir sehen nach dem Abendessen fern.

Nach der Arbeit schläft er.

in

when?

particular point in time

Er las das Buch in 2 Stunden.

In einer Stunde wollen wir essen.

Die Schüler gehen 5 Tage in der Woche zur Schule

seit – since, for

Since when?

Period of time that began in past and still going on in present.

Seit Februar lerne ich Deutsch.

Seit meiner Verletzung kann ich nicht mehr Fußball spielen.

Wir wohnen seit 5 Jahren in Deutschland.

vor – before

when?

point in time before a specific event (opposite of nach)

Vor den Nachrichten lief eine Musiksendung.

Vor der Arbeit macht er Sport.

Die Kinder spielen vor dem Unterricht.

von… an – starting on, from

A point of time in future

Von nächster Woche an will er mit dem Rauchen aufhören.

Sie kannten sich von Kindheit an.

Vom 1.Juni an.

von … bis – from… to

How long?, From when to?

A period of time with start and end.

Vom 1. Juni bis 5.Juni.

Unsere Buros sind wegen Renovierungsarbeiten vom 1.Juni bis zum 15.Juni geschlossen.

zwischen – between

When?

Specified period of time

Das geschah zwischen dem 1. und 5. Mai

Zwischen 6.00 und 7.00 Uhr.

Der Handwerker kommt zwischen 7 und 11 Uhr.

For Akkusative and Genitive Prepositions please click here!

6 Modal Verbs in German

What are modal verbs?

Six model verbs in German are: können, dürfen, mögen, müssen, wollen, sollen. They assist another verb in a sentence. They describe a relationship between subject and ectivity expressed with main verb. Modal verbs refer to verbs that express mood like want to, need to, like to, etc.

Meaning of modal verbs:

1.können: to be able to / ability / possibility / can

Sie können sehr laut singen.

Ich kann Deutsch sprechen

Thomas kann bei Niko wohnen.

2.dürfen : allow / permission

Wir dürfen heute ins Kino gehen.

Emma darf eine Freundin besuchen.

Wir dürfen auf diesem Projekt zusammenarbeiten.

dürfen + nicht(negation) : forbidden / not allowed

Er darf Fußball nicht spielen.

Wir dürfen heute nicht ins Kino gehen.

3.müssen : need to / must / have to / responsibility / necessity

Sie müssen einen Job finden.

Ich muss lernen, weil ich Montag eine Prüfüng habe.

Ihr müsst früh ins Bett gehen.

4.wollen – want / wish

Ich will einen schönen Tag.

Sie wollen am Samstag schwimmen.

Lisa will heute Abend mit Niko ins Kino gehen.

5.mögen – like / desire

Ich mag dieses Auto.

Obwohl sie die Schokolade mag, mag sie den Kuchen mehr.

Wir mögen im Schatten spielen.

The verb ‚mögen‘ is used very often in Konjunktiv II when we ask for something politely.

möchten – would like to

Ich möchte gerne in Bier trinken.

Wir möchten am Wochenende Ski laufen

Ich möchte bestellen.

6.sollen – should / commands / requests

Das Kind soll heute die erste Impfung erhalten.

Ich soll meine Hausaufgaben machen, aber ich will schlafen.

Wir sollen einen Film sehen.

Conjugation of modal verbs in Present tense:

dürfenkönnenmögenmüssensollenwollen
ichdarfkannmagmusssollwill
dudarfstkannstmagstmusstsollstwillst
er/sie/esdarfkannmagmusssollwill
wirdürfenkönnenmögenmüssensollenwollen
ihrdürftkönntmögtmüsstsolltwollt
sie/Siedürfenkönnenmögenmüssensollenwollen

Conjugation of modal verbs in Past tense:

dürfenkönnenmögenmüssensollenwollen
ichdurftekonntemochtemusstesolltewollte
dudurftestkonntestmochtestmusstestsolltestwolltest
er/sie/esdurftekonntemochtemusstesolltewollte
wirdurftenkonntenmochtenmusstensolltenwollten
ihrdurftetkonntetmochtetmusstetsolltetwolltet
sie/Siedurftenkonntenmochtenmusstensolltenwollten

Modal verbs are irregular.

The form of first and third person is always same.

In first person singular ( ich, du, er/es/sie) no umlauts.

In simple past no umlaut at all.

How to use modal verbs in a Sentence?

In a sentence the modal verb is conjugated and it takes the second position. Then the infinitive form of main verb goes to the end of the sentence.

Der Mann darf im Restaurant nicht rauchen.

Sentences with conjunction:

               In sentences with conjunction the conjugated modal verb moves to the end.

Ich rufe dich an, weil ich heute nicht kommen kann.

Maria ist zu Hause, obwohl sie arbeiten muss.

mögen and können as main verb:

Ich mag Schokolade.

Kannst du Deutsch?

5 Denglisch words you need to know

When we see an advertisement or a sign board in German we will usually notice some English words. Those words usually won’t mean the same way as in English and often have a totally different meaning.

Let’s see some of them!

1.das Handy – mobile phone

This is the most common word used everyday. In English it means something convenient.

Hast du meine Handy?

2.der Bodybag – shoulder bag

Bodybag means a shoulder bag. But in English a bag used to carry corpse.

Das Essen habe ich in meinem Bodybag gepackt

3.Mailbox – voice mail

Mailbox means voice mail in mobiles. In English its letter box.

Ich habe auf deine Mailbox gesprochen

4.trampen – hitch-hiking

trampen means hitch-hiking. In English tramp is a homeless person.

Nach dem Abitur bin ich durch ganz Europa getrampt.

5.der Beamer – projector

Beamer is projector. But in English its nickname for BMW

Die Präsentation schauen wir uns gleich an Beamer.

There are many more if you know any of them, let me know in comments.

10 Homeoffice Phrases you need to know.

The corona pandemic forced many of us to work from home. As a German learner or expats in Germany many of you may need this phrases while working with your colleagues in office.

1.Sollen wir über Skype oder Zoom sprechen?

Should we speak through skype or zoom?

2.Du bist noch gemutet.

You are still in mute mode.

3.Hallo! Kannst du mich hören?

Hello! Can you hear me?

4.Die Verbindung ist sehr schlecht.

The connection is very bad

5.Du musst dein Mikro anmachen.

You must switch on your microphone.

6.Wie kann ich meinen Bildschirm freigeben?

How can I release my screen?

7.Dein Mikrofon ist ausgeschaltet.

Your microphone is off.

8.Tut mir leid, dass ich nicht beim Meeting war, aber ich hatte Internetprobleme.

Sorry,I didn’t attend the meeting because I had internet problem.

9.Wir beschränken den E-Mail-Verkehr auf das Nötigste.

We limit the E-Mails traffic to the essentials.

10.Ich habe eine Frage bezüglich des Tickets #2345.

I have a question regarding the ticket #2345.

10 German Phrases most used in office

The following phrases will help you get on well in a German working environment.

1.Freut mich, Sie kennen zu lernen, Herr Ruland.

Pleasure to meet you, Mr. Ruland.

2.Können Sie mir bitte eine Mail schreiben?

Can you write me a E-mail?

3.Erklären Sie mir das bitte mal!

Please explain that to me!

4.Da haben wir noch Potenzial.

We can still work with that

5.Ich setze das mal auf meine To-Do Liste.

I will put that on my to-do list.

6.Da warte ich noch auf Feedback.

I am still waiting for Feedback.

7.Schönen Feierabend!

Have a nice evening!

8.Ich bin ein Teamplayer!

I am a teamplayer!

9.Ich kann dir das mal forwarden.

I can forward that to you.

10.Wir freuen uns auf die Zusammenarbeit mit Ihnen.

We look forward to work with you.

8 Weird German Words you will be amazed of!

German has its own strange and weird words and phrases. Let’s see some of those interesting weird german words.

1.Ohrwurm – ear worm

This word describes the fact of being stuck with a song on our head.

Ohrwurm – catchy song.

2.Fernweh – distance pain

This describes a situation where you want to be in a distance place than you are right now.

Fernweh – wanderlust

3.Kopfkino – head cinema

Describes a situation where you are literally playing out a scene in your mind.

Kopfkino – imagination

Ein guter Schriftsteller regt das Kopfkino seines Lesers an.

4.Zungenbrecher – tongue breaker

Describes sequence of words or phrases very hard to pronounce for native speakers.

Zungenbrecher – tongue twister.

5.Weichei – soft egg

Describes someone who is coward or weak- minded. It is also called as Würstchen.

Weichei – coward or weak-minded person

6. Fremdschämen – Exterior shame.

Describes a situation when you feel uncomfortable or awkward, because another person has created an embrassing situation.

Fremdschämen -cringe

7.Lebensmüde – life tierd

Situation where someone does something stupid and life threatening.

lebensmüde – suicide

8.Kuddlemuddle -???

Describes an unstructured mess or chaos.

Kuddlemuddle -messy.